Skip to content

Commit f59b9ab

Browse files
Add new tests for flattening of appearances with tricky dimensions
BBox and Matrix of the form field appearances are different from WidgetAnnotation Rect. DEVSIX-1741
1 parent 9842aa3 commit f59b9ab

11 files changed

+66
-25
lines changed

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

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.forms;
4444

45+
import com.itextpdf.forms.fields.PdfFormField;
46+
import com.itextpdf.forms.fields.PdfTextFormField;
47+
import com.itextpdf.kernel.colors.DeviceRgb;
4548
import com.itextpdf.kernel.pdf.PdfDocument;
4649
import com.itextpdf.kernel.pdf.PdfReader;
4750
import com.itextpdf.kernel.pdf.PdfWriter;
@@ -62,41 +65,23 @@ public class FormFieldFlatteningTest extends ExtendedITextTest {
6265

6366
@BeforeClass
6467
public static void beforeClass() {
65-
createDestinationFolder(destinationFolder);
68+
createOrClearDestinationFolder(destinationFolder);
6669
}
6770

6871
@Test
6972
public void formFlatteningTest01() throws IOException, InterruptedException {
70-
String srcFilename = sourceFolder + "formFlatteningSource.pdf";
71-
String filename = destinationFolder + "formFlatteningTest01.pdf";
73+
String srcFilename = "formFlatteningSource.pdf";
74+
String filename = "formFlatteningTest01.pdf";
7275

73-
PdfDocument doc = new PdfDocument(new PdfReader(srcFilename), new PdfWriter(filename));
74-
75-
PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true);
76-
form.flattenFields();
77-
78-
doc.close();
79-
80-
Assert.assertNull(new CompareTool().compareByContent(filename, sourceFolder + "cmp_formFlatteningTest01.pdf", destinationFolder, "diff_"));
76+
flattenFieldsAndCompare(srcFilename, filename);
8177
}
8278

8379
@Test
8480
public void formFlatteningChoiceFieldTest01() throws IOException, InterruptedException {
85-
String srcFilename = sourceFolder + "formFlatteningSourceChoiceField.pdf";
86-
String filename = destinationFolder + "formFlatteningChoiceFieldTest01.pdf";
87-
88-
PdfDocument doc = new PdfDocument(new PdfReader(srcFilename), new PdfWriter(filename));
89-
90-
PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true);
91-
form.flattenFields();
92-
93-
doc.close();
81+
String srcFilename = "formFlatteningSourceChoiceField.pdf";
82+
String filename = "formFlatteningChoiceFieldTest01.pdf";
9483

95-
CompareTool compareTool = new CompareTool();
96-
String errorMessage = compareTool.compareByContent(filename, sourceFolder + "cmp_formFlatteningChoiceFieldTest01.pdf", destinationFolder, "diff_");
97-
if (errorMessage != null) {
98-
Assert.fail(errorMessage);
99-
}
84+
flattenFieldsAndCompare(srcFilename, filename);
10085
}
10186

10287
@Test
@@ -115,4 +100,60 @@ public void multiLineFormFieldClippingTest() throws IOException, InterruptedExce
115100

116101
Assert.assertNull(new CompareTool().compareByContent(dest, cmp, destinationFolder, "diff_"));
117102
}
103+
104+
@Test
105+
public void rotatedFieldAppearanceTest01() throws IOException, InterruptedException {
106+
String srcFilename = "src_rotatedFieldAppearanceTest01.pdf";
107+
String filename = "rotatedFieldAppearanceTest01.pdf";
108+
109+
flattenFieldsAndCompare(srcFilename, filename);
110+
}
111+
112+
@Test
113+
public void rotatedFieldAppearanceTest02() throws IOException, InterruptedException {
114+
String srcFilename = "src_rotatedFieldAppearanceTest02.pdf";
115+
String filename = "rotatedFieldAppearanceTest02.pdf";
116+
117+
flattenFieldsAndCompare(srcFilename, filename);
118+
}
119+
120+
@Test
121+
public void degeneratedRectTest01() throws IOException, InterruptedException {
122+
String srcFilename = "src_degeneratedRectTest01.pdf";
123+
String filename = "degeneratedRectTest01.pdf";
124+
125+
flattenFieldsAndCompare(srcFilename, filename);
126+
}
127+
128+
@Test
129+
public void degeneratedRectTest02() throws IOException, InterruptedException {
130+
String srcFilename = "src_degeneratedRectTest02.pdf";
131+
String filename = "degeneratedRectTest02.pdf";
132+
133+
flattenFieldsAndCompare(srcFilename, filename);
134+
}
135+
136+
@Test
137+
public void scaledRectTest01() throws IOException, InterruptedException {
138+
String srcFilename = "src_scaledRectTest01.pdf";
139+
String filename = "scaledRectTest01.pdf";
140+
141+
flattenFieldsAndCompare(srcFilename, filename);
142+
}
143+
144+
private static void flattenFieldsAndCompare(String srcFile, String outFile) throws IOException, InterruptedException {
145+
PdfReader reader = new PdfReader(sourceFolder + srcFile);
146+
PdfWriter writer = new PdfWriter(destinationFolder + outFile);
147+
PdfDocument document = new PdfDocument(reader, writer);
148+
PdfAcroForm.getAcroForm(document, false).flattenFields();
149+
150+
document.close();
151+
152+
CompareTool compareTool = new CompareTool();
153+
String errorMessage = compareTool.compareByContent(destinationFolder + outFile, sourceFolder + "cmp_" + outFile, destinationFolder, "diff_");
154+
155+
if (errorMessage != null) {
156+
Assert.fail(errorMessage);
157+
}
158+
}
118159
}

0 commit comments

Comments
 (0)