From 30c6c9e883a50dae6adb0bfe884414f0d682cc31 Mon Sep 17 00:00:00 2001 From: Angelina Pavlovets Date: Wed, 4 May 2022 23:54:39 +0300 Subject: [PATCH] Handle NPE on CopyPagesTo DEVSIX-6298 --- .../com/itextpdf/forms/PdfPageFormCopier.java | 3 ++- .../java/com/itextpdf/forms/PdfFormCopyTest.java | 12 ++++++++++++ .../cmp_widgetContainsNoTEntryTest.pdf | Bin 0 -> 1304 bytes .../forms/PdfFormCopyTest/fieldThreeWidgets.pdf | Bin 0 -> 1606 bytes 4 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 forms/src/test/resources/com/itextpdf/forms/PdfFormCopyTest/cmp_widgetContainsNoTEntryTest.pdf create mode 100644 forms/src/test/resources/com/itextpdf/forms/PdfFormCopyTest/fieldThreeWidgets.pdf diff --git a/forms/src/main/java/com/itextpdf/forms/PdfPageFormCopier.java b/forms/src/main/java/com/itextpdf/forms/PdfPageFormCopier.java index 7332092504..376f2478e8 100644 --- a/forms/src/main/java/com/itextpdf/forms/PdfPageFormCopier.java +++ b/forms/src/main/java/com/itextpdf/forms/PdfPageFormCopier.java @@ -335,7 +335,8 @@ private void addChildToExistingParent(PdfDictionary fieldDic, Set existi if (existingFields.contains(name)) { PdfArray kids = parent.getAsArray(PdfName.Kids); for (PdfObject kid : kids) { - if (((PdfDictionary) kid).get(PdfName.T).equals(fieldDic.get(PdfName.T))) { + if (((PdfDictionary) kid).get(PdfName.T) != null && + ((PdfDictionary) kid).get(PdfName.T).equals(fieldDic.get(PdfName.T))) { PdfFormField kidField = makeFormField(kid); PdfFormField field = makeFormField(fieldDic); if (kidField == null || field == null) { diff --git a/forms/src/test/java/com/itextpdf/forms/PdfFormCopyTest.java b/forms/src/test/java/com/itextpdf/forms/PdfFormCopyTest.java index 8683115e66..8e386ad985 100644 --- a/forms/src/test/java/com/itextpdf/forms/PdfFormCopyTest.java +++ b/forms/src/test/java/com/itextpdf/forms/PdfFormCopyTest.java @@ -696,4 +696,16 @@ public void mergeTwoWidgetsAndTwoWidgetsTest() throws IOException, InterruptedEx Assert.assertNull(new CompareTool().compareByContent(destFilename, cmpFileName, destinationFolder, "diff_")); } + + @Test + public void widgetContainsNoTEntryTest() throws IOException, InterruptedException { + String sourceFileName = sourceFolder + "fieldThreeWidgets.pdf"; + String destFileName = destinationFolder + "widgetContainsNoTEntryTest.pdf"; + String cmpFileName = sourceFolder + "cmp_widgetContainsNoTEntryTest.pdf"; + PdfDocument sourcePdfDocument = new PdfDocument(new PdfReader(sourceFileName)); + PdfDocument resultPdfDocument = new PdfDocument(new PdfWriter(destFileName)); + sourcePdfDocument.copyPagesTo(1, sourcePdfDocument.getNumberOfPages(), resultPdfDocument, new PdfPageFormCopier()); + resultPdfDocument.close(); + Assert.assertNull(new CompareTool().compareByContent(destFileName, cmpFileName, destinationFolder, "diff_")); + } } diff --git a/forms/src/test/resources/com/itextpdf/forms/PdfFormCopyTest/cmp_widgetContainsNoTEntryTest.pdf b/forms/src/test/resources/com/itextpdf/forms/PdfFormCopyTest/cmp_widgetContainsNoTEntryTest.pdf new file mode 100644 index 0000000000000000000000000000000000000000..bf42d7a59bfedd3551766a23028b3d1baf785876 GIT binary patch literal 1304 zcmb7E%Wl&^6a}&KSKLk1L@1qy?Zifrr8cRnik3#MRTgO$j(w9FQcpFWRDo~d2iWi{ zEcgTX5oTNmBR2wdq)73}z4y#L=fs`qB-!ID02IXHgEbnFqbqV5hGYtk z#v(FJBo+#W)kB1-n)yDs+|?fkT!+rd)n=hS-6MIQXDd`B?w)M(Qsu=eW<3&{Uyh1G zs35Sg$P9`1JTj*ybM}$#8Sg)Dyj_pC_l<9MvG?!dHe_MGRwzlhP83cs6&aFqELQ3k zydWr*#N^J}etGpmpO;388mdJxYVRB{a+NdVNd!i$2+U5=oQy@Gv>PQD+K60WmM6zz zJEQv1^MM(;$qE~XnC{(OCY2%-@(!3GF?m>F z36I08&T*oWwOBRHcGEo8ORHQI6D>)1@`h8+sY@MhbBFt{seM4{L62OB?B4}bDY8w9 zvYW@aRo`GNB*O5JA9A?q9-U6l;Q?i-d2V{}l~YRlh6$=_IF({^5AUvf%?-`%>-ENB zqtngE+dM00RpRH5nXUG0T1@`ymM|vAs(9?!<5Ap=u~g$*Oh5L>HDS$+O2OTb9M_t$ z`diJLlC8w0MS(F(Z&4o@xUTKO5_XvD=mpGy;tw_)=t#7%0o7KtFo$YyTbRvt$XXa> zPv?0|uiL6)`Yq7LIMZ=$)p_1ubxJ1r8l`#LoK0XhvIj0_Y?;`p@7jsK@G}py%%h8R znI=B6;QtkKa#kz_nCwL)5<+`geZ9&*qn1-E+^OQwH`(b|*|AEMNL4BBGvDfTPA)_1 EHw%|w(*OVf literal 0 HcmV?d00001 diff --git a/forms/src/test/resources/com/itextpdf/forms/PdfFormCopyTest/fieldThreeWidgets.pdf b/forms/src/test/resources/com/itextpdf/forms/PdfFormCopyTest/fieldThreeWidgets.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e5d8a6d99fea5acc4cf55e9eda4c0331b5f04ba2 GIT binary patch literal 1606 zcmbVMTW-@p6b13;Rm@M+L@1qy?Zie=q&BImik3#MRV31UIQBR-q@HR#L4mcf0e;vD zAM5}&!kuwan=}HIk)p&WXYS+NbKRPZgDySnTdkkJzW=aj4-q8h@2$ZAA6?-}IBK7)^HSx-BBC7}>3@!j zLa5=;;zg>HP#x?E*_=%_XJ58`@x7RkO;SfhMzk9?tbDG2p-WU_y-PV&x5eRvQ zv`@4r7Q94NY;$oGt9U6E^~Fa zg_PDM9>*mQKvn!MzR$+s+_02GaBm^MEF=0Rm}ZvirNt`)##QdA3FFN(>lyr^2{QL`WC2J z9S`+f*LG2cwwUXHEA>Ie1LhMbe+TQ4$2?~b&$eMMJ9m8-oW6^B{v(W#3Zsqs2!*wP z^Ct$f?_m9Y#cTFKDA;(1M`%?`GoELI;`%f+&`%m_^0s%IJPPj~&1+(eH8fB%&X-*3 zH}lyTO$S-pPg6Thk~vG~Zo=liXJ@{jWxkX6w&xH3&6we{A`?jOXoy20z;)*5RsNa7 fxWNG}vo>A5V_majR;gmCj3X%N5v$cYxeTn|ZX>NR literal 0 HcmV?d00001