PdfNameTree implements Serializable { public Map<String, PdfObject> getNames() { if (items.size() > 0) { return items; } PdfDictionary dictionary = catalog.getPdfObject().getAsDictionary(PdfName.Names); if (dictionary != null) { dictionary = dictionary.getAsDictionary(treeType); if (dictionary != null) { items = readTree(dictionary); Set<String> keys = new HashSet<>(); keys.addAll(items.keySet()); for (String key : keys) { if (treeType.equals(PdfName.Dests)) { PdfArray arr = getDestArray(items.get(key)); if (arr != null) { items.put(key, arr); } else items.remove(key); } else if (items.get(key) == null) items.remove(key); } } } if (treeType.equals(PdfName.Dests)) { PdfDictionary destinations = catalog.getPdfObject().getAsDictionary(PdfName.Dests); if (destinations != null) { Set<PdfName> keys = destinations.keySet(); for (PdfName key : keys) { PdfArray array = getDestArray(destinations.get(key)); if (array == null) { continue; } items.put(key.getValue(), array); } } } return items; } PdfNameTree(PdfCatalog catalog, PdfName treeType); Map<String, PdfObject> getNames(); void addEntry(String key, PdfObject value); boolean isModified(); void setModified(); PdfDictionary buildTree();  }
@Test public void embeddedFileAndJavascriptTest() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "FileWithSingleAttachment.pdf")); PdfNameTree embeddedFilesNameTree = pdfDocument.getCatalog().getNameTree(PdfName.EmbeddedFiles); Map<String, PdfObject> objs = embeddedFilesNameTree.getNames(); PdfNameTree javascript = pdfDocument.getCatalog().getNameTree(PdfName.JavaScript); Map<String, PdfObject> objs2 = javascript.getNames(); pdfDocument.close(); Assert.assertEquals(1, objs.size()); Assert.assertEquals(1, objs2.size()); }
@Test public void embeddedFileAddedInAppendModeTest() throws IOException{ ByteArrayOutputStream boasEmpty = new ByteArrayOutputStream(); PdfWriter emptyDocWriter = new PdfWriter(boasEmpty); PdfDocument emptyDoc = new PdfDocument(emptyDocWriter); emptyDoc.addNewPage(); PdfDictionary emptyNamesDic = new PdfDictionary(); emptyNamesDic.makeIndirect(emptyDoc); emptyDoc.getCatalog().getPdfObject().put(PdfName.Names,emptyNamesDic); emptyDoc.close(); ByteArrayOutputStream boasAttached = new ByteArrayOutputStream(); PdfWriter attachDocWriter = new PdfWriter(boasAttached); PdfDocument attachDoc = new PdfDocument(attachDocWriter); attachDoc.addNewPage(); attachDoc.close(); PdfReader appendReader = new PdfReader(new ByteArrayInputStream(boasEmpty.toByteArray())); ByteArrayOutputStream boasAppend = new ByteArrayOutputStream(); PdfWriter appendWriter = new PdfWriter(boasAppend); PdfDocument appendDoc = new PdfDocument(appendReader,appendWriter,new StampingProperties().useAppendMode()); appendDoc.addFileAttachment("Test File", PdfFileSpec.createEmbeddedFileSpec(appendDoc,boasAttached.toByteArray(),"Append Embedded File test","Test file",null)); appendDoc.close(); PdfReader finalReader = new PdfReader(new ByteArrayInputStream(boasAppend.toByteArray())); PdfDocument finalDoc = new PdfDocument(finalReader); PdfNameTree embeddedFilesNameTree = finalDoc.getCatalog().getNameTree(PdfName.EmbeddedFiles); Map<String, PdfObject> embeddedFilesMap = embeddedFilesNameTree.getNames(); Assert.assertTrue(embeddedFilesMap.size()>0); Assert.assertTrue(embeddedFilesMap.containsKey("Test File")); }
@Test public void annotationAppearanceTest() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "AnnotationAppearanceTest.pdf")); PdfPage page = pdfDocument.addNewPage(); PdfCanvas canvas = new PdfCanvas(page); canvas.setFillColor(ColorConstants.MAGENTA).beginText().setFontAndSize(PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN), 30) .setTextMatrix(25, 500).showText("This file has AP key in Names dictionary").endText(); PdfArray array = new PdfArray(); array.add(new PdfString("normalAppearance")); array.add(new PdfAnnotationAppearance().setState(PdfName.N, new PdfFormXObject(new Rectangle(50, 50 , 50, 50))).getPdfObject()); PdfDictionary dict = new PdfDictionary(); dict.put(PdfName.Names, array); PdfDictionary dictionary = new PdfDictionary(); dictionary.put(PdfName.AP, dict); pdfDocument.getCatalog().getPdfObject().put(PdfName.Names, dictionary); PdfNameTree appearance = pdfDocument.getCatalog().getNameTree(PdfName.AP); Map<String, PdfObject> objs = appearance.getNames(); pdfDocument.close(); Assert.assertEquals(1, objs.size()); }
@Test public void checkNamesOrder() throws IOException { PdfDocument doc = new PdfDocument(new PdfReader(sourceFolder + "namedDestinations.pdf")); final List<String> expectedNames = new ArrayList<>(); expectedNames.add("Destination_1"); expectedNames.add("Destination_2"); expectedNames.add("Destination_3"); expectedNames.add("Destination_4"); expectedNames.add("Destination_5"); System.out.println("Expected names: " + expectedNames); for (int i = 0; i < 10; i++) { Map<String, PdfObject> names = doc.getCatalog().getNameTree(PdfName.Dests).getNames(); List<String> actualNames = new ArrayList<>(names.keySet()); System.out.println("Actual names: " + actualNames); Assert.assertEquals(expectedNames, actualNames); } doc.close(); }