HTML到PDF的一些字符丢失(iTextSharp的)字符、HTML、PDF、iTextSharp

2023-09-02 21:37:24 作者:说爱太烫嘴

我想通过使用iTextSharp的库的GridView导出为PDF格式。问题是,一些土耳其的字符,如I,I,S,S等...缺少PDF文件研究。用于导出的PDF code是:

 保护无效LinkBut​​tonPdf_Click(对象发件人,EventArgs的)
    {
        Response.ContentType =应用程序/ PDF格式;
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader(内容处置,附件;文件名= FileName.pdf);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        System.IO.StringWriter stringWrite =新的StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite =新的HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        StringReader读卡器=新StringReader(textConvert(stringWrite.ToString()));
        文档DOC =新的文件(PageSize.A4);
        HTMLWorker分析器=新HTMLWorker(DOC);
        PdfWriter.GetInstance(DOC,Response.OutputStream);
        doc.Open();
        parser.Parse(读卡器);
        doc.Close();
    }
    公共静态字符串textConvert(串S)
    {
        如果(S == NULL){返回NULL; }
        尝试
        {
            System.Text.Encoding encFrom = System.Text.Encoding.UTF8;
            System.Text.Encoding encTo = System.Text.Encoding.UTF8;
            字符串str = S;
            字节[] B = encFrom.GetBytes(STR);
            返回encTo.GetString(B);
        }
        抓{返回null; }
    }
 

注:当我想插入字符转换成PDF文件,缺少的字符显示在里面。我插入字符与此code:

  BASEFONT bffont = BaseFont.CreateFont(C:\ WINDOWS \ \字体arial.ttf,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        字体fontozel =新的字体(bffont,12,Font.NORMAL,新的颜色(0,0,0));
        doc.Add(新段(İİııŞŞşşĞĞğğ,fontozel));
 

解决方案

Finaly我想我找到了解决办法,我改变iTextSharp的源$ C ​​$ ca的一点,以显示土耳其字符。(土耳其字符code是cp1254 )

  

我添加公共常量字符串CP1254 =Cp1254。为[BaseFont.cs]源$ C ​​$ C

     怎么改变PDF的整体文字颜色加深

在我修改[FactoryProperties.cs]。我改变了这样的;

 公共字体GETFONT(ChainedProperties道具)
{
我不写全codeI改变只有code以下;
------------默认iTextSharp的code -------------------------------- ----------------------
  如果(编码== NULL)
                编码= BaseFont.WINANSI;
            返回fontImp.GetFont(脸,编码,真实,大小,样式,颜色);
-------------修改code -------------------------------- ------------

            编码= BaseFont.CP1254;
            返回fontImp.GetFont(C:\ WINDOWS \ \字体arial.ttf,编码,真实,大小,样式,颜色);
}
 

。经过我编译新的DLL和缺少的字符显示。

I want to export gridview to pdf by using the itextsharp library. The problem is that some turkish characters such as İ,ı,Ş,ş etc... are missing in the pdf document. The code used to export the pdf is:

 protected void LinkButtonPdf_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        System.IO.StringWriter stringWrite = new StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        StringReader reader = new StringReader(textConvert(stringWrite.ToString()));
        Document doc = new Document(PageSize.A4);
        HTMLWorker parser = new HTMLWorker(doc);
        PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();
        parser.Parse(reader);
        doc.Close();
    }
    public static string textConvert(string S)
    {
        if (S == null) { return null; }
        try
        {
            System.Text.Encoding encFrom = System.Text.Encoding.UTF8;
            System.Text.Encoding encTo = System.Text.Encoding.UTF8;
            string str = S;
            Byte[] b = encFrom.GetBytes(str);
            return encTo.GetString(b);
        }
        catch { return null; }
    }

Note: when I want to insert characters into the pdf document, the missing characters are shown in it. I insert the characters with this code:

   BaseFont bffont = BaseFont.CreateFont("C:\WINDOWS\Fonts\arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font fontozel = new Font(bffont, 12, Font.NORMAL, new Color(0, 0, 0));
        doc.Add(new Paragraph("İİııŞŞşşĞĞğğ", fontozel));

解决方案

Finaly I think I found the solution,I changed itextsharp source code a little in order to show turkish characters.(turkish character code is cp1254)

I add "public const string CP1254 = "Cp1254";" to [BaseFont.cs] in the source code.

After that I modify the [FactoryProperties.cs].I changed like this;

public Font GetFont(ChainedProperties props)
{
I don't write the whole code.I changed only code below;
------------Default itextsharp code------------------------------------------------------
  if (encoding == null)
                encoding = BaseFont.WINANSI;
            return fontImp.GetFont(face, encoding, true, size, style, color);
-------------modified code--------------------------------------------

            encoding = BaseFont.CP1254;
            return fontImp.GetFont("C:\WINDOWS\Fonts\arial.ttf", encoding, true, size, style, color);
}

.After I compile new dll ,and missing characters are shown.