uksz
uksz

Reputation: 18699

How to resolve "...is not available in this font's encoding"?

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

with different numbers.

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

UPDATE

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

Upvotes: 4

Views: 15328

Answers (1)

Issam El-atif
Issam El-atif

Reputation: 2486

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

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

Update

Do you know why it also displays a warning if form of: OpenType Layout tables used in font Avenir-Light are not implemented in PDFBox and will be ignored?

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

Upvotes: 4

Related Questions