4

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
uksz
  • 18,239
  • 30
  • 94
  • 161
  • How do you load Avenir Light font in your code? – Issam El-atif Oct 14 '16 at 14:44
  • 1
    @IssamELATIF, I don't load it in my code - the font is embed into the pdf. – uksz Oct 14 '16 at 14:46
  • Share some code about how you fill PDField or Cells it will help – Issam El-atif Oct 14 '16 at 14:58
  • @IssamELATIF, I updated my question – uksz Oct 14 '16 at 15:09
  • this may help http://stackoverflow.com/questions/22073137/how-to-set-the-text-of-a-pdtextbox-to-a-color/22082301#22082301 – Issam El-atif Oct 14 '16 at 15:38
  • 1
    I'm not sure if this works in a form (I already wrote an answer and deleted it for now) - please try `PDType0Font load(doc, new File("path/Avenir-Light.ttf"), false)` and use that one in your resources. – Tilman Hausherr Oct 14 '16 at 18:55
  • @TilmanHausherr, amazing, this works just fine. Could you explain in your answer what is this doing, so that I understand my mistake? – uksz Oct 15 '16 at 08:40
  • @TilmanHausherr, 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. – uksz Oct 15 '16 at 08:41
  • Your mistake is that WinAnsiEncoding or MacRomanEncoding (see Appendix D of the PDF specification) allows only a few characters. Re the message: don't bother. It means that this font has some improved capabilities, but that PDFBox doesn't use them. I don't know why this is reported as an error. – Tilman Hausherr Oct 15 '16 at 12:55
  • @TilmanHausherr, do you know if I can disable this error somewhere? It makes loggs very hard to read ;) – uksz Oct 15 '16 at 12:59
  • You could disable it with log4j but this would disable all messages from that class. https://stackoverflow.com/questions/4972954/how-to-disable-loggers-of-a-class-or-of-whole-package – Tilman Hausherr Oct 15 '16 at 13:04

1 Answers1

4

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

Issam El-atif
  • 2,366
  • 2
  • 17
  • 22
  • 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? – uksz Oct 15 '16 at 12:01
  • 1
    Yes, disable logging for this class org.apache.pdfbox.pdmodel.font. PDCIDFontType2 in log4j. Like this log4j.logger.org.apache.pdfbox.pdmodel.font. PDCIDFontType2=OFF – Issam El-atif Oct 15 '16 at 13:47