Esempio n. 1
0
        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));
        }
Esempio n. 2
0
        private static void _add_to_table(Guid applicationId, HttpContext context, PdfPTable mainTable,
                                          StyleAttrCSSResolver cssResolver, WikiPDFText text, Font font, PdfPCell cell = null)
        {
            if (cell == null)
            {
                cell = new PdfPCell();
            }

            cell.Border = 0;
            cell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
            cell.RunDirection        = text.IsRTL ? PdfWriter.RUN_DIRECTION_RTL : PdfWriter.RUN_DIRECTION_LTR;
            cell.SetLeading(0, 2);

            itsXmlHtml.DefaultTagProcessorFactory tagProcessors =
                (itsXmlHtml.DefaultTagProcessorFactory)itsXmlHtml.Tags.GetHtmlTagProcessorFactory();

            tagProcessors.RemoveProcessor(itsXmlHtml.HTML.Tag.IMG);                                          // remove the default processor
            tagProcessors.AddProcessor(itsXmlHtml.HTML.Tag.IMG, new CustomImageTagProcessor(applicationId)); // use our new processor

            //tagProcessors.AddProcessor(itsXmlHtml.HTML.Tag.TABLE, new TableTagProcessor());

            HtmlPipelineContext htmlContext =
                new HtmlPipelineContext(new itsXmlHtml.CssAppliersImpl(new IranSansFontProvider(font)));

            htmlContext.SetImageProvider(new ImageProvider());
            htmlContext.CharSet(Encoding.UTF8);
            htmlContext.SetAcceptUnknown(true).AutoBookmark(true).SetTagFactory(tagProcessors);

            ElementsCollector   elementsHandler = new ElementsCollector();
            CssResolverPipeline pipeline        = new CssResolverPipeline(cssResolver,
                                                                          new HtmlPipeline(htmlContext, new ElementHandlerPipeline(elementsHandler, null)));

            itsXml.XMLWorker worker = new itsXml.XMLWorker(pipeline, parseHtml: true);
            XMLParser        parser = new XMLParser(true, worker, Encoding.UTF8);

            parser.Parse(new StringReader(text.Text));

            cell.AddElement(elementsHandler.Paragraph);
            mainTable.AddCell(cell);
        }