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 に答える