public virtual void ChangeIdTest03() { MemoryStream baosInitial = new MemoryStream(); MemoryStream baosModified = new MemoryStream(); IDigest md5; try { md5 = DigestUtilities.GetDigest("MD5"); } catch (Exception e) { throw new PdfException(e); } PdfString initialId = new PdfString(md5.Digest("Initial ID 56789".GetBytes())); PdfString modifiedId = new PdfString("Modified ID 56789"); PdfWriter writer = new PdfWriter(baosInitial, new WriterProperties().SetInitialDocumentId(initialId).SetModifiedDocumentId (modifiedId)); PdfDocument pdfDocument = new PdfDocument(writer); pdfDocument.AddNewPage(); pdfDocument.Close(); PdfReader reader = new PdfReader(new RandomAccessSourceFactory().CreateSource(baosInitial.ToArray()), new ReaderProperties()); pdfDocument = new PdfDocument(reader); PdfArray idArray = pdfDocument.GetTrailer().GetAsArray(PdfName.ID); pdfDocument.Close(); NUnit.Framework.Assert.IsNotNull(idArray); PdfString extractedInitialValue = idArray.GetAsString(0); NUnit.Framework.Assert.AreEqual(initialId, extractedInitialValue); PdfString extractedModifiedValue = idArray.GetAsString(1); NUnit.Framework.Assert.AreEqual(modifiedId, extractedModifiedValue); pdfDocument = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().CreateSource(baosInitial.ToArray ()), new ReaderProperties()), new PdfWriter(baosModified)); new PdfCanvas(pdfDocument.AddNewPage()).SaveState().LineTo(100, 100).MoveTo(100, 100).Stroke().RestoreState (); pdfDocument.Close(); reader = new PdfReader(new RandomAccessSourceFactory().CreateSource(baosModified.ToArray()), new ReaderProperties ()); pdfDocument = new PdfDocument(reader); idArray = pdfDocument.GetTrailer().GetAsArray(PdfName.ID); pdfDocument.Close(); NUnit.Framework.Assert.IsNotNull(idArray); extractedInitialValue = idArray.GetAsString(0); NUnit.Framework.Assert.AreEqual(initialId, extractedInitialValue); extractedModifiedValue = idArray.GetAsString(1); NUnit.Framework.Assert.AreNotEqual(modifiedId, extractedModifiedValue); }
public virtual void ChangeIdTest02() { MemoryStream baos = new MemoryStream(); IDigest md5; try { md5 = DigestUtilities.GetDigest("MD5"); } catch (Exception e) { throw new PdfException(e); } PdfString initialId = new PdfString(md5.Digest("Initial ID 56789".GetBytes())); PdfWriter writer = new PdfWriter(baos, new WriterProperties().SetInitialDocumentId(initialId)); PdfDocument pdfDocument = new PdfDocument(writer); pdfDocument.AddNewPage(); pdfDocument.Close(); byte[] documentBytes = baos.ToArray(); baos.Dispose(); PdfReader reader = new PdfReader(new MemoryStream(documentBytes)); pdfDocument = new PdfDocument(reader); PdfArray idArray = pdfDocument.GetTrailer().GetAsArray(PdfName.ID); NUnit.Framework.Assert.IsNotNull(idArray); PdfString extractedString = idArray.GetAsString(1); pdfDocument.Close(); NUnit.Framework.Assert.AreEqual(initialId, extractedString); }
public virtual void ChangeIdTest() { MemoryStream baos = new MemoryStream(); String value = "Modified ID 1234"; WriterProperties writerProperties = new WriterProperties(); writerProperties.SetModifiedDocumentId(new PdfString((value))); PdfWriter writer = new PdfWriter(baos, writerProperties); PdfDocument pdfDocument = new PdfDocument(writer); pdfDocument.AddNewPage(); pdfDocument.Close(); byte[] documentBytes = baos.ToArray(); baos.Dispose(); PdfReader reader = new PdfReader(new MemoryStream(documentBytes)); pdfDocument = new PdfDocument(reader); PdfArray idArray = pdfDocument.GetTrailer().GetAsArray(PdfName.ID); NUnit.Framework.Assert.IsNotNull(idArray); String extractedValue = idArray.GetAsString(1).GetValue(); pdfDocument.Close(); NUnit.Framework.Assert.AreEqual(value, extractedValue); }
private PdfString IterateItems(PdfDictionary dictionary, IDictionary <String, PdfObject> items, PdfString leftOver ) { PdfArray names = dictionary.GetAsArray(PdfName.Names); if (names != null) { for (int k = 0; k < names.Size(); k++) { PdfString name; if (leftOver == null) { name = names.GetAsString(k++); } else { name = leftOver; leftOver = null; } if (k < names.Size()) { items.Put(name.ToUnicodeString(), names.Get(k)); } else { return(name); } } } else { if ((names = dictionary.GetAsArray(PdfName.Kids)) != null) { for (int k = 0; k < names.Size(); k++) { PdfDictionary kid = names.GetAsDictionary(k); leftOver = IterateItems(kid, items, leftOver); } } } return(null); }