0
votes

Hello fellow developers,

I have a problem when printing PDFs that I've automatically generated with a Java application using iText 7. When I print such a PDF, the printout contains all pictures and graphics, but no text whatsoever.

Can someone tell me what the problem could possibly be? I've tried the "print as image" option in Adobe and came up with the same result.

Thank you very much.

EDIT/Added code and link:

Link to PDF file created this way

document = new Document(new PdfDocument(new PdfWriter(new FileOutputStream(dest))));        
this.form = PdfAcroForm.getAcroForm(document.getPdfDocument(), true);
PdfTextFormField fw1Field = PdfTextFormField.createText(document.getPdfDocument(),
                        new Rectangle(Variables.llx, Variables.lly, Variables.urx, Variables.ury), "Feld1");
fw1Field.setValue(fw1);
fw1Field.setReadOnly(Variables.readonly);
fw1Field.setBorderColor(Color.WHITE);
form.addField(fw1Field);

PdfTextFormField fsText = PdfTextFormField.createText(document.getPdfDocument(),
                        new Rectangle(Variables.llx + 150, Variables.lly, Variables.urx  + 50, Variables.ury), "FSText");
fsText.setValue("Freigabeschein:");
fsText.setBackgroundColor(Variables.backgroundColourText);
fsText.setBorderColor(Color.WHITE);
fsText.setReadOnly(Variables.readonlyText);
fsText.setBorderColor(Color.WHITE);
form.addField(fsText);

PdfTextFormField idField = PdfTextFormField.createText(document.getPdfDocument(),
                        new Rectangle(Variables.llx + 250, Variables.lly, Variables.urx, Variables.ury), "Freigabeschein Nummer");
idField.setValue(id);
idField.setReadOnly(Variables.readonly);
idField.setBorderColor(Color.WHITE);
form.addField(idField);

PdfTextFormField datumText = PdfTextFormField.createText(document.getPdfDocument(),
new Rectangle(Variables.llx + 350, Variables.lly, Variables.urx, Variables.ury), "DatumText");
datumText.setValue("Datum:");
datumText.setBackgroundColor(Variables.backgroundColourText);
datumText.setBorderColor(Color.WHITE);
datumText.setReadOnly(Variables.readonlyText);
form.addField(datumText);

//more Text, created exactly as above

PdfButtonFormField buttonSpeichern = PdfFormField.createPushButton(document.getPdfDocument(), new Rectangle(450, 20, 100, 30), "speichern", "SPEICHERN");
buttonSpeichern.setBackgroundColor(Color.LIGHT_GRAY);
buttonSpeichern.setValue("Speichern");
buttonSpeichern.setVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);
buttonSpeichern.setAdditionalAction(PdfName.D, PdfAction.createJavaScript("saveFSFunction(1);"));
form.addField(buttonSpeichern);

PdfButtonFormField buttonDrucken = PdfFormField.createPushButton(document.getPdfDocument(), new Rectangle(300, 20, 100, 30), "drucken", "DRUCKEN");
buttonDrucken.setBackgroundColor(Color.LIGHT_GRAY);
buttonDrucken.setValue("Drucken");                                             
buttonDrucken.setVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);
buttonDrucken.setAdditionalAction(PdfName.D, PdfAction.createJavaScript("printFunction(1, 0, 1, \"FS\");"));
form.addField(buttonDrucken);

document.close();
1
Please share A) the pivotal part of the code used to create the PDF B) a sample PDF that reproduces the issue.Amedee Van Gasse
Thank you for your reply, I added the code snippet a link to a sample PDF with the issue.Harry
Did you check whether you set Acrobat it to print the formfields? Either that, or your textfields all have their visibility set to visible, but not print. Although, I don't think that's the default setting when creating the text fields with iText, and you don't seem to set in the code you shared here...Samuel Huylebroeck
@SamuelHuylebroeck, thank you so much. I had to set all the fields .setVisibility(0); There was no enum for visible, so I assumed that's the standard behaviour - obviously not.Harry
@Harry There will be one in the next release. I remember encountering a similar question on slackoverflow some time back and adding it an enum for it. Fyi there's nothing special about the number 0 here, the other visibility options are mapped to 1, 2 and 3, and the default switch statement in the method is set to the visible and print setting.Samuel Huylebroeck

1 Answers

0
votes

A brief explanation of the problem and its solution using iText7:

Visibility of form field widget (i.e. the representation you'll see in a viewer or in print) is governed by a by an unsigned 32-bit integer that is stored in the /F entry of the widgets dictionary. This entry is optional, and the default value is 0. The integer is interpreted as a series of flags, with meaning based on their position, goiçng from low to high order. The flag at position 3 governs the printing behaviour of the widget. If it is set to 0, the widget will never be printed.

In iText7, when creating a textField annotation, the /F entry is not automatically added to the widget dictionary, unless a value is set explicitly using PdfTextFormField.setVisibility(), so the default behaviour here would roughly correspond with VISIBLE_BUT_DO_NOT_PRINT.

In 7.0.2 and earlier, there is no keyword for both visible and print, but the behaviour is implemented as the default case for PdfTextFormField.setVisibility(), calling the method with any number but 1,2 or 3, will result in this behaviour. (1, 2 and 3 are mapped to HIDDEN, VISIBLE_BUT_DO_NOT_PRINT and HIDDEN_BUT_PRINTABLE) respectively.

In 7.0.3 (still in development at the time of writing) and onwards, the keyword VISIBLE has been added for convenience's sake, and to avoid confusion.