3

iTextSharp 不显示日文字体。我找到了一个解决方案,但是当我编译时,我收到一条错误消息:

无法识别带有“UniGB-UCS2-H”的字体“STSong-Light”。

这是我的源代码:

BaseFont.AddToResourceSearch(serverPath + "\\lib\\iTextAsian.dll");
BaseFont.AddToResourceSearch(serverPath + "\\lib\\iTextAsianCmaps.dll");
BaseFont font = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);    

using (Document document = new Document())
{
    using (PdfSmartCopy copy = new PdfSmartCopy(
        document, new FileStream(directoryOutPdf + nameOutPdf, FileMode.Create))
    )
    {
        document.Open();

        // generate one page per statement        
        for (int i = 0; i < countBlank.Count; i++)
        {                        
            // replace this with your PDF form template          
            PdfReader reader = new PdfReader(pdfTemplatePath + @"\EmptyTemplateBankBlank_2012.pdf");
            using (var ms = new MemoryStream())
            {
                using (PdfStamper stamper = new PdfStamper(reader, ms))
                {
                    AcroFields form = stamper.AcroFields;
                    form.SetFieldProperty("Info", "textfont", font, null);
                    form.SetField("Info", "_源泉徴収票");                                                                
                    stamper.FormFlattening = true;
                }
                reader = new PdfReader(ms.ToArray());

                copy.AddPage(copy.GetImportedPage(reader, 1));
            }
        }
    }
}

我认为这是因为我没有安装STSong-Light字体。不幸的是我找不到STSong-Light,因此不得不设置Stsong字体,但它仍然不起作用。

String fontPath = Path.Combine(serverPath + "\\Fonts", "STSONG.ttf");

BaseFont baseFont = BaseFont.CreateFont(fontPath, "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont, 12, Font.NORMAL);
4

1 回答 1

4

创建 PdfStamper 后设置 SubstitutionFont:

stamper.AcroFields.AddSubstitutionFont(myFont.BaseFont);
于 2012-06-26T07:55:49.927 回答