@@ -74,20 +74,19 @@ This file is part of the iText (R) project.
74
74
import com .itextpdf .test .annotations .LogMessage ;
75
75
import com .itextpdf .test .annotations .LogMessages ;
76
76
import com .itextpdf .test .annotations .type .IntegrationTest ;
77
+ import java .io .ByteArrayInputStream ;
78
+ import java .io .IOException ;
79
+ import java .util .Map ;
77
80
import org .junit .Assert ;
78
81
import org .junit .BeforeClass ;
79
82
import org .junit .Test ;
80
83
import org .junit .experimental .categories .Category ;
81
84
82
- import java .io .ByteArrayInputStream ;
83
- import java .io .IOException ;
84
- import java .util .Map ;
85
-
86
85
@ Category (IntegrationTest .class )
87
86
public class PdfFormFieldTest extends ExtendedITextTest {
88
87
89
- public static final String sourceFolder = "./src/test/resources/com/itextpdf/forms/PdfFormFieldTest/" ;
90
88
public static final String destinationFolder = "./target/test/com/itextpdf/forms/PdfFormFieldTest/" ;
89
+ public static final String sourceFolder = "./src/test/resources/com/itextpdf/forms/PdfFormFieldTest/" ;
91
90
92
91
@ BeforeClass
93
92
public static void beforeClass () {
@@ -634,7 +633,7 @@ public void formRegenerateWithInvalidDefaultAppearance01() throws IOException, I
634
633
public void fillFieldWithHebrewCase1 () throws IOException , InterruptedException {
635
634
String testName = "fillFieldWithHebrewCase1" ;
636
635
String outPdf = destinationFolder + testName + ".pdf" ;
637
- String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
636
+ String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
638
637
639
638
PdfWriter writer = new PdfWriter (outPdf );
640
639
PdfDocument pdfDoc = new PdfDocument (writer );
@@ -657,15 +656,15 @@ public void fillFieldWithHebrewCase1() throws IOException, InterruptedException
657
656
658
657
pdfDoc .close ();
659
658
660
- Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
659
+ Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
661
660
}
662
661
663
662
@ Test
664
663
//Create a document with formfields and paragraphs in both fonts, and fill them after closing and reopening the document
665
664
public void fillFieldWithHebrewCase2 () throws IOException , InterruptedException {
666
665
String testName = "fillFieldWithHebrewCase2" ;
667
666
String outPdf = destinationFolder + testName + ".pdf" ;
668
- String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
667
+ String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
669
668
670
669
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
671
670
@@ -693,15 +692,15 @@ public void fillFieldWithHebrewCase2() throws IOException, InterruptedException
693
692
fillAcroForm (pdfDocument , text );
694
693
pdfDocument .close ();
695
694
696
- Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
695
+ Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
697
696
}
698
697
699
698
@ Test
700
699
//Create a document with formfields in both fonts, and fill them before closing the document
701
700
public void fillFieldWithHebrewCase3 () throws IOException , InterruptedException {
702
701
String testName = "fillFieldWithHebrewCase3" ;
703
702
String outPdf = destinationFolder + testName + ".pdf" ;
704
- String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
703
+ String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
705
704
706
705
PdfWriter writer = new PdfWriter (outPdf );
707
706
PdfDocument pdfDoc = new PdfDocument (writer );
@@ -719,15 +718,15 @@ public void fillFieldWithHebrewCase3() throws IOException, InterruptedException
719
718
720
719
pdfDoc .close ();
721
720
722
- Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
721
+ Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
723
722
}
724
723
725
724
@ Test
726
725
//Create a document with formfields in both fonts, and fill them after closing and reopening the document
727
726
public void fillFieldWithHebrewCase4 () throws IOException , InterruptedException {
728
727
String testName = "fillFieldWithHebrewCase4" ;
729
728
String outPdf = destinationFolder + testName + ".pdf" ;
730
- String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
729
+ String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
731
730
732
731
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
733
732
@@ -751,30 +750,7 @@ public void fillFieldWithHebrewCase4() throws IOException, InterruptedException
751
750
fillAcroForm (pdfDocument , text );
752
751
pdfDocument .close ();
753
752
754
- Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
755
- }
756
-
757
- private void createAcroForm (PdfDocument pdfDoc , PdfAcroForm form , PdfFont font , String text , int offSet ){
758
- for (int x = offSet ; x < (offSet + 3 ); x ++) {
759
- Rectangle rect = new Rectangle (100 + (30 * x ), 100 + (100 * x ), 55 , 30 );
760
- PdfFormField field = PdfFormField .createText (pdfDoc , rect , "f-" + x , "" , font , 12.0f );
761
- field .setJustification (PdfFormField .ALIGN_RIGHT );
762
- if (text != null ) {
763
- field .setValue (text );
764
- }
765
- form .addField (field );
766
- }
767
- }
768
-
769
- private void addParagraph (Document document , String text , PdfFont font ){
770
- document .add (new Paragraph ("Hello world " ).add (text ).setFont (font ));
771
- }
772
-
773
- private void fillAcroForm (PdfDocument pdfDocument , String text ){
774
- PdfAcroForm acroForm = PdfAcroForm .getAcroForm (pdfDocument , false );
775
- for (PdfFormField field : acroForm .getFormFields ().values ()) {
776
- field .setValue (text );
777
- }
753
+ Assert .assertNull (new CompareTool ().compareByContent (outPdf , cmpPdf , destinationFolder , "diff" + testName + "_" ));
778
754
}
779
755
780
756
@ Test
@@ -807,7 +783,7 @@ public void multilineFormFieldNewLineFontType3Test() throws IOException, Interru
807
783
String testName = "multilineFormFieldNewLineFontType3Test" ;
808
784
809
785
String outPdf = destinationFolder + testName + ".pdf" ;
810
- String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
786
+ String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf" ;
811
787
String srcPdf = sourceFolder + testName + ".pdf" ;
812
788
813
789
PdfWriter writer = new PdfWriter (outPdf );
@@ -817,7 +793,7 @@ public void multilineFormFieldNewLineFontType3Test() throws IOException, Interru
817
793
PdfAcroForm form = PdfAcroForm .getAcroForm (pdfDoc , true );
818
794
PdfTextFormField info = (PdfTextFormField ) form .getField ("info" );
819
795
info .setValue ("A\n \n E" );
820
-
796
+
821
797
pdfDoc .close ();
822
798
CompareTool compareTool = new CompareTool ();
823
799
String errorMessage = compareTool .compareByContent (outPdf , cmpPdf , destinationFolder , "diff_" );
@@ -917,7 +893,6 @@ public void noMaxLenWithSetCombFlagTest() throws IOException, InterruptedExcepti
917
893
}
918
894
}
919
895
920
-
921
896
@ Test
922
897
public void maxLenWithSetCombFlagAppearanceTest () throws IOException , InterruptedException {
923
898
String srcPdf = sourceFolder + "maxLenFields.pdf" ;
@@ -1196,4 +1171,68 @@ public void testDaInAppendMode() throws IOException, InterruptedException {
1196
1171
1197
1172
Assert .assertEquals ("/F1 25 Tf" , da .toString ());
1198
1173
}
1174
+
1175
+ @ Test
1176
+ public void setPageNewField () throws IOException {
1177
+ String filename = destinationFolder + "setPageNewField.pdf" ;
1178
+ PdfDocument pdfDoc = new PdfDocument (new PdfWriter (filename ));
1179
+ pdfDoc .addNewPage ();
1180
+ pdfDoc .addNewPage ();
1181
+ pdfDoc .addNewPage ();
1182
+
1183
+ String fieldName = "field1" ;
1184
+ int pageNum = 2 ;
1185
+ PdfAcroForm form = PdfAcroForm .getAcroForm (pdfDoc , true );
1186
+ PdfTextFormField field1 = PdfFormField .createText (pdfDoc , new Rectangle (90 , 700 , 150 , 22 ), fieldName , "new field" );
1187
+ field1 .setPage (pageNum );
1188
+ form .addField (field1 );
1189
+
1190
+ pdfDoc .close ();
1191
+
1192
+ // -------------------------------------------
1193
+ printOutputPdfNameAndDir (filename );
1194
+ PdfDocument resPdf = new PdfDocument (new PdfReader (filename ));
1195
+ PdfArray fieldsArr = resPdf .getCatalog ().getPdfObject ()
1196
+ .getAsDictionary (PdfName .AcroForm ).getAsArray (PdfName .Fields );
1197
+ Assert .assertEquals (1 , fieldsArr .size ());
1198
+
1199
+ PdfDictionary field = fieldsArr .getAsDictionary (0 );
1200
+ PdfDictionary fieldP = field .getAsDictionary (PdfName .P );
1201
+ // TODO DEVSIX-2912: shall be equal to second page object
1202
+ Assert .assertEquals (resPdf .getPage (3 ).getPdfObject (), fieldP );
1203
+
1204
+ Assert .assertNull (resPdf .getPage (1 ).getPdfObject ().getAsArray (PdfName .Annots ));
1205
+
1206
+ PdfArray secondPageAnnots = resPdf .getPage (2 ).getPdfObject ().getAsArray (PdfName .Annots );
1207
+ Assert .assertEquals (1 , secondPageAnnots .size ());
1208
+ Assert .assertEquals (field , secondPageAnnots .get (0 ));
1209
+
1210
+ // TODO DEVSIX-2912: third page annotations array shall be null
1211
+ PdfArray thirdPageAnnots = resPdf .getPage (3 ).getPdfObject ().getAsArray (PdfName .Annots );
1212
+ Assert .assertEquals (1 , thirdPageAnnots .size ());
1213
+ Assert .assertEquals (field , thirdPageAnnots .get (0 ));
1214
+ }
1215
+
1216
+ private void createAcroForm (PdfDocument pdfDoc , PdfAcroForm form , PdfFont font , String text , int offSet ) {
1217
+ for (int x = offSet ; x < (offSet + 3 ); x ++) {
1218
+ Rectangle rect = new Rectangle (100 + (30 * x ), 100 + (100 * x ), 55 , 30 );
1219
+ PdfFormField field = PdfFormField .createText (pdfDoc , rect , "f-" + x , "" , font , 12.0f );
1220
+ field .setJustification (PdfFormField .ALIGN_RIGHT );
1221
+ if (text != null ) {
1222
+ field .setValue (text );
1223
+ }
1224
+ form .addField (field );
1225
+ }
1226
+ }
1227
+
1228
+ private void addParagraph (Document document , String text , PdfFont font ) {
1229
+ document .add (new Paragraph ("Hello world " ).add (text ).setFont (font ));
1230
+ }
1231
+
1232
+ private void fillAcroForm (PdfDocument pdfDocument , String text ) {
1233
+ PdfAcroForm acroForm = PdfAcroForm .getAcroForm (pdfDocument , false );
1234
+ for (PdfFormField field : acroForm .getFormFields ().values ()) {
1235
+ field .setValue (text );
1236
+ }
1237
+ }
1199
1238
}
0 commit comments