Skip to content

Commit 22e702c

Browse files
LodrKumquatpavel-alay
authored andcommitted
Fix wrong loading of items in PdfNameTree
DEVSIX-1285
1 parent 0f19ed4 commit 22e702c

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,13 @@ public Map<String, PdfObject> getNames() {
9898
Set<String> keys = new HashSet<>();
9999
keys.addAll(items.keySet());
100100
for (String key : keys) {
101-
PdfArray arr = getNameArray(items.get(key));
102-
if (arr != null) {
103-
items.put(key, arr);
104-
} else
101+
if (treeType.equals(PdfName.Dests)) {
102+
PdfArray arr = getDestArray(items.get(key));
103+
if (arr != null) {
104+
items.put(key, arr);
105+
} else
106+
items.remove(key);
107+
} else if (items.get(key) == null)
105108
items.remove(key);
106109
}
107110
}
@@ -112,15 +115,14 @@ public Map<String, PdfObject> getNames() {
112115
if (destinations != null) {
113116
Set<PdfName> keys = destinations.keySet();
114117
for (PdfName key : keys) {
115-
PdfArray array = getNameArray(destinations.get(key));
118+
PdfArray array = getDestArray(destinations.get(key));
116119
if (array == null) {
117120
continue;
118121
}
119122
items.put(key.getValue(), array);
120123
}
121124
}
122125
}
123-
124126
return items;
125127
}
126128

@@ -254,7 +256,7 @@ private PdfString iterateItems(PdfDictionary dictionary, Map<String, PdfObject>
254256
return null;
255257
}
256258

257-
private PdfArray getNameArray(PdfObject obj) {
259+
private PdfArray getDestArray(PdfObject obj) {
258260
if (obj == null)
259261
return null;
260262
if (obj.isArray())
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)