Skip to content

Commit d2cbc0d

Browse files
committed
Invoke equals on a PdfName constant rather than on a nullable object
DEVSIX-5631
1 parent bfa83e0 commit d2cbc0d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfPage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public PdfPage copyTo(PdfDocument toDocument, IPdfPageExtraCopier copier) {
402402
PdfPage page = getDocument().getPageFactory().createPdfPage(dictionary);
403403
copyInheritedProperties(page, toDocument);
404404
for (PdfAnnotation annot : getAnnotations()) {
405-
if (annot.getSubtype().equals(PdfName.Link)) {
405+
if (PdfName.Link.equals(annot.getSubtype())) {
406406
getDocument().storeLinkAnnotation(page, (PdfLinkAnnotation) annot);
407407
} else {
408408
PdfAnnotation newAnnot = PdfAnnotation.makeAnnotation(

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfPagesTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.io.LogMessageConstant;
4646
import com.itextpdf.io.image.ImageDataFactory;
47+
import com.itextpdf.io.source.RandomAccessSourceFactory;
4748
import com.itextpdf.io.util.MessageFormatUtil;
4849
import com.itextpdf.kernel.PdfException;
4950
import com.itextpdf.kernel.colors.ColorConstants;
5051
import com.itextpdf.kernel.geom.PageSize;
5152
import com.itextpdf.kernel.geom.Rectangle;
53+
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
5254
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
5355
import com.itextpdf.kernel.pdf.extgstate.PdfExtGState;
5456
import 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

Comments
 (0)