Наследование: iTextSharp.tool.xml.pipeline.AbstractPipeline
        public virtual void Parse(Stream stream, string xHtml)
        {
            xHtml = XhtmlCleaner != null
                ? XhtmlCleaner(xHtml)
                : XhtmlHelper.CloseSimpleTags(xHtml);
            InitParser();

            using (var stringReader = new StringReader(xHtml))
            {
                using (Document document = new Document())
                {
                    PdfWriter writer = PdfWriter.GetInstance(document, stream);
                    document.Open();

                    HtmlPipelineContext
                        .SetTagFactory(TagProcessorFactory)
                        .SetLinkProvider(LinkProvider)
                        .SetImageProvider(ImageProvider)
                    ;
                    var pdfWriterPipeline = new PdfWriterPipeline(document, writer);
                    var htmlPipeline = new HtmlPipeline(HtmlPipelineContext, pdfWriterPipeline);
                    var cssResolverPipeline = new CssResolverPipeline(CssResolver, htmlPipeline);

                    XMLWorker worker = new XMLWorker(cssResolverPipeline, true);
                    XMLParser parser = new XMLParser(worker);
                    parser.Parse(stringReader);
                }
            }
        }
        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();
            }
        }
Пример #3
0
        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);
        }
        public void ConvertHtmlToPdf(string xHtml, string css)
        {
            using (var stream = new FileStream(OUTPUT_FILE, FileMode.Create))
            {
                using (var document = new Document())
                {
                    var writer = PdfWriter.GetInstance(document, stream);
                    document.Open();

                    // instantiate custom tag processor and add to `HtmlPipelineContext`.
                    var tagProcessorFactory = Tags.GetHtmlTagProcessorFactory();
                    tagProcessorFactory.AddProcessor(
                        new TableDataProcessor(), 
                        new string[] { HTML.Tag.TD }
                    );
                    var htmlPipelineContext = new HtmlPipelineContext(null);
                    htmlPipelineContext.SetTagFactory(tagProcessorFactory);

                    var pdfWriterPipeline = new PdfWriterPipeline(document, writer);
                    var htmlPipeline = new HtmlPipeline(htmlPipelineContext, pdfWriterPipeline);

                    // get an ICssResolver and add the custom CSS
                    var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
                    cssResolver.AddCss(css, "utf-8", true);
                    var cssResolverPipeline = new CssResolverPipeline(
                        cssResolver, htmlPipeline
                    );

                    var worker = new XMLWorker(cssResolverPipeline, true);
                    var parser = new XMLParser(worker);
                    using (var stringReader = new StringReader(xHtml))
                    {
                        parser.Parse(stringReader);
                    }
                }
            }
        }