使用PDFBox将西里尔字符写入PDF表单域 [英] Write cyrillic chars into PDF form fields with PDFBox

查看:798
本文介绍了使用PDFBox将西里尔字符写入PDF表单域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用pdfbox 2.0.5来填写PDF文档的表单域,使用下面的代码:
$ b $ pre $ doc $ PDDocument .load(的inputStream);
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDAcroForm form = catalog.getAcroForm(); (PDF字段:form.getFieldTree()){
field.setValue(должен);

}

我得到这个错误: U + 0434('afii10069')在这种字体中不能使用Times-Roman(generic:TimesNewRomanPSMT)编码:StandardEncoding带有差异

PDF文档本身包含西里尔文本,显示正常。我曾尝试使用不同的字体。对于Arial Unicode MS,它希望下载一个50MB的Adobe Acrobat Reader DC字体包。这是一个西里尔文字符的要求吗?

我需要在文本字段中指定哪一种字体来处理西里尔字符(或亚洲字符)?

感谢,
Ropo

解决方案

{/ Ubuntu}字体,并从中创建一个新的字体资源。这里是一个快速入门,可以作为如何实现类似的东西的指导。代码是特定于我有一个示例。
$ b $ pre $ PDDocument doc = PDDocument.load(新文件(...) );
PDAcroForm acroForm = doc.getDocumentCatalog()。getAcroForm();
PDResources formResources = acroForm.getDefaultResources();
PDTrueTypeFont font =(PDTrueTypeFont)formResources.getFont(COSName.getPDFName(Ubuntu));

//这里是重复使用字体作为新的字体资源的'魔术'
TrueTypeFont ttFont = font.getTrueTypeFont();

PDFont font2 = PDType0Font.load(doc,ttFont,true);
ttFont.close();

formResources.put(COSName.getPDFName(F0),font2);

PDTextField formField =(PDTextField)acroForm.getField(Text2);
formField.setDefaultAppearance(/ F0 0 Tf 0 g);
formField.setValue(öäüинформацию);

doc.save(...);
doc.close();


I am using pdfbox 2.0.5 to fill out form fields of a PDF document using this code:

        doc = PDDocument.load(inputStream);
        PDDocumentCatalog catalog = doc.getDocumentCatalog();
        PDAcroForm form = catalog.getAcroForm();
        for (PDField field : form.getFieldTree()){
            field.setValue("должен");
        }

I get this error: U+0434 ('afii10069') is not available in this font Times-Roman (generic: TimesNewRomanPSMT) encoding: StandardEncoding with differences

The PDF document itself contains cyrillic text which is displayed fine. I have tried using different fonts. For "Arial Unicode MS" it wants to download a 50MB "Adobe Acrobat Reader DC Font Pack". Is this a requirement for cyrillic characters?

Which font do I have to specify in the text field to handle cyrillic (or asian) characters?

Thanks, Ropo

解决方案

Adobe handles that by reusing the embedded font file in the {/Ubuntu} font and creates a new font resource from that. Here is a quick hack which can serve as a guide of how to achieve something similar. The code is specific to a sample I've got.

PDDocument doc = PDDocument.load(new File(...));
PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();
PDResources formResources = acroForm.getDefaultResources();
PDTrueTypeFont font = (PDTrueTypeFont) formResources.getFont(COSName.getPDFName("Ubuntu"));

// here is the 'magic' to reuse the font as a new font resource
TrueTypeFont ttFont = font.getTrueTypeFont();

PDFont font2 = PDType0Font.load(doc, ttFont, true);
ttFont.close();

formResources.put(COSName.getPDFName("F0"), font2);

PDTextField formField = (PDTextField) acroForm.getField("Text2");
formField.setDefaultAppearance("/F0 0 Tf 0 g");
formField.setValue("öäüинформацию");

doc.save(...);
doc.close();

这篇关于使用PDFBox将西里尔字符写入PDF表单域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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