@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()); }
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 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 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); }
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(); }
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();  }