Skip to content

Commit ffa2d3c

Browse files
author
Alexandr Pliushchou
committed
Change how annot tag applied for UA2 in addAnnotation method
DEVSIX-8807
1 parent e6fb263 commit ffa2d3c

File tree

6 files changed

+36
-14
lines changed

6 files changed

+36
-14
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,12 +889,8 @@ public PdfPage addAnnotation(int index, PdfAnnotation annotation, boolean tagAnn
889889
// "Annot" tag was added starting from PDF 1.5
890890
&& PdfVersion.PDF_1_4.compareTo(getDocument().getPdfVersion()) < 0) {
891891

892-
if (PdfVersion.PDF_2_0.compareTo(getDocument().getPdfVersion()) > 0) {
893-
if (!(annotation instanceof PdfWidgetAnnotation) && !(annotation instanceof PdfLinkAnnotation)
894-
&& !(annotation instanceof PdfPrinterMarkAnnotation)) {
895-
tagPointer.addTag(StandardRoles.ANNOT);
896-
}
897-
} else if (annotation instanceof PdfMarkupAnnotation) {
892+
if (!(annotation instanceof PdfWidgetAnnotation) && !(annotation instanceof PdfLinkAnnotation)
893+
&& !(annotation instanceof PdfPrinterMarkAnnotation)) {
898894
tagPointer.addTag(StandardRoles.ANNOT);
899895
}
900896
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ This file is part of the iText (R) project.
2525
import com.itextpdf.io.source.ByteArrayOutputStream;
2626
import com.itextpdf.kernel.geom.Rectangle;
2727
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
28+
import com.itextpdf.kernel.pdf.annot.PdfPopupAnnotation;
2829
import com.itextpdf.kernel.utils.CompareTool;
2930
import com.itextpdf.test.ExtendedITextTest;
3031

32+
import java.io.ByteArrayInputStream;
3133
import java.io.IOException;
3234
import java.util.Arrays;
3335
import java.util.Collections;
@@ -168,6 +170,31 @@ public void setTrimBoxTest() {
168170
Assertions.assertTrue(pageDictionary.isModified());
169171
}
170172

173+
@Test
174+
public void addAnnotationAnnotTagPDF2Test() throws IOException {
175+
byte[] docBytes;
176+
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
177+
try (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream,
178+
new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
179+
doc.setTagged();
180+
PdfPage page = new PdfPage(doc);
181+
PdfPopupAnnotation annot = new PdfPopupAnnotation(new Rectangle(100, 100, 100, 100));
182+
annot.setName(new PdfString("this is a pop up"));
183+
page.addAnnotation(annot);
184+
}
185+
docBytes = outputStream.toByteArray();
186+
}
187+
188+
try (PdfDocument docReopen = new PdfDocument(new PdfReader(new ByteArrayInputStream(docBytes)))) {
189+
PdfDictionary structTreeRoot = docReopen.getCatalog().getPdfObject().getAsDictionary(PdfName.StructTreeRoot);
190+
PdfDictionary structElem = ((PdfDictionary) structTreeRoot.getAsArray(PdfName.K).get(0)).
191+
getAsDictionary(PdfName.K);
192+
Assertions.assertEquals(PdfName.Annot, structElem.getAsName(PdfName.S));
193+
Assertions.assertEquals(new PdfString("this is a pop up"), structElem.getAsDictionary(PdfName.K).
194+
getAsDictionary(PdfName.Obj).getAsString(PdfName.NM));
195+
}
196+
}
197+
171198
/**
172199
* Simulates indirect state of object making sure it is not marked as modified.
173200
*

layout/src/test/java/com/itextpdf/layout/PdfUA2AnnotationsTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ This file is part of the iText (R) project.
6161
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
6262
import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
6363
import com.itextpdf.kernel.pdf.tagging.StandardRoles;
64-
import com.itextpdf.kernel.pdf.tagutils.TagTreePointer;
6564
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
6665
import com.itextpdf.kernel.utils.CompareTool;
6766
import com.itextpdf.kernel.xmp.XMPException;
@@ -217,9 +216,9 @@ public void pdfUA2RubberStampNoContentsAnnotationsTest() throws IOException, XMP
217216
}
218217

219218
@Test
220-
//TODO DEVSIX-8807 Kernel: addAnnotation method doesn't annotate content elements with Annot tag when PDF version is 2.0
221-
public void pdfUA2ScreenAnnotationsTest() throws IOException, XMPException {
219+
public void pdfUA2ScreenAnnotationsTest() throws IOException, XMPException, InterruptedException {
222220
String outFile = DESTINATION_FOLDER + "pdfuaScreenAnnotationTest.pdf";
221+
String cmpFile = SOURCE_FOLDER + "cmp_pdfuaScreenAnnotationTest.pdf";
223222

224223
try (PdfDocument pdfDocument = new PdfDocument(
225224
new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
@@ -230,7 +229,7 @@ public void pdfUA2ScreenAnnotationsTest() throws IOException, XMPException {
230229
pdfPage.addAnnotation(screen);
231230
pdfPage.flush();
232231
}
233-
new VeraPdfValidator().validateFailure(outFile);// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
232+
compareAndValidate(outFile, cmpFile);
234233
}
235234

236235
@Test
@@ -319,9 +318,9 @@ public void pdfUA2RedactionNoContentsAnnotationsTest() throws IOException, XMPEx
319318
}
320319

321320
@Test
322-
//TODO DEVSIX-8807 Kernel: addAnnotation method doesn't annotate content elements with Annot tag when PDF version is 2.0
323321
public void pdfUA23DAnnotationsTest() throws IOException, XMPException, InterruptedException {
324322
String outFile = DESTINATION_FOLDER + "pdfua3DAnnotationTest.pdf";
323+
String cmpFile = SOURCE_FOLDER + "cmp_pdfua3DAnnotationTest.pdf";
325324

326325
try (PdfDocument pdfDocument = new PdfDocument(
327326
new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
@@ -332,7 +331,7 @@ public void pdfUA23DAnnotationsTest() throws IOException, XMPException, Interrup
332331

333332
pdfPage.flush();
334333
}
335-
new VeraPdfValidator().validateFailure(outFile);// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
334+
compareAndValidate(outFile, cmpFile);
336335
}
337336

338337
@Test
@@ -464,9 +463,9 @@ public void pdfUA2AltContentDiffAnnotationTest()
464463
}
465464

466465
@Test
467-
//TODO DEVSIX-8807 Kernel: addAnnotation method doesn't annotate content elements with Annot tag when PDF version is 2.0
468466
public void pdfUA2TabAnnotationsTest() throws IOException, XMPException, InterruptedException {
469467
String outFile = DESTINATION_FOLDER + "pdfuaMultipleAnnotsTabAnnotationTest.pdf";
468+
String cmpFile = SOURCE_FOLDER + "cmp_pdfuaMultipleAnnotsTabAnnotationTest.pdf";
470469

471470
try (PdfDocument pdfDocument = new PdfDocument(
472471
new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
@@ -484,7 +483,7 @@ public void pdfUA2TabAnnotationsTest() throws IOException, XMPException, Interru
484483
Assertions.assertEquals(PdfName.S, pageT);
485484
}
486485
}
487-
new VeraPdfValidator().validateFailure(outFile);// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
486+
compareAndValidate(outFile, cmpFile);
488487
}
489488

490489
@Test

0 commit comments

Comments
 (0)