1
votes

So, in this application we're using iText to fill out PDF forms and PDFBox to load that filled out PDF and convert to image into our system.

The problem is when the image is converted. All the information is there, but the checkboxes are... weird? Instead of the styled checkbox "check mark" that is set on the PDF, the checkboxes get a weird "empty box" inside of them.

enter image description here

How it is supposed to be:

enter image description here

PDFBox version 2.0.11 iText version is 5.5.13

Here is a little snippet of the code where the conversion happens:

PDDocument pdf = PDDocument.load(byteArrayInputStream);
PDFRenderer renderer = new PDFRenderer(pdf);
BufferedImage[] images = new BufferedImage[pdf.getNumberOfPages()];
PDPage page = null;
BufferedImage image = null;
for (int i = 0; i < images.length; i++) {
        try {
            image = renderer.renderImageWithDPI(i, 300,org.apache.pdfbox.rendering.ImageType.RGB);
            ...

I'm kind of sensing a "loss of quality" too after the conversion. Before, we were using PDFBox 1.8 and the conversion quality was low and it was losing some font formatting and style. Since the upgrade it got better, but is still bugged.

Where the filling happens:

PdfReader reader = new PdfReader(filePath);

ByteArrayOutputStream lStr = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, lStr);
AcroFields acroFields = stamper.getAcroFields();

for (Entry<String, Item> map : acroFields.getFields().entrySet()) {
    String key = map.getKey();

    if (!fields.has(key))
        continue;

    if (fields.isNull(key))
        continue;

    acroFields.setField(key, fields.getString(key), true);
}
stamper.setFormFlattening(true);

stamper.close();
reader.close();

...

Do you guys know what this is?

Thanks!

1
Make sure the zapf dingbats or Microsoft Gothic font is installed. See also the log messages. And share the PDF if it still doesn't work. - Tilman Hausherr
In the "How it is supposed to be:" image, are the different background colors of "Alarme" and "Bancos de Cuoro" a different design or merely due to one of them having the focus? - mkl
For PDFBox, just copy the missing font (if it is missing) the way your OS requires it. In windows, copy the zapf dingbats .pfb file in your \windows\fonts directory (or the msgothic.tt* file). There is no need to change your PDF file. - Tilman Hausherr
segment from our code: protected String[] getSearchableDirectories() { return new String[] { System.getProperty("user.home") + "/.fonts", // user "/usr/local/fonts", // local "/usr/local/share/fonts", // local shared "/usr/share/fonts", // system "/usr/X11R6/lib/X11/fonts" // X }; } - Tilman Hausherr
So the easiest to test is to create a .fonts directory in your user directory and copy the font there. - Tilman Hausherr

1 Answers

3
votes

Got it working thanks to Tilman Hausherr's suggestion. The problem was indeed the fonts missing in the server running the application. (Zapf Dingbats and/or MS Gothic).

Installing the missing fonts in a directory "./fonts" or "/usr/share/fonts" (Linux) / "/Windows/Fonts" (Windows) did the trick!