Skip to content

Commit aad6d46

Browse files
Add new test for #setPage method of PdfFormField
DEVSIX-2912
1 parent d2e3a11 commit aad6d46

File tree

2 files changed

+87
-39
lines changed

2 files changed

+87
-39
lines changed

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

Lines changed: 78 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,19 @@ This file is part of the iText (R) project.
7474
import com.itextpdf.test.annotations.LogMessage;
7575
import com.itextpdf.test.annotations.LogMessages;
7676
import com.itextpdf.test.annotations.type.IntegrationTest;
77+
import java.io.ByteArrayInputStream;
78+
import java.io.IOException;
79+
import java.util.Map;
7780
import org.junit.Assert;
7881
import org.junit.BeforeClass;
7982
import org.junit.Test;
8083
import org.junit.experimental.categories.Category;
8184

82-
import java.io.ByteArrayInputStream;
83-
import java.io.IOException;
84-
import java.util.Map;
85-
8685
@Category(IntegrationTest.class)
8786
public class PdfFormFieldTest extends ExtendedITextTest {
8887

89-
public static final String sourceFolder = "./src/test/resources/com/itextpdf/forms/PdfFormFieldTest/";
9088
public static final String destinationFolder = "./target/test/com/itextpdf/forms/PdfFormFieldTest/";
89+
public static final String sourceFolder = "./src/test/resources/com/itextpdf/forms/PdfFormFieldTest/";
9190

9291
@BeforeClass
9392
public static void beforeClass() {
@@ -634,7 +633,7 @@ public void formRegenerateWithInvalidDefaultAppearance01() throws IOException, I
634633
public void fillFieldWithHebrewCase1() throws IOException, InterruptedException {
635634
String testName = "fillFieldWithHebrewCase1";
636635
String outPdf = destinationFolder + testName + ".pdf";
637-
String cmpPdf = sourceFolder + "cmp_"+ testName + ".pdf";
636+
String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf";
638637

639638
PdfWriter writer = new PdfWriter(outPdf);
640639
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -657,15 +656,15 @@ public void fillFieldWithHebrewCase1() throws IOException, InterruptedException
657656

658657
pdfDoc.close();
659658

660-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff" + testName+ "_"));
659+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff" + testName + "_"));
661660
}
662661

663662
@Test
664663
//Create a document with formfields and paragraphs in both fonts, and fill them after closing and reopening the document
665664
public void fillFieldWithHebrewCase2() throws IOException, InterruptedException {
666665
String testName = "fillFieldWithHebrewCase2";
667666
String outPdf = destinationFolder + testName + ".pdf";
668-
String cmpPdf = sourceFolder + "cmp_"+ testName + ".pdf";
667+
String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf";
669668

670669
ByteArrayOutputStream baos = new ByteArrayOutputStream();
671670

@@ -693,15 +692,15 @@ public void fillFieldWithHebrewCase2() throws IOException, InterruptedException
693692
fillAcroForm(pdfDocument, text);
694693
pdfDocument.close();
695694

696-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff" + testName+ "_"));
695+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff" + testName + "_"));
697696
}
698697

699698
@Test
700699
//Create a document with formfields in both fonts, and fill them before closing the document
701700
public void fillFieldWithHebrewCase3() throws IOException, InterruptedException {
702701
String testName = "fillFieldWithHebrewCase3";
703702
String outPdf = destinationFolder + testName + ".pdf";
704-
String cmpPdf = sourceFolder + "cmp_"+ testName + ".pdf";
703+
String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf";
705704

706705
PdfWriter writer = new PdfWriter(outPdf);
707706
PdfDocument pdfDoc = new PdfDocument(writer);
@@ -719,15 +718,15 @@ public void fillFieldWithHebrewCase3() throws IOException, InterruptedException
719718

720719
pdfDoc.close();
721720

722-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff" + testName+ "_"));
721+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff" + testName + "_"));
723722
}
724723

725724
@Test
726725
//Create a document with formfields in both fonts, and fill them after closing and reopening the document
727726
public void fillFieldWithHebrewCase4() throws IOException, InterruptedException {
728727
String testName = "fillFieldWithHebrewCase4";
729728
String outPdf = destinationFolder + testName + ".pdf";
730-
String cmpPdf = sourceFolder + "cmp_"+ testName + ".pdf";
729+
String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf";
731730

732731
ByteArrayOutputStream baos = new ByteArrayOutputStream();
733732

@@ -751,30 +750,7 @@ public void fillFieldWithHebrewCase4() throws IOException, InterruptedException
751750
fillAcroForm(pdfDocument, text);
752751
pdfDocument.close();
753752

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 + "_"));
778754
}
779755

780756
@Test
@@ -807,7 +783,7 @@ public void multilineFormFieldNewLineFontType3Test() throws IOException, Interru
807783
String testName = "multilineFormFieldNewLineFontType3Test";
808784

809785
String outPdf = destinationFolder + testName + ".pdf";
810-
String cmpPdf = sourceFolder + "cmp_"+ testName + ".pdf";
786+
String cmpPdf = sourceFolder + "cmp_" + testName + ".pdf";
811787
String srcPdf = sourceFolder + testName + ".pdf";
812788

813789
PdfWriter writer = new PdfWriter(outPdf);
@@ -817,7 +793,7 @@ public void multilineFormFieldNewLineFontType3Test() throws IOException, Interru
817793
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
818794
PdfTextFormField info = (PdfTextFormField) form.getField("info");
819795
info.setValue("A\n\nE");
820-
796+
821797
pdfDoc.close();
822798
CompareTool compareTool = new CompareTool();
823799
String errorMessage = compareTool.compareByContent(outPdf, cmpPdf, destinationFolder, "diff_");
@@ -917,7 +893,6 @@ public void noMaxLenWithSetCombFlagTest() throws IOException, InterruptedExcepti
917893
}
918894
}
919895

920-
921896
@Test
922897
public void maxLenWithSetCombFlagAppearanceTest() throws IOException, InterruptedException {
923898
String srcPdf = sourceFolder + "maxLenFields.pdf";
@@ -1196,4 +1171,68 @@ public void testDaInAppendMode() throws IOException, InterruptedException {
11961171

11971172
Assert.assertEquals("/F1 25 Tf", da.toString());
11981173
}
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+
}
11991238
}

pdftest/src/main/java/com/itextpdf/test/ITextTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ public static void restoreCryptographyRestrictions() {
151151
}
152152
}
153153

154+
public static void printOutputPdfNameAndDir(String pdfName) {
155+
printPathToConsole(pdfName, "Output PDF: ");
156+
printPathToConsole(new File(pdfName).getParent(), "Output PDF folder: ");
157+
}
158+
159+
public static void printPathToConsole(String path, String comment) {
160+
System.out.println(comment + "file:///" + new File(path).toURI().normalize().getPath());
161+
}
162+
154163
protected Timeout getTestTimeout() {
155164
return new Timeout(5, TimeUnit.MINUTES);
156165
}

0 commit comments

Comments
 (0)