ItextSharp不显示日语字体

时间:2012-06-25 10:17:52

标签: c# itextsharp

iTextSharp不显示日文字体。我找到了一个solution但是当我编译时,我收到一个错误说:

  

无法识别带有'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);

1 个答案:

答案 0 :(得分:4)

创建PdfStamper后设置SubstitutionFont:

stamper.AcroFields.AddSubstitutionFont(myFont.BaseFont);