使用iTextSharp不显示中文字符

问题描述 投票:0回答:2

我一直在使用itextsharp从我的数据库数据生成pdf。我的数据库数据包含文本,x和y坐标以及页码。这是我如何做到的。

 string oldFile = Server.MapPath(string.Format("~/App_Data/Documents/Templates/{0}", documentModel.Filename));

        // open the reader
        PdfReader reader = new PdfReader(oldFile);
        Rectangle size = reader.GetPageSizeWithRotation(1);
        iTextSharp.text.Document document = new iTextSharp.text.Document(size);

        PdfWriter writer = PdfWriter.GetInstance(document, _memoryStream);
        writer.CloseStream = false;
        document.Open();

        PdfContentByte cb;
        PdfImportedPage page;
        BaseFont bf;


foreach (var pageNumber in pages)
        {
            document.NewPage();

            cb = writer.DirectContent;
            // create the new page and add it to the pdf
            page = writer.GetImportedPage(reader, pageNumber);
            cb.AddTemplate(page, 0, 0);
            bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb.SetColorFill(BaseColor.DARK_GRAY);
            cb.SetFontAndSize(bf, 9);

            var items = documentModel.FieldContents.Where(a => a.Page == pageNumber).ToList();

            foreach (var item in items)
            {
                cb.BeginText();
                cb.ShowTextAligned(item.Alignment, item.Text ?? string.Empty, item.X, item.Y, item.Rotation);
                cb.EndText();
            }

        }

        ////////////////////////////////////////////////////////////////////

        document.Close();
        writer.Close();
        reader.Close();

        byte[] byteInfo = _memoryStream.ToArray();
        _memoryStream.Write(byteInfo, 0, byteInfo.Length);
        _memoryStream.Position = 0;

        RedirectToAction("Index");
        return new FileStreamResult(_memoryStream, "application/pdf");

我已经使用了一段时间了。但我也想支持中文字符。我尝试输入中文字符,但没有显示。我把它改回英文字符并且有效。有任何想法吗?谢谢!

c# fonts itextsharp
2个回答
2
投票

您的代码中存在不同的错误。

正如mkl已经解释的那样,你使用的Type 1 Helvetica字体没有嵌入,也不了解中国字形。您需要使用知道如何绘制中文字符的字体。 CP1252 is the encoding for the Latin alphabet,所以你不应该指望它对中文有所了解。

您还创建了非法的代码(导致无效的PDF语法):

cb.BeginText();
cb.ShowTextAligned(...);
cb.EndText();

这是一个没有定义字体和大小的文本对象。您在图形状态(不属于图形)中使用'setFontAndSize()'方法而不是文本状态(需要它)。这段代码运行的事实告诉我你正在使用一个旧的iTextSharp版本。 You should upgrade.

您的问题的解决方案是找到有关添加Chinese text with iText的示例。然后创建一个'Phrase'并使用ColumnText.showTextAligned();定位它。通过使用ColumnText,您可以避免使用低级方法遇到的问题:您在不阅读PDF规范的情况下使用beginText()endText()ColumnText将在引擎盖下完成所有这些艰难的工作。

如果Java示例让您感到困惑,请访问this page,在那里您可以找到相应示例的C#端口的链接。


1
投票

您可以加载这样的本地中文字体

BaseFont baseFont = BaseFont.CreateFont(
                "C:\\Windows\\Fonts\\simhei.ttf",
                BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED); 
Font font = new Font(baseFont);

然后使用“字体”进一步发短信。 您可以参考下面常用的中文本地字体

            {"yahei", "C:\\WINDOWS\\FONTS\\msyh.ttf"},
            {"fangsong", "C:\\WINDOWS\\FONTS\\simfang.ttf"},
            {"heiti" ,"C:\\Windows\\Fonts\\simhei.ttf"},
            {"kaiti" ,"C:\\Windows\\Fonts\\simkai.ttf"},
            {"lishu" ,"C:\\Windows\\Fonts\\SIMLI.TTF"},
            {"youyuan" ,"C:\\Windows\\Fonts\\SIMYOU.TTF"},
            {"songti" ,"C:\\Windows\\Fonts\\simsun.ttc,0"},
            {"xinsongti" ,"C:\\Windows\\Fonts\\simsun.ttc,1"}

希望它能帮到你!

© www.soinside.com 2019 - 2024. All rights reserved.