@@ -48,6 +48,7 @@ source product.
48
48
using iText . Kernel ;
49
49
using iText . Kernel . Pdf . Canvas ;
50
50
using iText . Test ;
51
+ using iText . Test . Attributes ;
51
52
52
53
namespace iText . Kernel . Pdf {
53
54
/// <author>Michael Demey</author>
@@ -67,10 +68,11 @@ public static void BeforeClass() {
67
68
[ NUnit . Framework . Test ]
68
69
public virtual void ChangeIdTest ( ) {
69
70
MemoryStream baos = new MemoryStream ( ) ;
70
- PdfWriter writer = new PdfWriter ( baos ) ;
71
- PdfDocument pdfDocument = new PdfDocument ( writer ) ;
72
71
String value = "Modified ID 1234" ;
73
- writer . properties . SetModifiedDocumentId ( new PdfString ( value ) ) ;
72
+ WriterProperties writerProperties = new WriterProperties ( ) ;
73
+ writerProperties . SetModifiedDocumentId ( new PdfString ( ( value ) ) ) ;
74
+ PdfWriter writer = new PdfWriter ( baos , writerProperties ) ;
75
+ PdfDocument pdfDocument = new PdfDocument ( writer ) ;
74
76
pdfDocument . AddNewPage ( ) ;
75
77
pdfDocument . Close ( ) ;
76
78
byte [ ] documentBytes = baos . ToArray ( ) ;
@@ -156,5 +158,200 @@ public virtual void ChangeIdTest03() {
156
158
extractedModifiedValue = idArray . GetAsString ( 1 ) ;
157
159
NUnit . Framework . Assert . AreNotEqual ( modifiedId , extractedModifiedValue ) ;
158
160
}
161
+
162
+ /// <exception cref="System.IO.IOException"/>
163
+ [ NUnit . Framework . Test ]
164
+ public virtual void FetchReaderIdTest ( ) {
165
+ MemoryStream baos = new MemoryStream ( ) ;
166
+ IDigest md5 ;
167
+ try {
168
+ md5 = DigestUtilities . GetDigest ( "MD5" ) ;
169
+ }
170
+ catch ( Exception e ) {
171
+ throw new PdfException ( e ) ;
172
+ }
173
+ PdfString initialId = new PdfString ( md5 . Digest ( "Initial ID 56789" . GetBytes ( ) ) ) ;
174
+ PdfWriter writer = new PdfWriter ( baos , new WriterProperties ( ) . SetInitialDocumentId ( initialId ) ) ;
175
+ PdfDocument pdfDocument = new PdfDocument ( writer ) ;
176
+ pdfDocument . AddNewPage ( ) ;
177
+ pdfDocument . Close ( ) ;
178
+ byte [ ] documentBytes = baos . ToArray ( ) ;
179
+ baos . Dispose ( ) ;
180
+ PdfReader reader = new PdfReader ( new MemoryStream ( documentBytes ) ) ;
181
+ pdfDocument = new PdfDocument ( reader ) ;
182
+ String firstOriginalId = iText . IO . Util . JavaUtil . GetStringForBytes ( reader . GetOriginalFileId ( ) ) ;
183
+ String secondOriginalId = iText . IO . Util . JavaUtil . GetStringForBytes ( reader . GetOriginalFileId ( ) ) ;
184
+ String firstModifiedId = iText . IO . Util . JavaUtil . GetStringForBytes ( reader . GetModifiedFileId ( ) ) ;
185
+ String secondModifiedId = iText . IO . Util . JavaUtil . GetStringForBytes ( reader . GetModifiedFileId ( ) ) ;
186
+ NUnit . Framework . Assert . AreEqual ( firstOriginalId , secondOriginalId ) ;
187
+ NUnit . Framework . Assert . AreEqual ( firstModifiedId , secondModifiedId ) ;
188
+ }
189
+
190
+ /// <exception cref="System.IO.IOException"/>
191
+ [ NUnit . Framework . Test ]
192
+ public virtual void WriterPropertiesPriorityTest ( ) {
193
+ MemoryStream baos = new MemoryStream ( ) ;
194
+ IDigest md5 ;
195
+ try {
196
+ md5 = DigestUtilities . GetDigest ( "MD5" ) ;
197
+ }
198
+ catch ( Exception e ) {
199
+ throw new PdfException ( e ) ;
200
+ }
201
+ PdfString originalId = new PdfString ( md5 . Digest ( "Initial ID 01234" . GetBytes ( ) ) ) ;
202
+ PdfString modifiedId = new PdfString ( md5 . Digest ( "Modified ID 56789" . GetBytes ( ) ) ) ;
203
+ PdfWriter writer = new PdfWriter ( baos , new WriterProperties ( ) . SetInitialDocumentId ( originalId ) . SetModifiedDocumentId
204
+ ( modifiedId ) ) ;
205
+ PdfDocument pdfDocument = new PdfDocument ( writer ) ;
206
+ pdfDocument . AddNewPage ( ) ;
207
+ pdfDocument . Close ( ) ;
208
+ byte [ ] documentBytes = baos . ToArray ( ) ;
209
+ baos . Dispose ( ) ;
210
+ PdfString newOriginalId = new PdfString ( md5 . Digest ( "Initial ID 98765" . GetBytes ( ) ) ) ;
211
+ PdfString newModifiedId = new PdfString ( md5 . Digest ( "Modified ID 43210" . GetBytes ( ) ) ) ;
212
+ PdfReader reader = new PdfReader ( new MemoryStream ( documentBytes ) ) ;
213
+ PdfWriter newWriter = new PdfWriter ( new MemoryStream ( ) , new WriterProperties ( ) . SetInitialDocumentId ( newOriginalId
214
+ ) . SetModifiedDocumentId ( newModifiedId ) ) ;
215
+ pdfDocument = new PdfDocument ( reader , newWriter ) ;
216
+ String extractedOriginalId = pdfDocument . GetOriginalDocumentId ( ) . GetValue ( ) ;
217
+ String extractedModifiedId = pdfDocument . GetModifiedDocumentId ( ) . GetValue ( ) ;
218
+ pdfDocument . Close ( ) ;
219
+ NUnit . Framework . Assert . AreEqual ( extractedOriginalId , newOriginalId . GetValue ( ) ) ;
220
+ NUnit . Framework . Assert . AreEqual ( extractedModifiedId , newModifiedId . GetValue ( ) ) ;
221
+ }
222
+
223
+ /// <exception cref="System.IO.IOException"/>
224
+ [ NUnit . Framework . Test ]
225
+ public virtual void ReadPdfWithTwoStringIdsTest ( ) {
226
+ PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( sourceFolder + "pdfWithTwoStringIds.pdf" ) ) ;
227
+ String originalId = null ;
228
+ String modifiedId = null ;
229
+ if ( pdfDocument . GetOriginalDocumentId ( ) != null ) {
230
+ originalId = pdfDocument . GetOriginalDocumentId ( ) . GetValue ( ) ;
231
+ }
232
+ if ( pdfDocument . GetModifiedDocumentId ( ) != null ) {
233
+ modifiedId = pdfDocument . GetModifiedDocumentId ( ) . GetValue ( ) ;
234
+ }
235
+ pdfDocument . Close ( ) ;
236
+ NUnit . Framework . Assert . IsNotNull ( originalId ) ;
237
+ NUnit . Framework . Assert . IsNotNull ( modifiedId ) ;
238
+ }
239
+
240
+ /// <exception cref="System.IO.IOException"/>
241
+ [ NUnit . Framework . Test ]
242
+ [ LogMessage ( iText . IO . LogMessageConstant . DOCUMENT_IDS_ARE_CORRUPTED ) ]
243
+ public virtual void ReadPdfWithTwoNumberIdsTest ( ) {
244
+ PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( sourceFolder + "pdfWithTwoNumberIds.pdf" ) ) ;
245
+ String originalId = null ;
246
+ String modifiedId = null ;
247
+ if ( pdfDocument . GetOriginalDocumentId ( ) != null ) {
248
+ originalId = pdfDocument . GetOriginalDocumentId ( ) . GetValue ( ) ;
249
+ }
250
+ if ( pdfDocument . GetModifiedDocumentId ( ) != null ) {
251
+ modifiedId = pdfDocument . GetModifiedDocumentId ( ) . GetValue ( ) ;
252
+ }
253
+ pdfDocument . Close ( ) ;
254
+ NUnit . Framework . Assert . IsNull ( originalId ) ;
255
+ NUnit . Framework . Assert . IsNull ( modifiedId ) ;
256
+ }
257
+
258
+ /// <exception cref="System.IO.IOException"/>
259
+ [ NUnit . Framework . Test ]
260
+ [ LogMessage ( iText . IO . LogMessageConstant . DOCUMENT_IDS_ARE_CORRUPTED ) ]
261
+ public virtual void ReadPdfWithOneNumberOneStringIdsTest ( ) {
262
+ PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( sourceFolder + "pdfWithOneNumberOneStringIds.pdf" )
263
+ ) ;
264
+ String originalId = null ;
265
+ String modifiedId = null ;
266
+ if ( pdfDocument . GetOriginalDocumentId ( ) != null ) {
267
+ originalId = pdfDocument . GetOriginalDocumentId ( ) . GetValue ( ) ;
268
+ }
269
+ if ( pdfDocument . GetModifiedDocumentId ( ) != null ) {
270
+ modifiedId = pdfDocument . GetModifiedDocumentId ( ) . GetValue ( ) ;
271
+ }
272
+ pdfDocument . Close ( ) ;
273
+ NUnit . Framework . Assert . IsNull ( originalId ) ;
274
+ NUnit . Framework . Assert . IsNotNull ( modifiedId ) ;
275
+ }
276
+
277
+ /// <exception cref="System.IO.IOException"/>
278
+ [ NUnit . Framework . Test ]
279
+ [ LogMessage ( iText . IO . LogMessageConstant . DOCUMENT_IDS_ARE_CORRUPTED ) ]
280
+ public virtual void ReadPdfWithOneStringIdValueTest ( ) {
281
+ PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( sourceFolder + "pdfWithOneStringId.pdf" ) ) ;
282
+ String originalId = null ;
283
+ String modifiedId = null ;
284
+ if ( pdfDocument . GetOriginalDocumentId ( ) != null ) {
285
+ originalId = pdfDocument . GetOriginalDocumentId ( ) . GetValue ( ) ;
286
+ }
287
+ if ( pdfDocument . GetModifiedDocumentId ( ) != null ) {
288
+ modifiedId = pdfDocument . GetModifiedDocumentId ( ) . GetValue ( ) ;
289
+ }
290
+ pdfDocument . Close ( ) ;
291
+ NUnit . Framework . Assert . IsNull ( originalId ) ;
292
+ NUnit . Framework . Assert . IsNull ( modifiedId ) ;
293
+ }
294
+
295
+ /// <exception cref="System.IO.IOException"/>
296
+ [ NUnit . Framework . Test ]
297
+ [ LogMessage ( iText . IO . LogMessageConstant . DOCUMENT_IDS_ARE_CORRUPTED ) ]
298
+ public virtual void ReadPdfWithOneNumberIdValueTest ( ) {
299
+ PdfDocument pdfDocument = new PdfDocument ( new PdfReader ( sourceFolder + "pdfWithOneNumberId.pdf" ) ) ;
300
+ String originalId = null ;
301
+ String modifiedId = null ;
302
+ if ( pdfDocument . GetOriginalDocumentId ( ) != null ) {
303
+ originalId = pdfDocument . GetOriginalDocumentId ( ) . GetValue ( ) ;
304
+ }
305
+ if ( pdfDocument . GetModifiedDocumentId ( ) != null ) {
306
+ modifiedId = pdfDocument . GetModifiedDocumentId ( ) . GetValue ( ) ;
307
+ }
308
+ pdfDocument . Close ( ) ;
309
+ NUnit . Framework . Assert . IsNull ( originalId ) ;
310
+ NUnit . Framework . Assert . IsNull ( modifiedId ) ;
311
+ }
312
+
313
+ /// <exception cref="System.IO.IOException"/>
314
+ [ NUnit . Framework . Test ]
315
+ [ LogMessage ( iText . IO . LogMessageConstant . DOCUMENT_IDS_ARE_CORRUPTED ) ]
316
+ public virtual void ReadPdfWithNoIdTest ( ) {
317
+ PdfReader reader = new PdfReader ( sourceFolder + "pdfWithNoId.pdf" ) ;
318
+ PdfDocument pdfDocument = new PdfDocument ( reader ) ;
319
+ String originalId = null ;
320
+ String modifiedId = null ;
321
+ if ( pdfDocument . GetOriginalDocumentId ( ) != null ) {
322
+ originalId = pdfDocument . GetOriginalDocumentId ( ) . GetValue ( ) ;
323
+ }
324
+ if ( pdfDocument . GetModifiedDocumentId ( ) != null ) {
325
+ modifiedId = pdfDocument . GetModifiedDocumentId ( ) . GetValue ( ) ;
326
+ }
327
+ pdfDocument . Close ( ) ;
328
+ NUnit . Framework . Assert . IsNull ( originalId ) ;
329
+ NUnit . Framework . Assert . IsNull ( modifiedId ) ;
330
+ NUnit . Framework . Assert . AreEqual ( 0 , reader . GetOriginalFileId ( ) . Length ) ;
331
+ NUnit . Framework . Assert . AreEqual ( 0 , reader . GetModifiedFileId ( ) . Length ) ;
332
+ }
333
+ // @Test
334
+ // public void appendModeTest() {
335
+ // String originalId;
336
+ // String newOriginalId;
337
+ // String appendModeNewOriginalId;
338
+ // ByteArrayOutputStream baos = new ByteArrayOutputStream();
339
+ // PdfWriter initialWriter = new PdfWriter(baos, new WriterProperties().setInitialDocumentId(originalId));
340
+ // PdfWriter newWriter = new PdfWriter(baos, new WriterProperties().setInitialDocumentId(newOriginalId));
341
+ // PdfWriter appendModeWriter = new PdfWriter(baos, new WriterProperties().setInitialDocumentId(appendModeNewOriginalId));
342
+ //
343
+ //
344
+ // }
345
+ //
346
+ // @Test
347
+ // public void encryptionAes128Test() {
348
+ // ByteArrayOutputStream baos = new ByteArrayOutputStream();
349
+ // PdfString originalId = new PdfString("Original ID 56789");
350
+ // PdfWriter initialWriter = new PdfWriter(baos, new WriterProperties().setInitialDocumentId(originalId));
351
+ //
352
+ // Assert.assertNotEquals();
353
+ // Assert.assertEquals();
354
+ //
355
+ // }
159
356
}
160
357
}
0 commit comments