|
| 1 | +package com.itextpdf.kernel.pdf; |
| 2 | + |
| 3 | +import com.itextpdf.io.font.FontConstants; |
| 4 | +import com.itextpdf.io.util.UrlUtil; |
| 5 | +import com.itextpdf.kernel.colors.ColorConstants; |
| 6 | +import com.itextpdf.kernel.font.PdfFontFactory; |
| 7 | +import com.itextpdf.kernel.geom.Rectangle; |
| 8 | +import com.itextpdf.kernel.pdf.annot.PdfAnnotationAppearance; |
| 9 | +import com.itextpdf.kernel.pdf.canvas.PdfCanvas; |
| 10 | +import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; |
| 11 | +import com.itextpdf.test.ExtendedITextTest; |
| 12 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 13 | +import org.junit.Assert; |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.experimental.categories.Category; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +@Category(IntegrationTest.class) |
| 22 | +public class PdfNameTreeTest extends ExtendedITextTest { |
| 23 | + |
| 24 | + public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/PdfNameTreeTest/"; |
| 25 | + public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/PdfNameTreeTest/"; |
| 26 | + |
| 27 | + @BeforeClass |
| 28 | + public static void beforeClass() { |
| 29 | + createOrClearDestinationFolder(destinationFolder); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void EmbeddedFileAndJavascriptTest() throws IOException { |
| 34 | + PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "FileWithSingleAttachment.pdf")); |
| 35 | + PdfNameTree embeddedFilesNameTree = pdfDocument.getCatalog().getNameTree(PdfName.EmbeddedFiles); |
| 36 | + Map<String, PdfObject> objs = embeddedFilesNameTree.getNames(); |
| 37 | + PdfNameTree javascript = pdfDocument.getCatalog().getNameTree(PdfName.JavaScript); |
| 38 | + Map<String, PdfObject> objs2 = javascript.getNames(); |
| 39 | + pdfDocument.close(); |
| 40 | + Assert.assertEquals(1, objs.size()); |
| 41 | + Assert.assertEquals(1, objs2.size()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void AnnotationAppearanceTest() throws IOException { |
| 46 | + PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "AnnotationAppearanceTest.pdf")); |
| 47 | + PdfPage page = pdfDocument.addNewPage(); |
| 48 | + PdfCanvas canvas = new PdfCanvas(page); |
| 49 | + canvas.setFillColor(ColorConstants.MAGENTA).beginText().setFontAndSize(PdfFontFactory.createFont(FontConstants.TIMES_ROMAN), 30) |
| 50 | + .setTextMatrix(25, 500).showText("This file has AP key in Names dictionary").endText(); |
| 51 | + PdfArray array = new PdfArray(); |
| 52 | + array.add(new PdfString("normalAppearance")); |
| 53 | + array.add(new PdfAnnotationAppearance().setState(PdfName.N, new PdfFormXObject(new Rectangle(50, 50 , 50, 50))).getPdfObject()); |
| 54 | + |
| 55 | + PdfDictionary dict = new PdfDictionary(); |
| 56 | + dict.put(PdfName.Names, array); |
| 57 | + PdfDictionary dictionary = new PdfDictionary(); |
| 58 | + dictionary.put(PdfName.AP, dict); |
| 59 | + pdfDocument.getCatalog().getPdfObject().put(PdfName.Names, dictionary); |
| 60 | + |
| 61 | + PdfNameTree appearance = pdfDocument.getCatalog().getNameTree(PdfName.AP); |
| 62 | + Map<String, PdfObject> objs = appearance.getNames(); |
| 63 | + pdfDocument.close(); |
| 64 | + Assert.assertEquals(1, objs.size()); |
| 65 | + } |
| 66 | +} |
0 commit comments