Skip to content

Commit bcea6d1

Browse files
committed
Add tests for PdfUA 8.9 annotations, 8.11 - 8.14
DEVSIX-7942
1 parent a4a6534 commit bcea6d1

17 files changed

+753
-2
lines changed

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

Lines changed: 622 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,21 @@ This file is part of the iText (R) project.
3030
import com.itextpdf.kernel.font.PdfFontFactory;
3131
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
3232
import com.itextpdf.kernel.geom.Rectangle;
33+
import com.itextpdf.kernel.pdf.PageLabelNumberingStyle;
34+
import com.itextpdf.kernel.pdf.PdfDictionary;
3335
import com.itextpdf.kernel.pdf.PdfDocument;
3436
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
37+
import com.itextpdf.kernel.pdf.PdfName;
38+
import com.itextpdf.kernel.pdf.PdfNumber;
39+
import com.itextpdf.kernel.pdf.PdfPage;
3540
import com.itextpdf.kernel.pdf.PdfString;
3641
import com.itextpdf.kernel.pdf.PdfVersion;
3742
import com.itextpdf.kernel.pdf.PdfViewerPreferences;
3843
import com.itextpdf.kernel.pdf.PdfWriter;
3944
import com.itextpdf.kernel.pdf.WriterProperties;
4045
import com.itextpdf.kernel.pdf.action.PdfAction;
4146
import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
47+
import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
4248
import com.itextpdf.kernel.pdf.tagging.IStructureNode;
4349
import com.itextpdf.kernel.pdf.tagging.PdfNamespace;
4450
import com.itextpdf.kernel.pdf.tagging.PdfStructTreeRoot;
@@ -581,14 +587,14 @@ public void checkBibliographicEntryTest() throws IOException, XMPException, Inte
581587
String outFile = DESTINATION_FOLDER + "bibliographicEntryTest.pdf";
582588
String cmpFile = SOURCE_FOLDER + "cmp_bibliographicEntryTest.pdf";
583589

