Skip to content

Commit c1093d1

Browse files
committed
Create empty PdfArray for fields in acroform when fields entry doesn't exist, warn with logging message
DEVSIX-1405
1 parent 106fb16 commit c1093d1

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

forms/src/main/java/com/itextpdf/forms/PdfAcroForm.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,13 @@ public void replaceField(String name, PdfFormField field) {
819819
* @return a {@link PdfArray} of field dictionaries
820820
*/
821821
protected PdfArray getFields() {
822-
return getPdfObject().getAsArray(PdfName.Fields);
822+
PdfArray fields = getPdfObject().getAsArray(PdfName.Fields);
823+
if (fields == null) {
824+
logger.warn(LogMessageConstant.NO_FIELDS_IN_ACROFORM);
825+
fields = new PdfArray();
826+
getPdfObject().put(PdfName.Fields, fields);
827+
}
828+
return fields;
823829
}
824830

825831
@Override

forms/src/test/java/com/itextpdf/forms/PdfFormFieldTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This file is part of the iText (R) project.
4646
import com.itextpdf.forms.fields.PdfChoiceFormField;
4747
import com.itextpdf.forms.fields.PdfFormField;
4848
import com.itextpdf.forms.fields.PdfTextFormField;
49+
import com.itextpdf.io.LogMessageConstant;
4950
import com.itextpdf.io.source.ByteArrayOutputStream;
5051
import com.itextpdf.kernel.geom.Rectangle;
5152
import com.itextpdf.kernel.pdf.PdfDocument;
@@ -54,6 +55,8 @@ This file is part of the iText (R) project.
5455
import com.itextpdf.kernel.pdf.PdfWriter;
5556
import com.itextpdf.kernel.utils.CompareTool;
5657
import com.itextpdf.test.ExtendedITextTest;
58+
import com.itextpdf.test.annotations.LogMessage;
59+
import com.itextpdf.test.annotations.LogMessages;
5760
import com.itextpdf.test.annotations.type.IntegrationTest;
5861
import org.junit.Assert;
5962
import org.junit.BeforeClass;
@@ -339,4 +342,24 @@ public void autoScaleFontSizeInFormFields() throws IOException, InterruptedExcep
339342
Assert.fail(errorMessage);
340343
}
341344
}
345+
346+
@Test
347+
@LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.NO_FIELDS_IN_ACROFORM)})
348+
public void acroFieldDictionaryNoFields() throws IOException, InterruptedException {
349+
String outPdf = destinationFolder + "acroFieldDictionaryNoFields.pdf";
350+
String cmpPdf = sourceFolder + "cmp_acroFieldDictionaryNoFields.pdf";
351+
352+
PdfWriter writer = new PdfWriter(outPdf);
353+
PdfReader reader = new PdfReader(sourceFolder + "acroFieldDictionaryNoFields.pdf");
354+
PdfDocument pdfDoc = new PdfDocument(reader, writer);
355+
356+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
357+
pdfDoc.close();
358+
359+
CompareTool compareTool = new CompareTool();
360+
String errorMessage = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "diff_");
361+
if (errorMessage != null) {
362+
Assert.fail(errorMessage);
363+
}
364+
}
342365
}

io/src/main/java/com/itextpdf/io/LogMessageConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public final class LogMessageConstant {
8585
public static final String CALCULATE_HASHCODE_FOR_MODIFIED_PDFNUMBER = "Calculate hashcode for modified PdfNumber.";
8686
public static final String MAKE_COPY_OF_CATALOG_DICTIONARY_IS_FORBIDDEN = "Make copy of Catalog dictionary is forbidden.";
8787
public static final String NAME_ALREADY_EXISTS_IN_THE_NAME_TREE = "Name \"{0}\" already exists in the name tree; old value will be replaced by the new one.";
88+
public static final String NO_FIELDS_IN_ACROFORM = "Required AcroForm entry /Fields does not exist in the document. Empty array /Fields will be created.";
8889
public static final String NOT_TAGGED_PAGES_IN_TAGGED_DOCUMENT = "Not tagged pages are copied to the tagged document. Destination document now may contain not tagged content.";
8990
public static final String NUM_TREE_SHALL_NOT_END_WITH_KEY = "Number tree ends with a key which is invalid according to the PDF specification.";
9091
public static final String PDF_READER_CLOSING_FAILED = "PdfReader closing failed due to the error occurred!";

0 commit comments

Comments
 (0)