@@ -29,24 +29,30 @@ You should have received a copy of the GNU Affero General Public License
29
29
30
30
namespace iText . Forms {
31
31
public class PdfAcroFormInAppendModeTest : ExtendedITextTest {
32
+ private const String testFolder = "PdfAcroFormInAppendModeTest/" ;
33
+
32
34
private static readonly String destinationFolder = NUnit . Framework . TestContext . CurrentContext . TestDirectory
33
- + "/test/itext/forms/PdfAcroFormInAppendModeTest/" ;
35
+ + "/test/itext/forms/" + testFolder ;
34
36
35
37
private static readonly String sourceFolder = iText . Test . TestUtil . GetParentProjectDirectory ( NUnit . Framework . TestContext
36
- . CurrentContext . TestDirectory ) + "/resources/itext/forms/PdfAcroFormInAppendModeTest/" ;
38
+ . CurrentContext . TestDirectory ) + "/resources/itext/forms/" + testFolder ;
37
39
38
40
private static readonly String inputFile = destinationFolder + "inputFile.pdf" ;
39
41
42
+ private static readonly String inputFileWithIndirectFieldsArray = destinationFolder + "inputFileWithIndirectFieldsArray.pdf" ;
43
+
40
44
[ NUnit . Framework . OneTimeSetUp ]
41
45
public static void BeforeClass ( ) {
42
46
CreateDestinationFolder ( destinationFolder ) ;
43
47
CreateInputFile ( ) ;
48
+ CreateInputFileWithIndirectFieldsArray ( ) ;
44
49
}
45
50
46
51
[ NUnit . Framework . Test ]
47
- public virtual void CreateFieldInAppendModeTest ( ) {
48
- String outputFile = "createFieldInAppendModeTest.pdf" ;
49
- PdfDocument outputDoc = CreateDocInAppendMode ( destinationFolder + outputFile ) ;
52
+ public virtual void AddFieldTest ( ) {
53
+ String outputFile = "addFieldTest.pdf" ;
54
+ PdfDocument outputDoc = new PdfDocument ( new PdfReader ( inputFile ) , new PdfWriter ( destinationFolder + outputFile
55
+ ) , new StampingProperties ( ) . UseAppendMode ( ) ) ;
50
56
PdfFormField field = PdfFormField . CreateCheckBox ( outputDoc , new Rectangle ( 10 , 10 , 24 , 24 ) , "checkboxname" ,
51
57
"On" , PdfFormField . TYPE_CHECK ) ;
52
58
PdfAcroForm . GetAcroForm ( outputDoc , true ) . AddField ( field ) ;
@@ -55,18 +61,19 @@ public virtual void CreateFieldInAppendModeTest() {
55
61
}
56
62
57
63
[ NUnit . Framework . Test ]
58
- public virtual void RemoveFieldInAppendModeTest ( ) {
59
- String outputFile = "removeFieldInAppendModeTest.pdf" ;
60
- PdfDocument outputDoc = CreateDocInAppendMode ( destinationFolder + outputFile ) ;
64
+ public virtual void RemoveFieldTest ( ) {
65
+ String outputFile = "removeFieldTest.pdf" ;
66
+ PdfDocument outputDoc = new PdfDocument ( new PdfReader ( inputFile ) , new PdfWriter ( destinationFolder + outputFile
67
+ ) , new StampingProperties ( ) . UseAppendMode ( ) ) ;
61
68
PdfAcroForm . GetAcroForm ( outputDoc , true ) . RemoveField ( "textfield2" ) ;
62
69
outputDoc . Close ( ) ;
63
70
CompareWithCmp ( outputFile ) ;
64
71
}
65
72
66
73
[ NUnit . Framework . Test ]
67
- public virtual void RemoveFieldWithParentInAppendModeTest ( ) {
74
+ public virtual void RemoveKidTest ( ) {
68
75
// Creating input document
69
- String inputFile = "inputRemoveFieldWithParentInAppendModeTest .pdf" ;
76
+ String inputFile = "in_removeKidTest .pdf" ;
70
77
PdfDocument inDoc = new PdfDocument ( new PdfWriter ( destinationFolder + inputFile ) ) ;
71
78
inDoc . AddNewPage ( ) ;
72
79
PdfFormField root = PdfFormField . CreateEmptyField ( inDoc ) ;
@@ -77,7 +84,7 @@ public virtual void RemoveFieldWithParentInAppendModeTest() {
77
84
PdfAcroForm . GetAcroForm ( inDoc , true ) . AddField ( root ) ;
78
85
inDoc . Close ( ) ;
79
86
// Creating stamping document
80
- String outputFile = "removeFieldWithParentInAppendModeTest .pdf" ;
87
+ String outputFile = "removeKidTest .pdf" ;
81
88
PdfReader reader = new PdfReader ( destinationFolder + inputFile ) ;
82
89
PdfWriter writer = new PdfWriter ( destinationFolder + outputFile ) ;
83
90
PdfDocument outputDoc = new PdfDocument ( reader , writer , new StampingProperties ( ) . UseAppendMode ( ) ) ;
@@ -87,16 +94,65 @@ public virtual void RemoveFieldWithParentInAppendModeTest() {
87
94
}
88
95
89
96
[ NUnit . Framework . Test ]
90
- public virtual void ReplaceFieldInAppendModeTest ( ) {
91
- String outputFile = "replaceFieldInAppendModeTest.pdf" ;
92
- PdfDocument outputDoc = CreateDocInAppendMode ( destinationFolder + outputFile ) ;
97
+ public virtual void ReplaceFieldTest ( ) {
98
+ String outputFile = "replaceFieldTest.pdf" ;
99
+ PdfDocument outputDoc = new PdfDocument ( new PdfReader ( inputFile ) , new PdfWriter ( destinationFolder + outputFile
100
+ ) , new StampingProperties ( ) . UseAppendMode ( ) ) ;
93
101
PdfFormField newField = PdfFormField . CreateText ( outputDoc , new Rectangle ( 20 , 160 , 100 , 20 ) , "newfield" , "new field"
94
102
) ;
95
103
PdfAcroForm . GetAcroForm ( outputDoc , true ) . ReplaceField ( "textfield1" , newField ) ;
96
104
outputDoc . Close ( ) ;
97
105
CompareWithCmp ( outputFile ) ;
98
106
}
99
107
108
+ [ NUnit . Framework . Test ]
109
+ public virtual void AddFieldToIndirectFieldsArrayTest ( ) {
110
+ String outputFile = "addFieldToIndirectFieldsArrayTest.pdf" ;
111
+ PdfDocument document = new PdfDocument ( new PdfReader ( inputFileWithIndirectFieldsArray ) , new PdfWriter ( destinationFolder
112
+ + outputFile ) , new StampingProperties ( ) . UseAppendMode ( ) ) ;
113
+ PdfFormField field = PdfFormField . CreateCheckBox ( document , new Rectangle ( 10 , 10 , 24 , 24 ) , "checkboxname" ,
114
+ "On" , PdfFormField . TYPE_CHECK ) ;
115
+ // Get an existing acroform and add new field to it
116
+ PdfAcroForm . GetAcroForm ( document , false ) . AddField ( field ) ;
117
+ document . Close ( ) ;
118
+ CompareWithCmp ( outputFile ) ;
119
+ }
120
+
121
+ [ NUnit . Framework . Test ]
122
+ public virtual void RemoveFieldFromIndirectFieldsArrayTest ( ) {
123
+ String outputFile = "removeFieldFromIndirectFieldsArrayTest.pdf" ;
124
+ PdfDocument outputDoc = new PdfDocument ( new PdfReader ( inputFileWithIndirectFieldsArray ) , new PdfWriter ( destinationFolder
125
+ + outputFile ) , new StampingProperties ( ) . UseAppendMode ( ) ) ;
126
+ PdfAcroForm . GetAcroForm ( outputDoc , true ) . RemoveField ( "textfield2" ) ;
127
+ outputDoc . Close ( ) ;
128
+ CompareWithCmp ( outputFile ) ;
129
+ }
130
+
131
+ [ NUnit . Framework . Test ]
132
+ public virtual void RemoveKidFromIndirectKidsArrayTest ( ) {
133
+ String inputFile = "in_removeKidFromIndirectKidsArrayTest.pdf" ;
134
+ String outputFile = "removeKidFromIndirectKidsArrayTest.pdf" ;
135
+ // Creating input document
136
+ PdfDocument inDoc = new PdfDocument ( new PdfWriter ( destinationFolder + inputFile ) ) ;
137
+ inDoc . AddNewPage ( ) ;
138
+ PdfFormField root = PdfFormField . CreateEmptyField ( inDoc ) ;
139
+ root . SetFieldName ( "root" ) ;
140
+ PdfFormField child = PdfFormField . CreateEmptyField ( inDoc ) ;
141
+ child . SetFieldName ( "child" ) ;
142
+ root . AddKid ( child ) ;
143
+ PdfAcroForm . GetAcroForm ( inDoc , true ) . AddField ( root ) ;
144
+ // make kids array indirect
145
+ PdfAcroForm . GetAcroForm ( inDoc , true ) . GetField ( "root" ) . GetKids ( ) . MakeIndirect ( inDoc ) ;
146
+ inDoc . Close ( ) ;
147
+ // Creating stamping document
148
+ PdfReader reader = new PdfReader ( destinationFolder + inputFile ) ;
149
+ PdfWriter writer = new PdfWriter ( destinationFolder + outputFile ) ;
150
+ PdfDocument outputDoc = new PdfDocument ( reader , writer , new StampingProperties ( ) . UseAppendMode ( ) ) ;
151
+ PdfAcroForm . GetAcroForm ( outputDoc , true ) . RemoveField ( "root.child" ) ;
152
+ outputDoc . Close ( ) ;
153
+ CompareWithCmp ( outputFile ) ;
154
+ }
155
+
100
156
private static void CreateInputFile ( ) {
101
157
PdfDocument document = new PdfDocument ( new PdfWriter ( inputFile ) ) ;
102
158
document . AddNewPage ( ) ;
@@ -107,10 +163,16 @@ private static void CreateInputFile() {
107
163
document . Close ( ) ;
108
164
}
109
165
110
- private static PdfDocument CreateDocInAppendMode ( String outFile ) {
111
- PdfReader reader = new PdfReader ( inputFile ) ;
112
- PdfWriter writer = new PdfWriter ( outFile ) ;
113
- return new PdfDocument ( reader , writer , new StampingProperties ( ) . UseAppendMode ( ) ) ;
166
+ private static void CreateInputFileWithIndirectFieldsArray ( ) {
167
+ PdfDocument document = new PdfDocument ( new PdfWriter ( inputFileWithIndirectFieldsArray ) ) ;
168
+ document . AddNewPage ( ) ;
169
+ PdfAcroForm acroForm = PdfAcroForm . GetAcroForm ( document , true ) ;
170
+ acroForm . GetFields ( ) . MakeIndirect ( document ) ;
171
+ acroForm . AddField ( PdfFormField . CreateText ( document , new Rectangle ( 20 , 160 , 100 , 20 ) , "textfield1" , "text1"
172
+ ) ) ;
173
+ acroForm . AddField ( PdfFormField . CreateText ( document , new Rectangle ( 20 , 130 , 100 , 20 ) , "textfield2" , "text2"
174
+ ) ) ;
175
+ document . Close ( ) ;
114
176
}
115
177
116
178
private static void CompareWithCmp ( String outputFile ) {
0 commit comments