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 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(); }
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); }
virtual public void SetUp() { LoggerFactory.GetInstance().SetLogger(new SysoLogger(3)); css = new StyleAttrCSSResolver(); Dictionary<String, String> pAttr = new Dictionary<String, String>(); pAttr["style"] = "fontk: Verdana; color: blue; test: a"; parent = new Tag("parent", pAttr); Dictionary<String, String> cAttr = new Dictionary<String, String>(); cAttr["style"] = "fontk: Arial; font-size: large; test: b"; child = new Tag("child", cAttr); child.Parent = parent; }
virtual public void SetUp() { StyleAttrCSSResolver css = new StyleAttrCSSResolver(); css.AddCss("dummy { key1: value1; key2: value2 } .aklass { key3: value3;} #dumid { key4: value4}", true); CssResolverPipeline p = new CssResolverPipeline(css, null); Tag t = new Tag("dummy"); t.Attributes["id"] = "dumid"; t.Attributes["class"] = "aklass"; WorkerContextImpl context = new WorkerContextImpl(); p.Init(context); IPipeline open = p.Open(context, t, null); css2 = t.CSS; }
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(); }
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); } } } }
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")); }
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); }
private void processHtml(IElementHandler elementsHandler) { var cssResolver = new StyleAttrCSSResolver(); if (CssFilesPath != null && CssFilesPath.Any()) { foreach (var cssFile in CssFilesPath) { cssResolver.AddCss(XmlWorkerUtils.GetCssFile(cssFile)); } } if (!string.IsNullOrEmpty(InlineCss)) { cssResolver.AddCss(InlineCss, "utf-8", true); } var htmlContext = new HtmlPipelineContext(new CssAppliersImpl(new UnicodeFontProvider(DefaultFont))); if (!string.IsNullOrEmpty(ImagesPath)) { htmlContext.SetImageProvider(new ImageProvider { ImagesPath = ImagesPath }); } htmlContext.CharSet(Encoding.UTF8); var tagsProcessorFactory = (DefaultTagProcessorFactory)Tags.GetHtmlTagProcessorFactory(); if (PdfElement != null) { tagsProcessorFactory.AddProcessor("totalpagesnumber", new TotalPagesNumberXmlWorkerProcessor(PdfElement)); } htmlContext.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagsProcessorFactory); var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new ElementHandlerPipeline(elementsHandler, null))); var worker = new XMLWorker(pipeline, parseHtml: true); var parser = new XMLParser(); parser.AddListener(worker); parser.Parse(new StringReader(Html)); }
/** * @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); } }
/** * Get a CSSResolver implementation. * * @param addDefaultCss true if the defaultCss should already be added. * @return the default CSSResolver * */ public ICSSResolver GetDefaultCssResolver(bool addDefaultCss) { ICSSResolver resolver = new StyleAttrCSSResolver(); if (addDefaultCss) { resolver.AddCss(GetDefaultCSS()); } return resolver; }
/** * 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; }