本文整理汇总了C#中iTextSharp.tool.xml.pipeline.html.HtmlPipelineContext类的典型用法代码示例。如果您正苦于以下问题:C# HtmlPipelineContext类的具体用法?C# HtmlPipelineContext怎么用?C# HtmlPipelineContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HtmlPipelineContext类属于iTextSharp.tool.xml.pipeline.html命名空间,在下文中一共展示了HtmlPipelineContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GeneratePdfFromView
public byte[] GeneratePdfFromView(string viewString, string[] cssPaths, string fontPath)
{
using (var memoryStream = new MemoryStream())
{
var doc = new Document(PageSize.A4);
var writer = PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
var tagProcessors = InitializeTagProcessor();
var cssResolver = InitializeCssFiles(cssPaths);
var fontProvider = new CustomFontFactory(fontPath);
var hpc = new HtmlPipelineContext(new CssAppliersImpl(fontProvider));
hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagProcessors);
var htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, writer));
var pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
var worker = new XMLWorker(pipeline, true);
var xmlParser = new XMLParser(true, worker, Encoding.Unicode);
xmlParser.Parse(new StringReader(viewString));
doc.Close();
return memoryStream.ToArray();
}
}
示例2: SetUp
virtual public void SetUp() {
parent = new Tag("body");
parent.CSS[CSS.Property.FONT_FAMILY] = "Helvetica";
parent.CSS[CSS.Property.FONT_SIZE] = "12pt";
first = new Tag(null);
first.CSS[CSS.Property.FONT_FAMILY] = "Helvetica";
first.CSS[CSS.Property.FONT_SIZE] = "12pt";
second = new Tag(null);
second.CSS[CSS.Property.FONT_FAMILY] = "Helvetica";
second.CSS[CSS.Property.FONT_SIZE] = "12pt";
child = new Tag(null);
child.CSS[CSS.Property.FONT_FAMILY] = "Helvetica";
child.CSS[CSS.Property.FONT_SIZE] = "12pt";
parent.AddChild(first);
first.Parent = parent;
second.Parent = parent;
first.AddChild(child);
second.AddChild(child);
parent.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(parent) + "pt";
first.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(first) + "pt";
first.CSS[CSS.Property.TEXT_ALIGN] = CSS.Value.LEFT;
second.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(second) + "pt";
child.CSS[CSS.Property.FONT_SIZE] = fst.TranslateFontSize(child) + "pt";
firstPara = new Paragraph(new Chunk("default text for chunk creation"));
secondPara = new Paragraph(new Chunk("default text for chunk creation"));
configuration = new HtmlPipelineContext(null);
applier.Apply(firstPara, first, configuration);
}
示例3: MakePdf
protected override void MakePdf(string outPdf) {
Document doc = new Document(PageSize.A4.Rotate());
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(outPdf, FileMode.Create));
doc.SetMargins(45, 45, 0, 100);
doc.Open();
CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.Add(
XMLWorkerHelper.GetCSS(
File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
"main.css")));
cssFiles.Add(
XMLWorkerHelper.GetCSS(
File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
"widget082.css")));
StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
HtmlPipelineContext hpc =
new HtmlPipelineContext(
new CssAppliersImpl(new XMLWorkerFontProvider(RESOURCES + @"\tool\xml\examples\fonts")));
hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
hpc.SetImageProvider(new SampleTestImageProvider());
hpc.SetPageSize(doc.PageSize);
HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(true, worker, Encoding.GetEncoding("UTF-8"));
p.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8"));
doc.Close();
}
示例4: TransformHtml2Pdf
protected override void TransformHtml2Pdf() {
Document doc = new Document(PageSize.A1.Rotate());
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(outPdf, FileMode.Create));
doc.SetMargins(doc.LeftMargin - 10, doc.RightMargin - 10, doc.TopMargin, doc.BottomMargin);
doc.Open();
CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.Add(
XMLWorkerHelper.GetCSS(
File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + testName +
Path.DirectorySeparatorChar + "complexDiv02_files" + Path.DirectorySeparatorChar +
"minimum0.css")));
cssFiles.Add(
XMLWorkerHelper.GetCSS(
File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + testName +
Path.DirectorySeparatorChar + "complexDiv02_files" + Path.DirectorySeparatorChar +
"print000.css")));
cssFiles.Add(XMLWorkerHelper.GetCSS(File.OpenRead(RESOURCES + @"\tool\xml\examples\" + "sampleTest.css")));
StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
HtmlPipelineContext hpc =
new HtmlPipelineContext(
new CssAppliersImpl(new XMLWorkerFontProvider(RESOURCES + @"\tool\xml\examples\fonts")));
hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
hpc.SetImageProvider(new SampleTestImageProvider());
HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(true, worker, Encoding.GetEncoding("UTF-8"));
p.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8"));
doc.Close();
}
示例5: ParseXfaOnlyXML
virtual public void ParseXfaOnlyXML() {
StreamReader bis = File.OpenText(RESOURCE_TEST_PATH + SNIPPETS + TEST + "snippet.html");
Document doc = new Document(PageSize.A4);
float margin = utils.ParseRelativeValue("10%", PageSize.A4.Width);
doc.SetMargins(margin, margin, margin, margin);
PdfWriter writer = null;
try {
writer = PdfWriter.GetInstance(doc, new FileStream(
TARGET + TEST + "_charset.pdf", FileMode.Create));
}
catch (DocumentException e) {
Console.WriteLine(e);
}
CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS());
StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
HtmlPipelineContext hpc = new HtmlPipelineContext(null);
hpc.SetAcceptUnknown(true)
.AutoBookmark(true)
.SetTagFactory(Tags.GetHtmlTagProcessorFactory())
.CharSet(Encoding.GetEncoding("ISO-8859-1"));
IPipeline pipeline = new CssResolverPipeline(cssResolver,
new HtmlPipeline(hpc, new PdfWriterPipeline(doc, writer)));
XMLWorker worker = new XMLWorker(pipeline, true);
doc.Open();
XMLParser p = new XMLParser(true, worker);
p.Parse(bis);
doc.Close();
}
示例6: HtmlToPdf
public void HtmlToPdf(string htmlFile, string pdfFile, string htmlImageDirectory)
{
using (FileStream pdfStream = new FileStream(pdfFile, FileMode.OpenOrCreate))
{
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, pdfStream);
doc.Open();
//TODO: apply external css
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
//HTML
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
htmlContext.SetImageProvider(new ImageProvider(htmlImageDirectory));
htmlContext.SetLinkProvider(new LinkProvider("/"));
//pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(doc, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(true, worker, Encoding.UTF8);
using (TextReader reader = File.OpenText(htmlFile))
{
p.Parse(reader);
}
doc.Close();
}
}
示例7: TestContentToChunk
virtual public void TestContentToChunk() {
Anchor a = new Anchor();
Tag t = new Tag("dummy");
String content2 = "some content";
WorkerContextImpl ctx = new WorkerContextImpl();
HtmlPipelineContext htmlPipelineContext = new HtmlPipelineContext(null);
ctx.Put(typeof (HtmlPipeline).FullName, htmlPipelineContext);
a.SetCssAppliers(new CssAppliersImpl());
IList<IElement> ct = a.Content(ctx, t, content2);
Assert.AreEqual(content2, ct[0].Chunks[0].Content);
}
示例8: SetupDefaultProcessingYourself
virtual public void SetupDefaultProcessingYourself() {
Document doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(doc,
new FileStream(TARGET + "columbus2.pdf", FileMode.Create));
doc.Open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext,
new PdfWriterPipeline(doc, writer)));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(worker);
p.Parse(File.OpenRead(RESOURCES + @"\examples\columbus.html"));
doc.Close();
}
示例9: SetUp
virtual public void SetUp() {
cells = new List<Element>();
tag = new Tag("td", new Dictionary<String, String>());
basicPara = new NoNewLineParagraph();
basic = new Chunk("content");
cell = new HtmlCell();
applier = new HtmlCellCssApplier();
LoggerFactory.GetInstance().SetLogger(new SysoLogger(3));
Tag parent = new Tag("tr");
parent.Parent = new Tag("table");
tag.Parent = parent;
basicPara.Add(basic);
cell.AddElement(basicPara);
cells.Add(cell);
config = new HtmlPipelineContext(null);
}
示例10: TransformHtml2Pdf
protected override void TransformHtml2Pdf(Document doc, PdfWriter pdfWriter, IImageProvider imageProvider,
IFontProvider fontProvider, Stream cssFile) {
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
HtmlPipelineContext hpc;
if (fontProvider != null)
hpc = new HtmlPipelineContext(new CssAppliersImpl(fontProvider));
else
hpc = new HtmlPipelineContext(null);
hpc.SetImageProvider(imageProvider);
hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser xmlParse = new XMLParser(true, worker, Encoding.UTF8);
xmlParse.Parse(new FileStream(inputHtml, FileMode.Open), Encoding.UTF8);
}
示例11: SetUp
virtual public void SetUp() {
LoggerFactory.GetInstance().SetLogger(new SysoLogger(3));
root = new Tag("body");
p = new Tag("p");
ul = new Tag("ul");
first = new Tag("li");
last = new Tag("li");
single = new ListItem("Single");
start = new ListItem("Start");
end = new ListItem("End");
listWithOne = new List<IElement>();
listWithTwo = new List<IElement>();
orderedUnorderedList = new OrderedUnorderedList();
CssAppliersImpl cssAppliers = new CssAppliersImpl();
orderedUnorderedList.SetCssAppliers(cssAppliers);
workerContextImpl = new WorkerContextImpl();
HtmlPipelineContext context2 = new HtmlPipelineContext(cssAppliers);
workerContextImpl.Put(typeof (HtmlPipeline).FullName, context2);
root.AddChild(p);
root.AddChild(ul);
ul.AddChild(first);
ul.AddChild(last);
p.CSS["font-size"] = "12pt";
p.CSS["margin-top"] = "12pt";
p.CSS["margin-bottom"] = "12pt";
new ParagraphCssApplier(cssAppliers).Apply(new Paragraph("paragraph"), p, context2);
first.CSS["margin-top"] = "50pt";
first.CSS["padding-top"] = "25pt";
first.CSS["margin-bottom"] = "50pt";
first.CSS["padding-bottom"] = "25pt";
last.CSS["margin-bottom"] = "50pt";
last.CSS["padding-bottom"] = "25pt";
listWithOne.Add(single);
listWithTwo.Add(start);
listWithTwo.Add(end);
}
示例12: MakePdf
protected override void MakePdf(string outPdf) {
Document doc = new Document(PageSize.A3.Rotate());
PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(outPdf, FileMode.Create));
pdfWriter.CreateXmpMetadata();
doc.SetMargins(200, 200, 0, 0);
doc.Open();
CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.Add(
XMLWorkerHelper.GetCSS(
File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
"main.css")));
cssFiles.Add(
XMLWorkerHelper.GetCSS(
File.OpenRead(RESOURCES + Path.DirectorySeparatorChar + testPath + Path.DirectorySeparatorChar + testName +
Path.DirectorySeparatorChar + "complexDiv_files" + Path.DirectorySeparatorChar +
"widget082.css")));
StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
HtmlPipelineContext hpc =
new HtmlPipelineContext(
new CssAppliersImpl(new XMLWorkerFontProvider(RESOURCES + @"\tool\xml\examples\fonts")));
hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory());
hpc.SetImageProvider(new SampleTestImageProvider());
hpc.SetPageSize(doc.PageSize);
HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, pdfWriter));
IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(true, worker, Encoding.GetEncoding("UTF-8"));
p.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8"));
//ICC_Profile icc = ICC_Profile.getInstance(ComplexDiv01Test.class.getResourceAsStream("sRGB Color Space Profile.icm"));
//pdfWriter.setOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);
doc.Close();
}
示例13: Apply
virtual public PdfDiv Apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc, HtmlPipelineContext context)
{
if (t.Attributes.ContainsKey("id"))
div.Tag = t.Attributes["id"];
IDictionary<String, String> css = t.CSS;
float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t);
if (fontSize == Font.UNDEFINED)
{
fontSize = FontSizeTranslator.DEFAULT_FONT_SIZE;
}
String align = null;
if (t.Attributes.ContainsKey(HTML.Attribute.ALIGN))
{
align = t.Attributes[HTML.Attribute.ALIGN];
}
else if (css.ContainsKey(CSS.Property.TEXT_ALIGN))
{
align = css[CSS.Property.TEXT_ALIGN];
}
if (align != null)
{
div.TextAlignment = CSS.GetElementAlignment(align);
}
String widthValue;
if (!css.TryGetValue(HTML.Attribute.WIDTH, out widthValue))
{
t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue);
}
if (widthValue != null)
{
float pageWidth = psc.PageSize.Width;
if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue))
{
div.Width = Math.Min(pageWidth, utils.ParsePxInCmMmPcToPt(widthValue));
}
else if (utils.IsRelativeValue(widthValue))
{
if (widthValue.Contains(CSS.Value.PERCENTAGE))
{
div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f);
}
else
{
div.Width = Math.Min(pageWidth, utils.ParseRelativeValue(widthValue, fontSize));
}
}
}
String heightValue;
if (!css.TryGetValue(HTML.Attribute.HEIGHT, out heightValue))
{
t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue);
}
if (heightValue != null)
{
if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue))
{
div.Height = utils.ParsePxInCmMmPcToPt(heightValue);
}
else if (utils.IsRelativeValue(heightValue))
{
if (heightValue.Contains(CSS.Value.PERCENTAGE))
{
div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f);
}
else
{
div.Height = utils.ParseRelativeValue(heightValue, fontSize);
}
}
}
float? marginTop = null;
float? marginBottom = null;
foreach (KeyValuePair<String, String> entry in css)
{
String key = entry.Key;
String value = entry.Value;
if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT))
{
div.Left = utils.ParseValueToPt(value, fontSize);
}
else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT))
{
if (div.Width == null || div.Left == null)
{
div.Right = utils.ParseValueToPt(value, fontSize);
}
}
else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP))
{
div.Top = utils.ParseValueToPt(value, fontSize);
}
else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM))
{
//.........这里部分代码省略.........
示例14: ParseToElementList
/**
* Parses an HTML string and a string containing CSS into a list of Element objects.
* The FontProvider will be obtained from iText's FontFactory object.
*
* @param html a String containing an XHTML snippet
* @param css a String containing CSS
* @return an ElementList instance
*/
public static ElementList ParseToElementList(String html, String css) {
// CSS
ICSSResolver cssResolver = new StyleAttrCSSResolver();
if (css != null) {
ICssFile cssFile = XMLWorkerHelper.GetCSS(new MemoryStream(Encoding.Default.GetBytes(css)));
cssResolver.AddCss(cssFile);
}
// HTML
CssAppliers cssAppliers = new CssAppliersImpl(FontFactory.FontImp);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
htmlContext.AutoBookmark(false);
// Pipelines
ElementList elements = new ElementList();
ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end);
CssResolverPipeline cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
// XML Worker
XMLWorker worker = new XMLWorker(cssPipeline, true);
XMLParser p = new XMLParser(worker);
p.Parse(new MemoryStream(Encoding.Default.GetBytes(html)));
return elements;
}
示例15: btnGeneratePdf_Click1
protected void btnGeneratePdf_Click1(object sender, EventArgs e)
{
var sb = new StringBuilder();
sb.Append("<table>");
sb.Append("<tr>");
sb.Append("<th class=\"th\">Title</th>");
sb.Append("<th>Author</th>");
sb.Append("</tr>");
MongoDbProvider.db.LoadData<Word>().ToList().ForEach(b =>
{
sb.Append("<tr>");
sb.AppendFormat("<td>{0}</td>", b.WordName);
sb.AppendFormat("<td>{0}</td>", b.Translation);
sb.Append("</tr>");
});
sb.Append("</table>");
using (var document = new Document())
{
PdfWriter writer = PdfWriter.GetInstance(document,
new FileStream(Server.MapPath("loremipsum5.pdf"), FileMode.Append));
document.Open();
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
//change this to your CCS file location
cssResolver.AddCssFile(Server.MapPath("style.css"), true);
var pipeline = new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
var worker = new XMLWorker(pipeline, true);
var p = new XMLParser(worker);
p.Parse(new StringReader(sb.ToString()));
}
}