@@ -44,11 +44,13 @@ This file is part of the iText (R) project.
4444
4545import com .itextpdf .io .LogMessageConstant ;
4646import com .itextpdf .io .image .ImageDataFactory ;
47+ import com .itextpdf .io .source .RandomAccessSourceFactory ;
4748import com .itextpdf .io .util .MessageFormatUtil ;
4849import com .itextpdf .kernel .PdfException ;
4950import com .itextpdf .kernel .colors .ColorConstants ;
5051import com .itextpdf .kernel .geom .PageSize ;
5152import com .itextpdf .kernel .geom .Rectangle ;
53+ import com .itextpdf .kernel .pdf .annot .PdfAnnotation ;
5254import com .itextpdf .kernel .pdf .canvas .PdfCanvas ;
5355import com .itextpdf .kernel .pdf .extgstate .PdfExtGState ;
5456import com .itextpdf .kernel .pdf .xobject .PdfFormXObject ;
@@ -586,6 +588,26 @@ public void verifyPagesAreNotReadOnOpenTest() throws IOException {
586588 Assert .assertFalse (reader .pagesAreRead );
587589 }
588590
591+ @ Test
592+ public void copyAnnotationWithoutSubtypeTest () throws IOException {
593+ try (
594+ ByteArrayOutputStream baos = createSourceDocumentWithEmptyAnnotation (new ByteArrayOutputStream ());
595+ PdfDocument documentToMerge = new PdfDocument (
596+ new PdfReader (
597+ new RandomAccessSourceFactory ().createSource (baos .toByteArray ()),
598+ new ReaderProperties ()));
599+ ByteArrayOutputStream resultantBaos = new ByteArrayOutputStream ();
600+ PdfDocument resultantDocument = new PdfDocument (new PdfWriter (resultantBaos ))) {
601+
602+ // We do expect that the following line will not throw any NPE
603+ PdfPage copiedPage = documentToMerge .getPage (1 ).copyTo (resultantDocument );
604+ Assert .assertEquals (1 , copiedPage .getAnnotations ().size ());
605+ Assert .assertNull (copiedPage .getAnnotations ().get (0 ).getSubtype ());
606+
607+ resultantDocument .addPage (copiedPage );
608+ }
609+ }
610+
589611 @ Test
590612 public void readPagesInBlocksTest () throws IOException {
591613 String srcFile = sourceFolder + "docWithBalancedPageTree.pdf" ;
@@ -735,6 +757,15 @@ private static int verifyIntegrity(PdfPagesTree pagesTree) {
735757 return -1 ;
736758 }
737759
760+ private static ByteArrayOutputStream createSourceDocumentWithEmptyAnnotation (ByteArrayOutputStream baos ) {
761+ try (PdfDocument sourceDocument = new PdfDocument (new PdfWriter (baos ))) {
762+ PdfPage page = sourceDocument .addNewPage ();
763+ PdfAnnotation annotation = PdfAnnotation .makeAnnotation (new PdfDictionary ());
764+ page .addAnnotation (annotation );
765+ return baos ;
766+ }
767+ }
768+
738769 private class CustomPdfReader extends PdfReader {
739770
740771 public boolean pagesAreRead = false ;
0 commit comments