@@ -41,21 +41,25 @@ This file is part of the iText (R) project.
41
41
42
42
@ Category (IntegrationTest .class )
43
43
public class PdfAcroFormInAppendModeTest extends ExtendedITextTest {
44
+ private static final String testFolder = "PdfAcroFormInAppendModeTest/" ;
45
+ private static final String destinationFolder = "./target/test/com/itextpdf/forms/" + testFolder ;
46
+ private static final String sourceFolder = "./src/test/resources/com/itextpdf/forms/" + testFolder ;
44
47
45
- private static final String destinationFolder = "./target/test/com/itextpdf/forms/PdfAcroFormInAppendModeTest/" ;
46
- private static final String sourceFolder = "./src/test/resources/com/itextpdf/forms/PdfAcroFormInAppendModeTest/" ;
47
48
private static final String inputFile = destinationFolder + "inputFile.pdf" ;
49
+ private static final String inputFileWithIndirectFieldsArray = destinationFolder + "inputFileWithIndirectFieldsArray.pdf" ;
48
50
49
51
@ BeforeClass
50
52
public static void beforeClass () throws FileNotFoundException {
51
53
createDestinationFolder (destinationFolder );
52
54
createInputFile ();
55
+ createInputFileWithIndirectFieldsArray ();
53
56
}
54
57
55
58
@ Test
56
- public void createFieldInAppendModeTest () throws IOException , InterruptedException {
57
- String outputFile = "createFieldInAppendModeTest.pdf" ;
58
- PdfDocument outputDoc = createDocInAppendMode (destinationFolder + outputFile );
59
+ public void addFieldTest () throws IOException , InterruptedException {
60
+ String outputFile = "addFieldTest.pdf" ;
61
+ PdfDocument outputDoc = new PdfDocument (new PdfReader (inputFile ), new PdfWriter (destinationFolder + outputFile ),
62
+ new StampingProperties ().useAppendMode ());
59
63
PdfFormField field = PdfFormField .createCheckBox (
60
64
outputDoc ,
61
65
new Rectangle (10 , 10 , 24 , 24 ),
@@ -67,18 +71,19 @@ public void createFieldInAppendModeTest() throws IOException, InterruptedExcepti
67
71
}
68
72
69
73
@ Test
70
- public void removeFieldInAppendModeTest () throws IOException , InterruptedException {
71
- String outputFile = "removeFieldInAppendModeTest.pdf" ;
72
- PdfDocument outputDoc = createDocInAppendMode (destinationFolder + outputFile );
74
+ public void removeFieldTest () throws IOException , InterruptedException {
75
+ String outputFile = "removeFieldTest.pdf" ;
76
+ PdfDocument outputDoc = new PdfDocument (new PdfReader (inputFile ), new PdfWriter (destinationFolder + outputFile ),
77
+ new StampingProperties ().useAppendMode ());
73
78
PdfAcroForm .getAcroForm (outputDoc , true ).removeField ("textfield2" );
74
79
outputDoc .close ();
75
80
compareWithCmp (outputFile );
76
81
}
77
82
78
83
@ Test
79
- public void removeFieldWithParentInAppendModeTest () throws IOException , InterruptedException {
84
+ public void removeKidTest () throws IOException , InterruptedException {
80
85
// Creating input document
81
- String inputFile = "inputRemoveFieldWithParentInAppendModeTest .pdf" ;
86
+ String inputFile = "in_removeKidTest .pdf" ;
82
87
PdfDocument inDoc = new PdfDocument (new PdfWriter (destinationFolder + inputFile ));
83
88
inDoc .addNewPage ();
84
89
PdfFormField root = PdfFormField .createEmptyField (inDoc );
@@ -90,7 +95,7 @@ public void removeFieldWithParentInAppendModeTest() throws IOException, Interrup
90
95
inDoc .close ();
91
96
92
97
// Creating stamping document
93
- String outputFile = "removeFieldWithParentInAppendModeTest .pdf" ;
98
+ String outputFile = "removeKidTest .pdf" ;
94
99
PdfReader reader = new PdfReader (destinationFolder + inputFile );
95
100
PdfWriter writer = new PdfWriter (destinationFolder + outputFile );
96
101
PdfDocument outputDoc = new PdfDocument (reader , writer , new StampingProperties ().useAppendMode ());
@@ -102,15 +107,78 @@ public void removeFieldWithParentInAppendModeTest() throws IOException, Interrup
102
107
}
103
108
104
109
@ Test
105
- public void replaceFieldInAppendModeTest () throws IOException , InterruptedException {
106
- String outputFile = "replaceFieldInAppendModeTest.pdf" ;
107
- PdfDocument outputDoc = createDocInAppendMode (destinationFolder + outputFile );
108
- PdfFormField newField = PdfFormField .createText (outputDoc , new Rectangle (20 , 160 , 100 , 20 ), "newfield" , "new field" );
110
+ public void replaceFieldTest () throws IOException , InterruptedException {
111
+ String outputFile = "replaceFieldTest.pdf" ;
112
+ PdfDocument outputDoc = new PdfDocument (new PdfReader (inputFile ), new PdfWriter (destinationFolder + outputFile ),
113
+ new StampingProperties ().useAppendMode ());
114
+ PdfFormField newField = PdfFormField
115
+ .createText (outputDoc , new Rectangle (20 , 160 , 100 , 20 ), "newfield" , "new field" );
109
116
PdfAcroForm .getAcroForm (outputDoc , true ).replaceField ("textfield1" , newField );
110
117
outputDoc .close ();
111
118
compareWithCmp (outputFile );
112
119
}
113
120
121
+ @ Test
122
+ public void addFieldToIndirectFieldsArrayTest () throws IOException , InterruptedException {
123
+ String outputFile = "addFieldToIndirectFieldsArrayTest.pdf" ;
124
+
125
+ PdfDocument document = new PdfDocument (new PdfReader (inputFileWithIndirectFieldsArray ),
126
+ new PdfWriter (destinationFolder + outputFile ), new StampingProperties ().useAppendMode ());
127
+
128
+ PdfFormField field = PdfFormField .createCheckBox (
129
+ document ,
130
+ new Rectangle (10 , 10 , 24 , 24 ),
131
+ "checkboxname" , "On" ,
132
+ PdfFormField .TYPE_CHECK );
133
+
134
+ // Get an existing acroform and add new field to it
135
+ PdfAcroForm .getAcroForm (document , false ).addField (field );
136
+
137
+ document .close ();
138
+
139
+ compareWithCmp (outputFile );
140
+ }
141
+
142
+ @ Test
143
+ public void removeFieldFromIndirectFieldsArrayTest () throws IOException , InterruptedException {
144
+ String outputFile = "removeFieldFromIndirectFieldsArrayTest.pdf" ;
145
+ PdfDocument outputDoc = new PdfDocument (new PdfReader (inputFileWithIndirectFieldsArray ), new PdfWriter (destinationFolder + outputFile ),
146
+ new StampingProperties ().useAppendMode ());
147
+ PdfAcroForm .getAcroForm (outputDoc , true ).removeField ("textfield2" );
148
+ outputDoc .close ();
149
+ compareWithCmp (outputFile );
150
+ }
151
+
152
+
153
+ @ Test
154
+ public void removeKidFromIndirectKidsArrayTest () throws IOException , InterruptedException {
155
+ String inputFile = "in_removeKidFromIndirectKidsArrayTest.pdf" ;
156
+ String outputFile = "removeKidFromIndirectKidsArrayTest.pdf" ;
157
+
158
+ // Creating input document
159
+ PdfDocument inDoc = new PdfDocument (new PdfWriter (destinationFolder + inputFile ));
160
+ inDoc .addNewPage ();
161
+ PdfFormField root = PdfFormField .createEmptyField (inDoc );
162
+ root .setFieldName ("root" );
163
+ PdfFormField child = PdfFormField .createEmptyField (inDoc );
164
+ child .setFieldName ("child" );
165
+ root .addKid (child );
166
+ PdfAcroForm .getAcroForm (inDoc , true ).addField (root );
167
+ // make kids array indirect
168
+ PdfAcroForm .getAcroForm (inDoc , true ).getField ("root" ).getKids ().makeIndirect (inDoc );
169
+ inDoc .close ();
170
+
171
+ // Creating stamping document
172
+ PdfReader reader = new PdfReader (destinationFolder + inputFile );
173
+ PdfWriter writer = new PdfWriter (destinationFolder + outputFile );
174
+ PdfDocument outputDoc = new PdfDocument (reader , writer , new StampingProperties ().useAppendMode ());
175
+
176
+ PdfAcroForm .getAcroForm (outputDoc , true ).removeField ("root.child" );
177
+
178
+ outputDoc .close ();
179
+ compareWithCmp (outputFile );
180
+ }
181
+
114
182
private static void createInputFile () throws FileNotFoundException {
115
183
PdfDocument document = new PdfDocument (new PdfWriter (inputFile ));
116
184
document .addNewPage ();
@@ -121,10 +189,15 @@ private static void createInputFile() throws FileNotFoundException {
121
189
document .close ();
122
190
}
123
191
124
- private static PdfDocument createDocInAppendMode (String outFile ) throws IOException {
125
- PdfReader reader = new PdfReader (inputFile );
126
- PdfWriter writer = new PdfWriter (outFile );
127
- return new PdfDocument (reader , writer , new StampingProperties ().useAppendMode ());
192
+ private static void createInputFileWithIndirectFieldsArray ()
193
+ throws FileNotFoundException {
194
+ PdfDocument document = new PdfDocument (new PdfWriter (inputFileWithIndirectFieldsArray ));
195
+ document .addNewPage ();
196
+ PdfAcroForm acroForm = PdfAcroForm .getAcroForm (document , true );
197
+ acroForm .getFields ().makeIndirect (document );
198
+ acroForm .addField (PdfFormField .createText (document , new Rectangle (20 , 160 , 100 , 20 ), "textfield1" , "text1" ));
199
+ acroForm .addField (PdfFormField .createText (document , new Rectangle (20 , 130 , 100 , 20 ), "textfield2" , "text2" ));
200
+ document .close ();
128
201
}
129
202
130
203
private static void compareWithCmp (String outputFile ) throws IOException , InterruptedException {
0 commit comments