本文介绍了如何解决“...在此字体的编码中不可用"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用 PDFBox 来填写一些 pdf.到目前为止一切都很好 - 我用 Avenir Light 字体在 pdf 中创建了一个表单,我可以填写它.但是,刚刚出现的问题是,当我尝试使用诸如 ł, ą, ć ... 我收到以下错误:

So I am using PDFBox to fill in some pdfs. So far everything was great - I created a form in pdf with Avenir Light font, and I could fill it in. However, the problem that just now showed up, is that when I am trying to fill the pdf using letters such as ł, ą, ć ... I get the following error:

U+0142 is not available in this font's encoding: MacRomanEncoding with differences

不同的数字.

现在,我的问题是 - 我该如何解决这个问题,以便我可以自动填写表单?当我在 Acrobat Reader 中打开 pdf 时,我可以插入这些字母,并且没有出现任何错误.这是我设置字段的方式:

Now, my question is - how can I fix this, so that I can fill the form automatically? When I open the pdf in Acrobat Reader, I can insert those letters, and I dont get any errors. Here is how I set the field:

public void setField(PDDocument document, PDField field, String value ) throws IOException {
    if( field != null && value != null) {
        try{
            field.setValue(value);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    else {
        System.err.println( "No field found with name:" + field.getPartialName() );
    }
}

更新

我一直在尝试像这样上传我自己的 Avenir-Light.tff:

I've been trying to upload my own Avenir-Light.tff like this:

PDFont font = PDType1Font.HELVETICA;
PDResources res = new PDResources();
COSName fontName = res.add(font);
acroForm.setDefaultResources(res);
String da = "/" + fontName.getName() + " 12 Tf 0 g";
acroForm.setDefaultAppearance(da);

然而,这似乎对打印的字段没有任何影响,并且抛出几乎相同的消息:

However, this doesn't seem to have any impact on the printed fields, and throws almost the same message:

U+0104 ('Aogonek') is not available in this font Helvetica (generic: ArialMT) encoding: WinAnsiEncoding

推荐答案

PDFBox在PDType1Font中定义了14种标准字体:

PDFBox define 14 standard fonts in PDType1Font :

PDType1Font.TIMES_ROMAN
PDType1Font.TIMES_BOLD
PDType1Font.TIMES_ITALI
PDType1Font.TIMES_BOLD_ITALIC
PDType1Font.HELVETICA
PDType1Font.HELVETICA_BOLD
PDType1Font.HELVETICA_OBLIQUE
PDType1Font.HELVETICA_BOLD_OBLIQUE
PDType1Font.COURIER
PDType1Font.COURIER_BOLD
PDType1Font.COURIER_OBLIQUE
PDType1Font.COURIER_BOLD_OBLIQUE
PDType1Font.SYMBOL
PDType1Font.ZAPF_DINGBATS

因此,如果您想使用 Avenir-Light,则必须从 .ttf 文件中加载它.您可以按照@TilmanHausherr 的建议进行操作 PDType0Font.load(doc, new File("path/Avenir-Light.ttf"), false).

So if you want to use Avenir-Light you have to load it from a .ttf file. You can do this as @TilmanHausherr suggested PDType0Font.load(doc, new File("path/Avenir-Light.ttf"), false).

PDFont font = PDType0Font.load(doc, new File("path/Avenir-Light.ttf"), false);
PDResources res = new PDResources();
COSName fontName = res.add(font);
acroForm.setDefaultResources(res);
String da = "/" + fontName.getName() + " 12 Tf 0 g";
acroForm.setDefaultAppearance(da);

更新

你知道为什么它还会显示警告 if form of: OpenType Layout字体 Avenir-Light 中使用的表格未在 PDFBox 中实现,并且会被忽略吗?

Avenir-light 字体使用 PDFBox 尚不支持的 OpenType 布局表(高级版式).这个高级版式将被忽略

Avenir-light font uses OpenType Layout tables (Advanced Typographic) that PDFBox does not support yet. This advaned typographics will be ignored

这篇关于如何解决“...在此字体的编码中不可用"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 18:58