@@ -44,11 +44,13 @@ This file is part of the iText (R) project.
44
44
45
45
import com .itextpdf .io .LogMessageConstant ;
46
46
import com .itextpdf .io .image .ImageDataFactory ;
47
+ import com .itextpdf .io .source .RandomAccessSourceFactory ;
47
48
import com .itextpdf .io .util .MessageFormatUtil ;
48
49
import com .itextpdf .kernel .PdfException ;
49
50
import com .itextpdf .kernel .colors .ColorConstants ;
50
51
import com .itextpdf .kernel .geom .PageSize ;
51
52
import com .itextpdf .kernel .geom .Rectangle ;
53
+ import com .itextpdf .kernel .pdf .annot .PdfAnnotation ;
52
54
import com .itextpdf .kernel .pdf .canvas .PdfCanvas ;
53
55
import com .itextpdf .kernel .pdf .extgstate .PdfExtGState ;
54
56
import com .itextpdf .kernel .pdf .xobject .PdfFormXObject ;
@@ -586,6 +588,26 @@ public void verifyPagesAreNotReadOnOpenTest() throws IOException {
586
588
Assert .assertFalse (reader .pagesAreRead );
587
589
}
588
590
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
+
589
611
@ Test
590
612
public void readPagesInBlocksTest () throws IOException {
591
613
String srcFile = sourceFolder + "docWithBalancedPageTree.pdf" ;
@@ -735,6 +757,15 @@ private static int verifyIntegrity(PdfPagesTree pagesTree) {
735
757
return -1 ;
736
758
}
737
759
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
+
738
769
private class CustomPdfReader extends PdfReader {
739
770
740
771
public boolean pagesAreRead = false ;
0 commit comments