iTextSharp的不显示日文字体 [英] ItextSharp doesn't display Japanese font

查看:415
本文介绍了iTextSharp的不显示日文字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iTextSharp的不显示日语字体。我发现的解决方案但是当我编译,我得到一个错误说:


字体'STSong-光 。UCS2-H'无法识别




下面是我的源代码:

  BaseFont.AddToResourceSearch(SERVERPATH +\\lib\\iTextAsian.dll); 
BaseFont.AddToResourceSearch(SERVERPATH +\\lib\\iTextAsianCmaps.dll);
BASEFONT字体= BaseFont.CreateFont(STSong-光,UniGB-UCS2-H,BaseFont.EMBEDDED);使用(文档的文档=新的文件())
{
使用(PdfSmartCopy副本=新PdfSmartCopy(
文件,新的FileStream(directoryOutPdf + nameOutPdf,的FileMode

。创建))

{
document.Open();

//生成每条语句
一页的for(int i = 0; I< countBlank.Count;我++)
{
//替换为您的这PDF表单模板
PdfReader读卡器=新PdfReader(pdfTemplatePath + @\EmptyTemplateBankBlank_2012.pdf);
使用(VAR毫秒=新的MemoryStream())
{使用(PdfStamper压模=新PdfStamper(读卡器,MS))
{
AcroFields形成=模子
。 AcroFields;
form.SetFieldProperty(信息,TEXTFONT,字体,NULL);
form.SetField(信息,_源泉徴收票);
stamper.FormFlattening = TRUE;
}
读者=新PdfReader(ms.ToArray());

copy.AddPage(copy.GetImportedPage(阅读器,1));
}
}
}
}



我认为这是因为我没有安装 STSong-灯字体。不幸的是我无法找到 STSong-灯,因此,必须设置 Stsong 字体,但它仍然无法正常工作

 字符串的FontPath = Path.Combine(SERVERPATH +\\Fonts,STSONG.ttf); 

BASEFONT BASEFONT = BaseFont.CreateFont(字体路径,UniGB-UCS2-H,BaseFont.NOT_EMBEDDED);
字体的字体=新字体(BASEFONT,12,Font.NORMAL);


解决方案

创建PdfStamper后设置SubstitutionFont >

  stamper.AcroFields.AddSubstitutionFont(myFont.BaseFont); 


iTextSharp doesn't display Japanese font. I found a solution but when I compile, I get an error saying:

Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.

Here's my source code:

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

I think it's because I didn't install the STSong-Light font. Unfortunately I could not find STSong-Light and, therefore, had to set Stsong font, but it still does not work.

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

解决方案

Set the SubstitutionFont after creating the PdfStamper:

stamper.AcroFields.AddSubstitutionFont(myFont.BaseFont);

这篇关于iTextSharp的不显示日文字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