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(); }
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(); }
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(); }
virtual public void SetUp() { LoggerFactory.GetInstance().SetLogger(new SysoLogger(3)); files = new CssFilesImpl(); StyleAttrCSSResolver resolver = new StyleAttrCSSResolver(files); string u = RESOURCES + "/css/style.css"; resolver.AddCssFile(u.Replace("%20", " "), false); // fix url conversion of space (%20) for File Dictionary<String, String> attr = new Dictionary<String, String>(); t = new Tag("body", attr); }
public void SetUp() { LoggerFactory.GetInstance().SetLogger(new SysoLogger(3)); cssFiles = new CssFilesImpl(); String path = RESOURCES + @"\css\test.css"; path = path.Substring(0, path.LastIndexOf("test.css")); FileRetrieveImpl r = new FileRetrieveImpl(new String[] {path}); StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles, r); HtmlPipelineContext hpc = new HtmlPipelineContext(null); hpc.SetAcceptUnknown(false).AutoBookmark(true).SetTagFactory(Tags.GetHtmlTagProcessorFactory()); IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(hpc, null)); XMLWorker worker = new XMLWorker(pipeline, true); p = new XMLParser(worker); }
private StyleAttrCSSResolver InitializeCssFiles(string[] cssPaths) { var cssFiles = new CssFilesImpl(); cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS()); var cssResolver = new StyleAttrCSSResolver(cssFiles); cssResolver.AddCss(@"body { padding: 2px 4px; }", "utf-8", true); foreach (var cssPath in cssPaths) { cssResolver.AddCssFile(cssPath, false); } return cssResolver; }
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(); }
/** * @param d the ElementHandler * @param inp the Stream * @throws IOException if something went seriously wrong with IO. */ public void ParseXHtml(IElementHandler d, Stream inp, Encoding charset) { CssFilesImpl cssFiles = new CssFilesImpl(); cssFiles.Add(GetDefaultCSS()); StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles); HtmlPipelineContext hpc = new HtmlPipelineContext(null); hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(GetDefaultTagProcessorFactory()); IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(hpc, new ElementHandlerPipeline(d, null))); XMLWorker worker = new XMLWorker(pipeline, true); XMLParser p = new XMLParser(true, worker, charset); if (charset != null) { p.Parse(inp, charset); } else { p.Parse(inp); } }
public void ParseXHtml(PdfWriter writer, Document doc, Stream inp, Stream inCssFile, Encoding charset, IFontProvider fontProvider) { CssFilesImpl cssFiles = new CssFilesImpl(); if (inCssFile != null) cssFiles.Add(GetCSS(inCssFile)); else cssFiles.Add(GetDefaultCSS()); StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles); HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(fontProvider)); hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(GetDefaultTagProcessorFactory()); HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(doc, writer)); IPipeline pipeline = new CssResolverPipeline(cssResolver, htmlPipeline); XMLWorker worker = new XMLWorker(pipeline, true); XMLParser p = new XMLParser(true, worker, charset); if (charset != null) { p.Parse(inp, charset); } else { p.Parse(inp); } }
/** * Parses the xml data. This method configures the XMLWorker to parse * (X)HTML/CSS and accept unknown tags. Writes the output in the given * PdfWriter with the given document. * * @param writer the PdfWriter * @param doc the Document * @param inp the reader * @throws IOException thrown when something went wrong with the IO */ public void ParseXHtml(PdfWriter writer, Document doc, TextReader inp) { CssFilesImpl cssFiles = new CssFilesImpl(); cssFiles.Add(GetDefaultCSS()); StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles); HtmlPipelineContext hpc = new HtmlPipelineContext(null); hpc.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(GetDefaultTagProcessorFactory()); IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(hpc, new PdfWriterPipeline(doc, writer))); XMLWorker worker = new XMLWorker(pipeline, true); XMLParser p = new XMLParser(); p.AddListener(worker); p.Parse(inp); }
static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Invalid number of arguments."); Console.WriteLine("Usage: html2Pdf.exe [input html_file/directory] [default css file]"); return; } List<FileStream> fileList = new List<FileStream>(); if (File.Exists(args[0])) { fileList.Add(new FileStream(args[0], FileMode.Open)); } else if (Directory.Exists(args[0])) { CollectHtmlFiles(fileList, args[0]); } if (fileList.Count == 0) { Console.WriteLine("Invalid html_file/directory"); Console.WriteLine("Usage: html2Pdf.exe [input html_file/directory] [default css file]"); return; } foreach (FileStream fileStream in fileList) { Document doc = new Document(PageSize.LETTER); doc.SetMargins(doc.LeftMargin, doc.RightMargin, 35, 0); String path = Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileStream.Name) + ".pdf"; PdfWriter pdfWriter = PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create)); doc.Open(); Dictionary<String, String> substFonts = new Dictionary<String, String>(); substFonts["Arial Unicode MS"] = "Helvetica"; CssFilesImpl cssFiles = new CssFilesImpl(); cssFiles.Add(XMLWorkerHelper.GetCSS(new FileStream(args[1], FileMode.Open))); StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles); HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(new UnembedFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS, substFonts))); hpc.SetImageProvider(new ImageProvider(args[0])); 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, null); xmlParse.Parse(fileStream); doc.Close(); String cmpPath = Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar + "cmp_" + Path.GetFileNameWithoutExtension(fileStream.Name) + ".pdf"; if (File.Exists(cmpPath)) { CompareTool ct = new CompareTool(path, cmpPath); String outImage = "<testName>-%03d.png".Replace("<testName>", Path.GetFileNameWithoutExtension(fileStream.Name) ); String cmpImage = "cmp_<testName>-%03d.png".Replace("<testName>", Path.GetFileNameWithoutExtension(fileStream.Name) ); String diffPath = Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar + "diff_" + Path.GetFileNameWithoutExtension(fileStream.Name); String errorMessage = ct.Compare(Path.GetDirectoryName(Path.GetFullPath(fileStream.Name)) + Path.DirectorySeparatorChar + "compare" + Path.DirectorySeparatorChar, diffPath); if (errorMessage != null) { Console.WriteLine(errorMessage); } } } }
static protected void TransformHtml2Pdf(FileStream inputHtml, Document doc, PdfWriter pdfWriter) { CssFilesImpl cssFiles = new CssFilesImpl(); cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS()); StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles); String Css1 = "https://specialtyonlinestg.cardinalhealth.com/CardinalHealthSpecialty/themes/html/SS_SPDCardinalHealth/css/globalStyles.css?version=RI_20121023"; String Css2 = "https://specialtyonlinestg.cardinalhealth.com/CardinalHealthSpecialty/themes/html/SS_SPDCardinalHealth/css/SPDCSS/CAHPHReconciliationPortletView.css?version=AC_20120717"; try { cssResolver.AddCssFile(Css1, true); cssResolver.AddCssFile(Css2, true); } catch (CssResolverException e) { // TODO Auto-generated catch block } HtmlPipelineContext htmlContext = new HtmlPipelineContext(new CssAppliersImpl(new XMLWorkerFontProvider())); htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory()); htmlContext.SetPageSize(new Rectangle(doc.Left, doc.Bottom, doc.Right, doc.Top)); // Pipelines PdfWriterPipeline pdf = new PdfWriterPipeline(doc, pdfWriter); HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); XMLWorker worker = new XMLWorker(css, true); XMLParser parser = new XMLParser(worker, Encoding.UTF8); parser.Parse(inputHtml, Encoding.UTF8); }
protected void TransformHtml2Pdf(Document doc, PdfWriter pdfWriter, IImageProvider imageProvider, IFontProvider fontProvider, Stream cssFile) { CssFilesImpl cssFiles = new CssFilesImpl(); if (cssFile == null) cssFile = typeof (XMLWorker).Assembly.GetManifestResourceStream("iTextSharp.tool.xml.css.default.css"); cssFiles.Add(XMLWorkerHelper.GetCSS(cssFile)); StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles); 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.GetEncoding("UTF-8")); xmlParse.Parse(File.OpenRead(inputHtml), Encoding.GetEncoding("UTF-8")); }
/** * Parses the xml data. This method configures the XMLWorker to parse * (X)HTML/CSS and accept unknown tags. Writes the output in the given * PdfWriter with the given document. * * @param writer the PdfWriter * @param doc the Document * @param in the reader * @throws IOException thrown when something went wrong with the IO */ public PdfTemplate ParseToTemplate(PdfContentByte cb, TextReader input) { CssFilesImpl cssFiles = new CssFilesImpl(); cssFiles.Add(GetDefaultCSS()); ICSSResolver cssResolver = new StyleAttrSvgCSSResolver(cssFiles); SvgPipelineContext hpc = new SvgPipelineContext(); hpc.SetTagFactory(GetDefaultTagProcessorFactory()); PdfTemplatePipeline templatePipeline = new PdfTemplatePipeline(cb); IPipeline pipeline = new CssResolverPipeline(cssResolver, new SvgPipeline(hpc, templatePipeline)); XMLWorker worker = new XMLWorker(pipeline, true); XMLParser p = new XMLParser(); p.AddListener(worker); p.Parse(input); return templatePipeline.GetTemplate(); }