HTML Export PDF Chinese file

Source: Internet
Author: User
Tags text to pdf

Today is making a bill export, you need to export the PDF file. So began to find a solution to the sky. In fact, to tell the truth, PDFs do know less, see the garden many people say

Itextsharp.

So specifically to Itextsharp is what has been searched, originally this foreign group of people developed the library of the PDF. So according to download the ITextSharp.DLL and quoted it, the results found that the results are too unsatisfactory. No style no Chinese, this makes people .... Even the rowspan of the cross-line was lost to me. This is not what I need at all. Want to give up, change a thing to do. Then itextsharp the open source project. It turns out that it can support both Chinese and style. Just need to reference his other class library Xmlworker.

Then downloaded the itextsharp-all-5.5.2.zip and Itextsharp.xmlworker-all-5.5.2.zip and then the perfect implementation.

The relevant code is as follows:

This class of files for the implementation of Chinese transcoding problems

Using itextsharp.text;using itextsharp.text.pdf;using itextsharp.tool.xml;using system;using System.collections.generic;using system.io;using system.linq;using system.text;using System.Web;///<summary> chinesefontfactory///</summary>public class chinesefontfactory:fontfactoryimp{    private static ReadOnly string sonefile = Path.Combine (Environment.getfolderpath (Environment.SpecialFolder.Fonts),    "SimSun." ttc,1 ");//Arial  here is the song body in the computer's Chinese package where public    override Font GetFont (string fontname, string encoding, bool embedded, float size, int style, Basecolor color,        bool cached)    {        //use Arial or superscript italic, choose a        basefont Basefont = Basefont.createfont (Sonefile, Basefont.identity_h, basefont.embedded);        return new Font (Basefont, size, style, color);}    }


Then encapsulate the conversion method

 <summary>///export HTML text to PDF file///</summary>//<param name= "HTMLText" ></param> <returns></returns> public byte[] Converthtmltexttopdf (string htmltext) {if (string.        IsNullOrEmpty (HTMLText)) {return null;        } MemoryStream outputstream = new MemoryStream ();//byte[] data = Encoding.UTF8.GetBytes (htmltext);//        MemoryStream msinput = new MemoryStream (data);        Document doc = new document ();//PDFWriter writer = pdfwriter.getinstance (doc, OutputStream); Pdfdestination pdfdest = new Pdfdestination (pdfdestination.xyz, 0, Doc.        Pagesize.height, 1f); Doc.        Open (); Xmlworkerhelper.getinstance ().        Parsexhtml (writer, doc, msinput, NULL, Encoding.utf8,new chinesefontfactory ());        Pdfaction action = pdfaction.gotolocalpage (1, pdfdest, writer); Writer.        Setopenaction (action); Doc.        Close ();        Msinput.close ();    Outputstream.close ();    return Outputstream.toarray (); }

Finally, the method triggers, for example, when the button is clicked, the PDF file is exported

protected void Btnpdf_click (object sender, EventArgs e)    {                       string fileName = DateTime.Now.ToString ();        System.Text.StringBuilder strb = new System.Text.StringBuilder ();        System.IO.StringWriter SW = new System.IO.StringWriter (STRB);        System.Web.UI.HtmlTextWriter HTW = new HtmlTextWriter (SW);        Pointcontent.rendercontrol (HTW);        String htmltext = Strb. ToString ();         Byte[] Pdffile = this. Converthtmltexttopdf (htmltext);        Response.ContentType = "Application/pdf";        response.contentencoding = System.Text.Encoding.UTF8;        Response.AddHeader ("Content-disposition", "attachment;filename=" + filename + ". pdf");        Response.Cache.SetCacheability (Httpcacheability.nocache);        Response.OutputStream.Write (pdffile, 0, pdffile.length);        Response.End ();    }

Finish the work, but in the process of using the reaction is a little slow. This is a bit of a little silent.



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.