/**
         * 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);
        }
Exemple #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);
        }
Exemple #3
0
        /* (non-Javadoc)
         * @see com.itextpdf.tool.xml.ITagProcessor#endElement(com.itextpdf.tool.xml.Tag, java.util.List, com.itextpdf.text.Document)
         */
        public override IList <IElement> End(IWorkerContext ctx, Tag tag, IList <IElement> currentContent)
        {
            List <IElement> l = new List <IElement>(1);

            if (currentContent.Count > 0)
            {
                IList <IElement> currentContentToParagraph = CurrentContentToParagraph(currentContent, true, true, tag, ctx);
                foreach (IElement p in currentContentToParagraph)
                {
                    ((Paragraph)p).Role = (getHeaderRole(GetLevel(tag)));
                }
                ParentTreeUtil pt = new ParentTreeUtil();
                try {
                    HtmlPipelineContext context = GetHtmlPipelineContext(ctx);

                    bool oldBookmark = context.AutoBookmark();
                    if (pt.GetParentTree(tag).Contains(HTML.Tag.TD))
                    {
                        context.AutoBookmark(false);
                    }

                    if (context.AutoBookmark())
                    {
                        Paragraph title = new Paragraph();
                        foreach (IElement w in currentContentToParagraph)
                        {
                            title.Add(w);
                        }

                        l.Add(new WriteH(context, tag, this, title));
                    }

                    context.AutoBookmark(oldBookmark);
                } catch (NoCustomContextException e) {
                    if (LOGGER.IsLogging(Level.ERROR))
                    {
                        LOGGER.Error(LocaleMessages.GetInstance().GetMessage(LocaleMessages.HEADER_BM_DISABLED), e);
                    }
                }
                l.AddRange(currentContentToParagraph);
            }
            return(l);
        }
 virtual public void VerifyDefaultBookmarksEnabled()
 {
     Assert.AreEqual(true, ctx.AutoBookmark());
 }