Skip to content

Commit e9ebcc0

Browse files
author
Samuel Huylebroeck
committed
Fix Names PdfObject not being marked as modified in append mode
Fix Names PdfObject present in Catalog not being marked as modified in append mode when adding embedded files, when the Names object was present already and indirect. Fix capitalization in tests DEVSIX-2826
1 parent d508087 commit e9ebcc0

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2251,6 +2251,7 @@ private void ensureTreeRootAddedToNames(PdfObject treeRoot, PdfName treeType) {
22512251
names.makeIndirect(this);
22522252
}
22532253
names.put(treeType, treeRoot);
2254+
names.setModified();
22542255
}
22552256

22562257
private static boolean isXmpMetaHasProperty(XMPMeta xmpMeta, String schemaNS, String propName) throws XMPException {

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfNameTreeTest.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
4848
import com.itextpdf.kernel.geom.Rectangle;
4949
import com.itextpdf.kernel.pdf.annot.PdfAnnotationAppearance;
5050
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
51+
import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
5152
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
5253
import com.itextpdf.test.ExtendedITextTest;
5354
import com.itextpdf.test.annotations.type.IntegrationTest;
@@ -56,6 +57,8 @@ This file is part of the iText (R) project.
5657
import org.junit.Test;
5758
import org.junit.experimental.categories.Category;
5859

60+
import java.io.ByteArrayInputStream;
61+
import java.io.ByteArrayOutputStream;
5962
import java.io.IOException;
6063
import java.util.Map;
6164

@@ -71,7 +74,7 @@ public static void beforeClass() {
7174
}
7275

7376
@Test
74-
public void EmbeddedFileAndJavascriptTest() throws IOException {
77+
public void embeddedFileAndJavascriptTest() throws IOException {
7578
PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "FileWithSingleAttachment.pdf"));
7679
PdfNameTree embeddedFilesNameTree = pdfDocument.getCatalog().getNameTree(PdfName.EmbeddedFiles);
7780
Map<String, PdfObject> objs = embeddedFilesNameTree.getNames();
@@ -83,7 +86,47 @@ public void EmbeddedFileAndJavascriptTest() throws IOException {
8386
}
8487

8588
@Test
86-
public void AnnotationAppearanceTest() throws IOException {
89+
public void embeddedFileAddedInAppendModeTest() throws IOException{
90+
//Create input document
91+
ByteArrayOutputStream boasEmpty = new ByteArrayOutputStream();
92+
PdfWriter emptyDocWriter = new PdfWriter(boasEmpty);
93+
PdfDocument emptyDoc = new PdfDocument(emptyDocWriter);
94+
emptyDoc.addNewPage();
95+
PdfDictionary emptyNamesDic = new PdfDictionary();
96+
emptyNamesDic.makeIndirect(emptyDoc);
97+
emptyDoc.getCatalog().getPdfObject().put(PdfName.Names,emptyNamesDic);
98+
99+
emptyDoc.close();
100+
101+
//Create input document
102+
ByteArrayOutputStream boasAttached = new ByteArrayOutputStream();
103+
PdfWriter attachDocWriter = new PdfWriter(boasAttached);
104+
PdfDocument attachDoc = new PdfDocument(attachDocWriter);
105+
attachDoc.addNewPage();
106+
attachDoc.close();
107+
108+
//Attach file in append mode
109+
PdfReader appendReader = new PdfReader(new ByteArrayInputStream(boasEmpty.toByteArray()));
110+
ByteArrayOutputStream boasAppend = new ByteArrayOutputStream();
111+
PdfWriter appendWriter = new PdfWriter(boasAppend);
112+
PdfDocument appendDoc = new PdfDocument(appendReader,appendWriter,new StampingProperties().useAppendMode());
113+
114+
appendDoc.addFileAttachment("Test File", PdfFileSpec.createEmbeddedFileSpec(appendDoc,boasAttached.toByteArray(),"Append Embedded File test","Test file",null));
115+
appendDoc.close();
116+
117+
//Check final result
118+
PdfReader finalReader = new PdfReader(new ByteArrayInputStream(boasAppend.toByteArray()));
119+
PdfDocument finalDoc = new PdfDocument(finalReader);
120+
121+
PdfNameTree embeddedFilesNameTree = finalDoc.getCatalog().getNameTree(PdfName.EmbeddedFiles);
122+
Map<String, PdfObject> embeddedFilesMap = embeddedFilesNameTree.getNames();
123+
124+
Assert.assertTrue(embeddedFilesMap.size()>0);
125+
Assert.assertTrue(embeddedFilesMap.containsKey("Test File"));
126+
}
127+
128+
@Test
129+
public void annotationAppearanceTest() throws IOException {
87130
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "AnnotationAppearanceTest.pdf"));
88131
PdfPage page = pdfDocument.addNewPage();
89132
PdfCanvas canvas = new PdfCanvas(page);

0 commit comments

Comments
 (0)