我用PDFBox填写了一份PDF表格,在保存它之前我把它压平了。该窗体具有文本的自定义字体以及窗体字段。当我在没有安装此自定义字体的设备上打开输出文档(带有扁平字段)时,普通文本的字体仍然正确,但扁平字段的字体显示为回退(?)字体。在已经安装了这种自定义字体的设备上,一切看起来都像预期的那样。
是否有一种方法,强制使用相同的自定义字体后,对所有文本扁平窗体?
用于用PDFBox填写PDF表单的代码(简化):
public class App
{
public static void main(String[] args) throws IOException {
String formTemplate = "src/main/resources/fonts.pdf";
String filledForm = "src/main/resources/fonts_out.pdf";
PDDocument pdfDocument = PDDocument.load(new File(formTemplate));
PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
acroForm.getField("text").setValue("Same font in form text field (updated with PDFBox)");
acroForm.setNeedAppearances(true);
acroForm.refreshAppearances();
acroForm.flatten();
pdfDocument.save(filledForm);
pdfDocument.close();
}
}预期:

未在系统上安装字体时的结果:

发布于 2019-01-02 13:47:48
对你的PDF的一些观察(前面提到的编码问题没有-只是代表我的无知):
F2字体替换F5字体来解决的。NeedAppearances标志,这意味着表单字段没有出现。任何读者都必须(重新)创建这些。这不是由PDFBox自动完成的,所以我添加了这个部分。下面的代码将DroidSans字体嵌入到PDF中:
File pdf = new File("Fonts.pdf");
final PDDocument document = PDDocument.load(pdf);
FileInputStream fontFile = new FileInputStream(new File("DroidSans.ttf"));
PDFont font = PDType0Font.load(document, fontFile, false);
//1. embedd and register the font (Catalog dict)
PDAcroForm pDAcroForm = document.getDocumentCatalog().getAcroForm();
//create a new font resource
PDResources res = pDAcroForm.getDefaultResources();
if (res == null) res = new PDResources();
COSName fontName = res.add(font);
pDAcroForm.setDefaultResources(res);
//2. Now change the font of form field to the newly added font
PDField field = pDAcroForm.getField("text");
//field.setValue("Same font in form text field (updated with PDFBox)");
COSDictionary dict = field.getCOSObject();
COSString defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA);
if (defaultAppearance != null){
String currentValue = dict.getString(COSName.DA);
//replace the font - this should be improved with a more general version
dict.setString(COSName.DA,currentValue.replace("F2", fontName.getName()));
//remove F2 completely
COSDictionary resources = res.getCOSObject();
for(Entry<COSName, COSBase> resource : resources.entrySet()) {
if(resource.getKey().equals(COSName.FONT)) {
COSObject fonts = (COSObject)resource.getValue();
COSDictionary fontDict = (COSDictionary)fonts.getObject();
COSName toBeRemoved=null;
for(Entry<COSName, COSBase> item : fontDict.entrySet()) {
if(item.getKey().getName().equals("F2")) {
toBeRemoved = item.getKey();
}
}
if(toBeRemoved!=null) {
fontDict.removeItem(toBeRemoved);
}
}
}
if(pDAcroForm.getNeedAppearances()) {
pDAcroForm.refreshAppearances();
pDAcroForm.setNeedAppearances(false);
}
//Flatten the document
pDAcroForm.flatten();
//Save the document
document.save("Form-Test-Result.pdf");
document.close();请注意,上面的代码是非常静态的-搜索和替换名为F2 的字体只适用于提供的在其他情况下它不会。
发布于 2020-08-12 09:05:43
PDFont font = PDType0Font.load(document, fontFile, false);
我认为最后一个参数('false')嵌入了字体中的所有字符。当使用像日本字体这样的大字体时,会产生一个很大的pdf。所以,我尝试了下面的代码,它对我有效。
(* Scala,PDFBox 2.0.20)
// val font = PDType0Font.load(document, fontFile, true);
// form.flatten()
// hack for embed minimul font?
val page = new PDPage(PDRectangle.A6) // any page size.
val stream = new PDPageContentStream(document, page)
stream.beginText()
stream.setFont(font, 0)
stream.showText(allChars) // `allChars` are inputed all characters in forms in the creating pdf.
stream.endText()
stream.close()
// NOTE: I did NOT add the page to the PDF but worked.
// Save the document ~https://stackoverflow.com/questions/53968324
复制相似问题