PDF file itextpdf Insert HTML content and Chinese solution

Source: Internet
Author: User
Tags closing tag

Brief Introduction

Currently there are many kinds of HTML files on the Web directly to the technical posts in PDF, but very few directly to the part of HTML as a paragraph into the PDF, and there is no good solution to the Chinese display problem.

So this morning, a study was conducted around this issue, and the solution was shared with everyone.

Itextpdf Basic Operations please visit: http://www.cnblogs.com/mvilplss/p/5640598.html

Thank you: http://gridmix.blog.51cto.com/4764051/1229585

Implementation ideas

If you want to insert an HTML fragment, we use a static method of a class:

 1  String html = "<div style= ' color:green;font-size:20px; ' > Hello World! Hello World!</div> " 2  Paragraph context = new   Paragraph ();  3  elementlist elementlist =xmlworkerhelper.parsetoelementlist (htmlstring, 
    
     null 
     );  4   ( Element element:elementlist) { 5   co Ntext.add (element);  6   7
      Document.add (context); 

But you will find that you can't display Chinese, there are many ways to solve the problem online, but it doesn't work.

View Xmlworkerhelper.parsetoelementlist (htmlstring, NULL) The source of this method, found

Cssappliers cssappliers = new Cssappliersimpl (Fontfactory.getfontimp ()), font replacement is possible. 
1  Public StaticElementlist parsetoelementlist (string html, string css)throwsIOException {2         //CSS3Cssresolver Cssresolver =Newstyleattrcssresolver ();4         if(CSS! =NULL) {5Cssfile cssfile = Xmlworkerhelper.getcss (NewBytearrayinputstream (Css.getbytes ()));6 cssresolver.addcss (cssfile);7         }8         9         //HTMLTenCssappliers cssappliers =NewCssappliersimpl (Fontfactory.getfontimp ());// The font can be manipulated here  OneHtmlpipelinecontext Htmlcontext =NewHtmlpipelinecontext (cssappliers); A htmlcontext.settagfactory (Tags.gethtmltagprocessorfactory ()); -Htmlcontext.autobookmark (false); -          the         //Pipelines -Elementlist elements =Newelementlist (); -Elementhandlerpipeline end =NewElementhandlerpipeline (Elements,NULL); -Htmlpipeline Htmlpipeline =NewHtmlpipeline (Htmlcontext, end); +Cssresolverpipeline Csspipeline =Newcssresolverpipeline (Cssresolver, htmlpipeline); -          +         //XML Worker AXmlworker worker =NewXmlworker (Csspipeline,true); atXmlparser p =NewXmlparser (worker); -P.parse (NewBytearrayinputstream (Html.getbytes ())); -          -         returnelements; -}

So we think of overriding the GetFont (*) Method of the Xmlworkerfontprovider class, and for fonts that do not have a CSS style declared, the default font is set by default using the Undefine font style.

1  Public classMyxmlworkerhelper {2      Public Static classMyfontsproviderextendsXmlworkerfontprovider {3          PublicMyfontsprovider () {4             Super(NULL,NULL);5         }6 7 @Override8          PublicFont GetFont (FinalString fontname, String encoding,floatSizeFinal intstyle) {9 TenString Fntname =FontName; One             if(Fntname = =NULL) { AFntname = "Song Body"; -             } -             return Super. GetFont (fntname, encoding, size, style); the         } -     } -  -      Public StaticElementlist parsetoelementlist (string html, string css)throwsIOException { +         //CSS -Cssresolver Cssresolver =Newstyleattrcssresolver (); +         if(CSS! =NULL) { ACssfile cssfile = Xmlworkerhelper.getcss (NewBytearrayinputstream (Css.getbytes ())); at cssresolver.addcss (cssfile); -         } -  -         //HTML -Myfontsprovider Fontprovider =NewMyfontsprovider (); -Cssappliers cssappliers =NewCssappliersimpl (fontprovider); inHtmlpipelinecontext Htmlcontext =NewHtmlpipelinecontext (cssappliers); - htmlcontext.settagfactory (Tags.gethtmltagprocessorfactory ()); toHtmlcontext.autobookmark (false); +  -         //Pipelines theElementlist elements =Newelementlist (); *Elementhandlerpipeline end =NewElementhandlerpipeline (Elements,NULL); $Htmlpipeline Htmlpipeline =NewHtmlpipeline (Htmlcontext, end);Panax NotoginsengCssresolverpipeline Csspipeline =Newcssresolverpipeline (Cssresolver, htmlpipeline); -  the         //XML Worker +Xmlworker worker =NewXmlworker (Csspipeline,true); AXmlparser p =NewXmlparser (worker); the        html = html.replace ("<br>", ""). Replace (") . Replace (" <link> "," ");  -P.parse (NewBytearrayinputstream (Html.getbytes ())); $  $         returnelements; -     } -  the}

Because Xmlwork does not support single-label HTML, the label is filtered. Otherwise you will get an error:Invalid nested tag div found, expected closing tag BR

PDF file itextpdf Insert HTML content and Chinese solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.