@@ -52,6 +52,8 @@ This file is part of the iText (R) project.
52
52
import com .itextpdf .kernel .pdf .xobject .PdfFormXObject ;
53
53
import com .itextpdf .test .ExtendedITextTest ;
54
54
import com .itextpdf .test .annotations .type .IntegrationTest ;
55
+
56
+ import java .util .ArrayList ;
55
57
import org .junit .Assert ;
56
58
import org .junit .BeforeClass ;
57
59
import org .junit .Test ;
@@ -60,6 +62,7 @@ This file is part of the iText (R) project.
60
62
import java .io .ByteArrayInputStream ;
61
63
import java .io .ByteArrayOutputStream ;
62
64
import java .io .IOException ;
65
+ import java .util .Set ;
63
66
import java .util .Map ;
64
67
65
68
@ Category (IntegrationTest .class )
@@ -147,4 +150,65 @@ public void annotationAppearanceTest() throws IOException {
147
150
pdfDocument .close ();
148
151
Assert .assertEquals (1 , objs .size ());
149
152
}
153
+
154
+ @ Test
155
+ public void setModifiedFlagTest () throws IOException {
156
+ testSetModified (false );
157
+ }
158
+
159
+ @ Test
160
+ public void setModifiedFlagAppendModeTest () throws IOException {
161
+ testSetModified (true );
162
+ }
163
+
164
+ private static void testSetModified (boolean isAppendMode ) throws IOException {
165
+ String [] expectedKeys = {
166
+ "new_key1" ,
167
+ "new_key2" ,
168
+ "new_key3" ,
169
+ };
170
+
171
+ ByteArrayOutputStream sourceFile = createDocumentInMemory ();
172
+ ByteArrayOutputStream modifiedFile = new ByteArrayOutputStream ();
173
+ PdfReader reader = new PdfReader (new ByteArrayInputStream (sourceFile .toByteArray ()));
174
+ PdfDocument pdfDoc = isAppendMode
175
+ ? new PdfDocument (reader , new PdfWriter (modifiedFile ), new StampingProperties ().useAppendMode ())
176
+ : new PdfDocument (reader , new PdfWriter (modifiedFile ));
177
+ PdfNameTree nameTree = pdfDoc .getCatalog ().getNameTree (PdfName .Dests );
178
+ Map <String , PdfObject > names = nameTree .getNames ();
179
+ ArrayList <String > keys = new ArrayList <>(names .keySet ());
180
+
181
+ for (int i = 0 ; i < keys .size (); i ++) {
182
+ names .put (expectedKeys [i ], names .get (keys .get (i )));
183
+ names .remove (keys .get (i ));
184
+ }
185
+
186
+ nameTree .setModified ();
187
+
188
+ pdfDoc .close ();
189
+
190
+ reader = new PdfReader (new ByteArrayInputStream (modifiedFile .toByteArray ()));
191
+ pdfDoc = new PdfDocument (reader );
192
+ nameTree = pdfDoc .getCatalog ().getNameTree (PdfName .Dests );
193
+ Set <String > actualKeys = nameTree .getNames ().keySet ();
194
+
195
+ Assert .assertArrayEquals (expectedKeys , actualKeys .toArray ());
196
+ }
197
+
198
+ private static ByteArrayOutputStream createDocumentInMemory () {
199
+ ByteArrayOutputStream boas = new ByteArrayOutputStream ();
200
+ PdfDocument pdfDoc = new PdfDocument (new PdfWriter (boas ));
201
+
202
+ pdfDoc .addNewPage ();
203
+ pdfDoc .getCatalog ().getNameTree (PdfName .Dests ).addEntry ("key1" ,
204
+ new PdfArray (new float [] {0 , 0 , 0 , 0 }));
205
+ pdfDoc .getCatalog ().getNameTree (PdfName .Dests ).addEntry ("key2" ,
206
+ new PdfArray (new float [] {1 , 1 , 1 , 1 }));
207
+ pdfDoc .getCatalog ().getNameTree (PdfName .Dests ).addEntry ("key3" ,
208
+ new PdfArray (new float [] {2 , 2 , 2 , 2 }));
209
+
210
+ pdfDoc .close ();
211
+
212
+ return boas ;
213
+ }
150
214
}
0 commit comments