7

When I try to write illegal characters to a PDF I obviously get an exception. E.g.

contentStream.showText("some illegal characters");    
...
java.lang.IllegalArgumentException: U+000A ('controlLF') is not available in this font Helvetica (generic: ArialMT) encoding: WinAnsiEncoding...

How can I find out which characters are not supported and strip them from the string?

ave4496
  • 2,950
  • 3
  • 21
  • 48
  • Usually you can count on a font not to contain glyphs for characters in the control character range < U+0020. Which characters exactly are available depends on the font in question and the declared encoding. – mkl Feb 14 '17 at 15:19

1 Answers1

9

Here is my solution... at least it works for what I need. I used the WinAnsiEncoding class of PDFBox and called the contains method to check if the character is supported.

import org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding;

public class Test extends WinAnsiEncoding {

    public static String remove(String test) {
        StringBuilder b = new StringBuilder();
        for (int i = 0; i < test.length(); i++) {
            if (WinAnsiEncoding.INSTANCE.contains(test.charAt(i))) {
                b.append(test.charAt(i));
            }
        }
        return b.toString();
    }

    public static void main(String[] args) {
        System.out.println(remove("abc\rcde"));
        // prints abccde
    }

}
ave4496
  • 2,950
  • 3
  • 21
  • 48
  • How can I add a new line between the strings that going to be written on PDF ? both \n and System.getProperty("line.separator") not working ... Please help – Ever Think Oct 12 '17 at 12:25
  • 1
    You have to split the string at the new line characters and manually write line by line. There is no other solution. – ave4496 Oct 12 '17 at 12:54
  • 1
    https://stackoverflow.com/questions/19635275/how-to-generate-multiple-lines-in-pdf-using-apache-pdfbox – ave4496 Oct 12 '17 at 14:55
  • How can I utilize the same code for a custom font loaded using (for example) `PDType0Font font = PDType0Font.load(document, new File("Amiri-Regular.ttf"));` ? – h q Apr 23 '20 at 18:32
  • Still getting the error: No glyph for U+0009 in font FreeSans – Chilly Code Jun 15 '23 at 22:20