584-
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))){
590+
try (PdfDocument pdfDocument = new PdfDocument(
591+
new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
585592
Document document = new Document(pdfDocument);
586593
PdfFont font = PdfFontFactory.createFont(FONT_FOLDER + "FreeSans.ttf",
587594
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
588595
document.setFont(font);
589596
createSimplePdfUA2Document(pdfDocument);
590597

591-
592598
Paragraph section = new Paragraph("Bibliography section:\n");
593599
section.getAccessibilityProperties().setRole(StandardRoles.SECT);
594600
Paragraph bibliography = new Paragraph("1. Author A. Title of Book. Publisher, Year.");
@@ -600,6 +606,116 @@ public void checkBibliographicEntryTest() throws IOException, XMPException, Inte
600606
compareAndValidate(outFile, cmpFile);
601607
}
602608

609+
@Test
610+
public void checkMetadataNoTitleTest() throws IOException, XMPException {
611+
String outFile = DESTINATION_FOLDER + "pdfuaMetadataNoTitleTest.pdf";
612+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
613+
byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp"));
614+
XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes));
615+
pdfDocument.setXmpMetadata(xmpMeta);
616+
pdfDocument.setTagged();
617+
pdfDocument.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(true));
618+
pdfDocument.getCatalog().setLang(new PdfString("en-US"));
619+
}
620+
Assert.assertNotNull(new VeraPdfValidator().validate(outFile));// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
621+
}
622+
623+
@Test
624+
public void checkMetadataDisplayDocTitleFalseTest() throws IOException, XMPException {
625+
String outFile = DESTINATION_FOLDER + "pdfuaMetadataDisplayDocTitleFalseTest.pdf";
626+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
627+
byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp"));
628+
XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes));
629+
pdfDocument.setXmpMetadata(xmpMeta);
630+
pdfDocument.setTagged();
631+
pdfDocument.getCatalog().setViewerPreferences(new PdfViewerPreferences().setDisplayDocTitle(false));
632+
pdfDocument.getCatalog().setLang(new PdfString("en-US"));
633+
PdfDocumentInfo info = pdfDocument.getDocumentInfo();
634+
info.setTitle("PdfUA2 Title");
635+
}
636+
Assert.assertNotNull(new VeraPdfValidator().validate(outFile));// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
637+
}
638+
639+
@Test
640+
public void checkMetadataNoViewerPrefTest() throws IOException, XMPException {
641+
String outFile = DESTINATION_FOLDER + "pdfuaMetadataNoViewerPrefTest.pdf";
642+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
643+
byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp"));
644+
XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes));
645+
pdfDocument.setXmpMetadata(xmpMeta);
646+
pdfDocument.setTagged();
647+
pdfDocument.getCatalog().setLang(new PdfString("en-US"));
648+
PdfDocumentInfo info = pdfDocument.getDocumentInfo();
649+
info.setTitle("PdfUA2 Title");
650+
}
651+
Assert.assertNotNull(new VeraPdfValidator().validate(outFile));// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
652+
}
653+
654+
@Test
655+
public void checkEmbeddedFileTest() throws IOException, XMPException, InterruptedException {
656+
String outFile = DESTINATION_FOLDER + "pdfuaEmbeddedFileTest.pdf";
657+
String cmpFile = SOURCE_FOLDER + "cmp_pdfuaEmbeddedFileTest.pdf";
658+
659+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
660+
createSimplePdfUA2Document(pdfDocument);
661+
PdfFont font = PdfFontFactory.createFont(FONT_FOLDER + "FreeSans.ttf",
662+
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
663+
Paragraph paragraph = new Paragraph("Hello PdfUA2").setFont(font);
664+
new Document(pdfDocument).add(paragraph);
665+
PdfFileSpec spec = PdfFileSpec.createEmbeddedFileSpec(pdfDocument, SOURCE_FOLDER + "sample.wav", "sample.wav", "sample", null, null);
666+
pdfDocument.addFileAttachment("specificname", spec);
667+
}
668+
compareAndValidate(outFile, cmpFile);
669+
}
670+
671+
@Test
672+
public void checkEmbeddedFileNoDescTest() throws IOException, XMPException {
673+
String outFile = DESTINATION_FOLDER + "pdfuaEmbeddedFileNoDescTest.pdf";
674+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
675+
createSimplePdfUA2Document(pdfDocument);
676+
PdfFileSpec spec = PdfFileSpec.createEmbeddedFileSpec(pdfDocument, SOURCE_FOLDER + "sample.wav", "sample.wav", "sample", null, null);
677+
((PdfDictionary) spec.getPdfObject()).remove(PdfName.Desc);
678+
pdfDocument.addFileAttachment("specificname", spec);
679+
}
680+
Assert.assertNotNull(new VeraPdfValidator().validate(outFile));// Android-Conversion-Skip-Line (TODO DEVSIX-7377 introduce pdf\a validation on Android)
681+
}
682+
683+
@Test
684+
public void checkPageLabelTest() throws IOException, XMPException, InterruptedException {
685+
String outFile = DESTINATION_FOLDER + "pdfuaPageLabelTest.pdf";
686+
String cmpFile = SOURCE_FOLDER + "cmp_pdfuaPageLabelTest.pdf";
687+
688+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
689+
createSimplePdfUA2Document(pdfDocument);
690+
PdfPage pdfPage = pdfDocument.addNewPage();
691+
PdfFont font = PdfFontFactory.createFont(FONT_FOLDER + "FreeSans.ttf",
692+
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
693+
Paragraph paragraph = new Paragraph("Hello PdfUA2").setFont(font);
694+
new Document(pdfDocument).add(paragraph);
695+
pdfPage.setPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, null, 1);
696+
}
697+
compareAndValidate(outFile, cmpFile);
698+
}
699+
700+
@Test
701+
public void checkPageNumberAndLabelTest() throws IOException, XMPException, InterruptedException {
702+
String outFile = DESTINATION_FOLDER + "pdfuaPageNumLabelTest.pdf";
703+
String cmpFile = SOURCE_FOLDER + "cmp_pdfuaPageNumLabelTest.pdf";
704+
705+
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile, new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0)))) {
706+
createSimplePdfUA2Document(pdfDocument);
707+
Document document = new Document(pdfDocument);
708+
PdfPage pdfPage = pdfDocument.addNewPage();
709+
PdfFont font = PdfFontFactory.createFont(FONT_FOLDER + "FreeSans.ttf",
710+
"WinAnsi", EmbeddingStrategy.FORCE_EMBEDDED);
711+
Paragraph paragraph = new Paragraph("Hello PdfUA2").setFont(font);
712+
document.add(paragraph);
713+
pdfPage.getPdfObject().getAsStream(PdfName.Contents).put(PdfName.PageNum, new PdfNumber(5));
714+
pdfPage.setPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, null, 5);
715+
}
716+
compareAndValidate(outFile, cmpFile);
717+
}
718+
603719
private void createSimplePdfUA2Document(PdfDocument pdfDocument) throws IOException, XMPException {
604720
byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp"));
605721
XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes));

0 commit comments

Comments
 (0)