c# - Html 到 pdf 缺少一些字符 (itextsharp)

标签 c# .net asp.net pdf-generation itextsharp

我想使用 itextsharp 库将 gridview 导出为 pdf。问题是 pdf 文档中缺少一些土耳其语字符,例如 İ、ı、Ş、ş 等。用于导出pdf的代码是:

 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; }
    }

注意:当我想在pdf文档中插入字符时,缺少的字符会显示在其中。我用这段代码插入字符:

   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));

最佳答案

最后我想我找到了解决方案,我稍微更改了 itextsharp 源代码以显示土耳其字符。(土耳其字符代码是 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);
}

.编译新的dll后,显示缺少的字符。

关于c# - Html 到 pdf 缺少一些字符 (itextsharp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1322303/

相关文章:

asp.net - 系统内存不足异常 : Exception when uploading a file

c# - List.Find<T>() 即使谓词匹配也返回 null

c# - 带有单词边界的正则表达式在 .NET Regex 中失败

c# - ASP.NET C# 将方法移动到类文件并从代码隐藏调用它

c# - 在 WCF MessageEncoder 中获取 HTTP header

.net - C# 2.0 : Can MethodBase. GetCurrentMethod() 返回 null?

asp.net - 如何在网络表单中包含部分 View

javascript - 如何缩小 KendoUI 饼图的大小?

c# - 自动化任务的所有可能选项(在我的例子中,是搜索索引更新)

c# - 正则表达式 : extract all words out of quotes