Skip to content

Commit 3ab56e4

Browse files
Add new test for #setPage method of PdfFormField
DEVSIX-2912 Autoported commit. Original commit hash: [554dde125] Manual files: pdftest/src/main/java/com/itextpdf/test/ITextTest.java
1 parent 4ca1882 commit 3ab56e4

File tree

3 files changed

+72
-27
lines changed

3 files changed

+72
-27
lines changed

itext.tests/itext.forms.tests/itext/forms/PdfFormFieldTest.cs

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ source product.
6060

6161
namespace iText.Forms {
6262
public class PdfFormFieldTest : ExtendedITextTest {
63-
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
64-
.CurrentContext.TestDirectory) + "/resources/itext/forms/PdfFormFieldTest/";
65-
6663
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
6764
+ "/test/itext/forms/PdfFormFieldTest/";
6865

66+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
67+
.CurrentContext.TestDirectory) + "/resources/itext/forms/PdfFormFieldTest/";
68+
6969
[NUnit.Framework.OneTimeSetUp]
7070
public static void BeforeClass() {
7171
CreateDestinationFolder(destinationFolder);
@@ -656,29 +656,6 @@ public virtual void FillFieldWithHebrewCase4() {
656656
+ testName + "_"));
657657
}
658658

659-
private void CreateAcroForm(PdfDocument pdfDoc, PdfAcroForm form, PdfFont font, String text, int offSet) {
660-
for (int x = offSet; x < (offSet + 3); x++) {
661-
Rectangle rect = new Rectangle(100 + (30 * x), 100 + (100 * x), 55, 30);
662-
PdfFormField field = PdfFormField.CreateText(pdfDoc, rect, "f-" + x, "", font, 12.0f);
663-
field.SetJustification(PdfFormField.ALIGN_RIGHT);
664-
if (text != null) {
665-
field.SetValue(text);
666-
}
667-
form.AddField(field);
668-
}
669-
}
670-
671-
private void AddParagraph(Document document, String text, PdfFont font) {
672-
document.Add(new Paragraph("Hello world ").Add(text).SetFont(font));
673-
}
674-
675-
private void FillAcroForm(PdfDocument pdfDocument, String text) {
676-
PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(pdfDocument, false);
677-
foreach (PdfFormField field in acroForm.GetFormFields().Values) {
678-
field.SetValue(text);
679-
}
680-
}
681-
682659
/// <exception cref="System.IO.IOException"/>
683660
/// <exception cref="System.Exception"/>
684661
[NUnit.Framework.Test]
@@ -1073,5 +1050,64 @@ public virtual void TestDaInAppendMode() {
10731050
pdfDoc.Close();
10741051
NUnit.Framework.Assert.AreEqual("/F1 25 Tf", da.ToString());
10751052
}
1053+
1054+
/// <exception cref="System.IO.IOException"/>
1055+
[NUnit.Framework.Test]
1056+
public virtual void SetPageNewField() {
1057+
String filename = destinationFolder + "setPageNewField.pdf";
1058+
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filename));
1059+
pdfDoc.AddNewPage();
1060+
pdfDoc.AddNewPage();
1061+
pdfDoc.AddNewPage();
1062+
String fieldName = "field1";
1063+
int pageNum = 2;
1064+
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);
1065+
PdfTextFormField field1 = PdfFormField.CreateText(pdfDoc, new Rectangle(90, 700, 150, 22), fieldName, "new field"
1066+
);
1067+
field1.SetPage(pageNum);
1068+
form.AddField(field1);
1069+
pdfDoc.Close();
1070+
// -------------------------------------------
1071+
PrintOutputPdfNameAndDir(filename);
1072+
PdfDocument resPdf = new PdfDocument(new PdfReader(filename));
1073+
PdfArray fieldsArr = resPdf.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm).GetAsArray(PdfName
1074+
.Fields);
1075+
NUnit.Framework.Assert.AreEqual(1, fieldsArr.Size());
1076+
PdfDictionary field = fieldsArr.GetAsDictionary(0);
1077+
PdfDictionary fieldP = field.GetAsDictionary(PdfName.P);
1078+
// TODO DEVSIX-2912: shall be equal to second page object
1079+
NUnit.Framework.Assert.AreEqual(resPdf.GetPage(3).GetPdfObject(), fieldP);
1080+
NUnit.Framework.Assert.IsNull(resPdf.GetPage(1).GetPdfObject().GetAsArray(PdfName.Annots));
1081+
PdfArray secondPageAnnots = resPdf.GetPage(2).GetPdfObject().GetAsArray(PdfName.Annots);
1082+
NUnit.Framework.Assert.AreEqual(1, secondPageAnnots.Size());
1083+
NUnit.Framework.Assert.AreEqual(field, secondPageAnnots.Get(0));
1084+
// TODO DEVSIX-2912: third page annotations array shall be null
1085+
PdfArray thirdPageAnnots = resPdf.GetPage(3).GetPdfObject().GetAsArray(PdfName.Annots);
1086+
NUnit.Framework.Assert.AreEqual(1, thirdPageAnnots.Size());
1087+
NUnit.Framework.Assert.AreEqual(field, thirdPageAnnots.Get(0));
1088+
}
1089+
1090+
private void CreateAcroForm(PdfDocument pdfDoc, PdfAcroForm form, PdfFont font, String text, int offSet) {
1091+
for (int x = offSet; x < (offSet + 3); x++) {
1092+
Rectangle rect = new Rectangle(100 + (30 * x), 100 + (100 * x), 55, 30);
1093+
PdfFormField field = PdfFormField.CreateText(pdfDoc, rect, "f-" + x, "", font, 12.0f);
1094+
field.SetJustification(PdfFormField.ALIGN_RIGHT);
1095+
if (text != null) {
1096+
field.SetValue(text);
1097+
}
1098+
form.AddField(field);
1099+
}
1100+
}
1101+
1102+
private void AddParagraph(Document document, String text, PdfFont font) {
1103+
document.Add(new Paragraph("Hello world ").Add(text).SetFont(font));
1104+
}
1105+
1106+
private void FillAcroForm(PdfDocument pdfDocument, String text) {
1107+
PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(pdfDocument, false);
1108+
foreach (PdfFormField field in acroForm.GetFormFields().Values) {
1109+
field.SetValue(text);
1110+
}
1111+
}
10761112
}
10771113
}

itext/itext.pdftest/itext/test/ITextTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ public static void DeleteDirectory(String path) {
8787
DeleteDirectoryContents(path, true);
8888
}
8989

90+
public static void PrintOutputPdfNameAndDir(String pdfName) {
91+
PrintPathToConsole(pdfName, "Output PDF: ");
92+
PrintPathToConsole(new FileInfo(pdfName).DirectoryName, "Output PDF folder: ");
93+
}
94+
95+
public static void PrintPathToConsole(String path, String comment) {
96+
Console.Out.WriteLine(comment + "file:///" + UrlUtil.ToNormalizedURI(new FileInfo(path)).AbsolutePath);
97+
}
98+
9099
protected virtual byte[] ReadFile(String filename) {
91100
return File.ReadAllBytes(filename);
92101
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d2e3a11ab70fd5a13a93d922cfd27bb94a69894d
1+
aad6d465c3a248545818a0ab6d45a84661d2e137

0 commit comments

Comments
 (0)