Exemplo n.º 1
0
        /**
         * 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);
        }
Exemplo n.º 2
0
        public static ElementList ParseToElementList(string html, string css)
        {
            ICSSResolver iCSSResolver = new StyleAttrCSSResolver();

            if (css != null)
            {
                ICssFile cSS = XMLWorkerHelper.GetCSS(new MemoryStream(Encoding.Default.GetBytes(css)));
                iCSSResolver.AddCss(cSS);
            }

            MyFontsProvider fontProvider = new MyFontsProvider();
            CssAppliers     cssAppliers  = new CssAppliersImpl(fontProvider);
            //CssAppliers cssAppliers = new CssAppliersImpl(FontFactory.FontImp);
            HtmlPipelineContext htmlPipelineContext = new HtmlPipelineContext(cssAppliers);

            htmlPipelineContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
            htmlPipelineContext.AutoBookmark(false);
            ElementList            elementList = new ElementList();
            ElementHandlerPipeline next        = new ElementHandlerPipeline(elementList, null);
            HtmlPipeline           next2       = new HtmlPipeline(htmlPipelineContext, next);
            CssResolverPipeline    pipeline    = new CssResolverPipeline(iCSSResolver, next2);
            XMLWorker listener  = new XMLWorker(pipeline, true);
            XMLParser xMLParser = new XMLParser(listener);

            //因为XMLWork不支持html的单标签,所以要对单标签进行过滤。不然就会报错:Invalid nested tag div found, expected closing tag br
            html = html.Replace("<br>", "").Replace("<hr>", "").Replace("<img>", "").Replace("<param>", "")
                   .Replace("<link>", "");

            //xMLParser.Parse(new MemoryStream(Encoding.Default.GetBytes(html)));
            xMLParser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(html)));
            return(elementList);
        }
Exemplo n.º 3
0
        public void SetUp()
        {
            lst = new List <IWritable>();
            IElementHandler elemH = new CustomElementHandler();

            p        = new ElementHandlerPipeline(elemH, null);
            po       = new ProcessObject();
            writable = new WritableElement(new Chunk("aaaaa"));
            po.Add(writable);
            context = new WorkerContextImpl();
            p.Init(context);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a <see cref="PdfTable"/> object from specified <b>HTML</b> code.
        /// </summary>
        /// <param name="html">A reference to input html code to convert.</param>
        /// <param name="css">A reference to css styles to apply.</param>
        /// <param name="config">Table configuration reference.</param>
        /// <returns>
        /// A new <see cref="PdfPTable"/> that contains a
        /// </returns>
        public static PdfTable CreateFromHtml(string html, string css = default, PdfTableConfig config = null)
        {
            var hasCss = !string.IsNullOrEmpty(css);

            if (!hasCss)
            {
                css = " ";
            }

            // css
            StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver();
            ICssFile             cssFile     = XMLWorkerHelper.GetCSS(css.AsStream());

            cssResolver.AddCss(cssFile);

            // html
            XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts)));
            CssAppliers           cssAppliers  = new CssAppliersImpl(fontProvider);
            HtmlPipelineContext   htmlContext  = new HtmlPipelineContext(cssAppliers);

            htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

            // pipelines
            ElementList            elements     = new ElementList();
            ElementHandlerPipeline pdf          = new ElementHandlerPipeline(elements, null);
            HtmlPipeline           htmlPipeline = new HtmlPipeline(htmlContext, pdf);
            CssResolverPipeline    cssPipeline  = new CssResolverPipeline(cssResolver, htmlPipeline);

            // XML Worker
            XMLWorker worker = new XMLWorker(cssPipeline, true);
            XMLParser parser = new XMLParser(worker, Encoding.UTF8);

            parser.Parse(html.AsStream(Encoding.UTF8));

            PdfPTable nativeTable = (PdfPTable)elements[0];

            return(new PdfTable(nativeTable, config));
        }