public virtual void TestCreateAndUpdateTwiceXMP() { String created = destinationFolder + "testCreateAndUpdateTwiceXMP_create.pdf"; String updated = destinationFolder + "testCreateAndUpdateTwiceXMP_update.pdf"; String updatedAgain = destinationFolder + "testCreateAndUpdateTwiceXMP_updatedAgain.pdf"; PdfDocument pdfDocument = new PdfDocument(new PdfWriter(created)); pdfDocument.AddNewPage(); pdfDocument.GetXmpMetadata(true); // create XMP metadata pdfDocument.Close(); pdfDocument = new PdfDocument(new PdfReader(created), new PdfWriter(updated)); PdfDictionary catalog = pdfDocument.GetCatalog().GetPdfObject(); ((PdfIndirectReference)catalog.Remove(PdfName.Metadata)).SetFree(); pdfDocument.Close(); pdfDocument = new PdfDocument(new PdfReader(updated), new PdfWriter(updatedAgain)); catalog = pdfDocument.GetCatalog().GetPdfObject(); ((PdfIndirectReference)catalog.Remove(PdfName.Metadata)).SetFree(); PdfXrefTable xref = pdfDocument.GetXref(); PdfIndirectReference ref0 = xref.Get(0); PdfIndirectReference freeRef1 = xref.Get(6); PdfIndirectReference freeRef2 = xref.Get(7); pdfDocument.Close(); /* * Current xref structure: * xref * 0 9 * 0000000006 65535 f % this is object 0; 6 refers to free object 6 * 0000000203 00000 n * 0000000510 00000 n * 0000000263 00000 n * 0000000088 00000 n * 0000000015 00000 n * 0000000007 00001 f % this is object 6; 7 refers to free object 7; note generation number * 0000000000 00001 f % this is object 7; 0 refers to free object 0; note generation number * 0000000561 00000 n */ NUnit.Framework.Assert.IsTrue(freeRef1.IsFree()); NUnit.Framework.Assert.AreEqual(ref0.offsetOrIndex, freeRef1.objNr); NUnit.Framework.Assert.AreEqual(1, freeRef1.genNr); NUnit.Framework.Assert.IsTrue(freeRef2.IsFree()); NUnit.Framework.Assert.AreEqual(freeRef1.offsetOrIndex, freeRef2.objNr); NUnit.Framework.Assert.AreEqual(1, freeRef2.genNr); pdfDocument.Close(); }
/// <summary>Flushes all modified objects which have not been flushed yet.</summary> /// <remarks>Flushes all modified objects which have not been flushed yet. Used in case incremental updates.</remarks> /// <param name="forbiddenToFlush"> /// a /// <see cref="Java.Util.Set{E}"/> /// of /// <see cref="PdfIndirectReference">references</see> /// that are forbidden to be flushed automatically. /// </param> protected internal virtual void FlushModifiedWaitingObjects(ICollection <PdfIndirectReference> forbiddenToFlush ) { PdfXrefTable xref = document.GetXref(); for (int i = 1; i < xref.Size(); i++) { PdfIndirectReference indirectReference = xref.Get(i); if (null != indirectReference && !indirectReference.IsFree() && !forbiddenToFlush.Contains(indirectReference )) { bool isModified = indirectReference.CheckState(PdfObject.MODIFIED); if (isModified) { PdfObject obj = indirectReference.GetRefersTo(false); if (obj != null) { if (!obj.Equals(objectStream)) { obj.Flush(); } } } } } if (objectStream != null && objectStream.GetSize() > 0) { objectStream.Flush(); objectStream = null; } }
/// <summary>Flushes all objects which have not been flushed yet.</summary> /// <param name="forbiddenToFlush"> /// a /// <see cref="Java.Util.Set{E}"/> /// of /// <see cref="PdfIndirectReference">references</see> /// that are forbidden to be flushed automatically. /// </param> protected internal virtual void FlushWaitingObjects(ICollection <PdfIndirectReference> forbiddenToFlush) { PdfXrefTable xref = document.GetXref(); bool needFlush = true; while (needFlush) { needFlush = false; for (int i = 1; i < xref.Size(); i++) { PdfIndirectReference indirectReference = xref.Get(i); if (indirectReference != null && !indirectReference.IsFree() && indirectReference.CheckState(PdfObject.MUST_BE_FLUSHED ) && !forbiddenToFlush.Contains(indirectReference)) { PdfObject obj = indirectReference.GetRefersTo(false); if (obj != null) { obj.Flush(); needFlush = true; } } } } if (objectStream != null && objectStream.GetSize() > 0) { objectStream.Flush(); objectStream = null; } }
public virtual void CheckNumberOfIndirectObjectsWithFreeReferencesTest() { PdfXrefTable table = new PdfXrefTable(); int numberOfReferences = 10; for (int i = 0; i < numberOfReferences; i++) { table.Add(new PdfIndirectReference(null, i + 1)); } table.InitFreeReferencesList(null); int freeReferenceNumber = 5; table.FreeReference(table.Get(freeReferenceNumber)); NUnit.Framework.Assert.AreEqual(numberOfReferences - 1, table.GetCountOfIndirectObjects()); NUnit.Framework.Assert.IsTrue(table.Get(freeReferenceNumber).IsFree()); }
public virtual void FreeRefReuseWhenAddNewObjTest() { String filename = destinationFolder + "freeRefReuseWhenAddNewObj.pdf"; PdfDocument pdfDoc1 = new PdfDocument(new PdfReader(sourceFolder + "pdfWithRemovedObjInOldVer.pdf"), new PdfWriter (filename).SetCompressionLevel(CompressionConstants.NO_COMPRESSION), new StampingProperties().UseAppendMode ()); pdfDoc1.GetCatalog().GetPdfObject().Put(new PdfName("CustomKey"), new PdfArray().MakeIndirect(pdfDoc1)); PdfObject newObj = pdfDoc1.GetCatalog().GetPdfObject(); newObj.SetModified(); int expectObjNumber = pdfDoc1.GetCatalog().GetPdfObject().Get(new PdfName("CustomKey")).GetIndirectReference ().GetObjNumber(); int expectGenNumber = pdfDoc1.GetCatalog().GetPdfObject().Get(new PdfName("CustomKey")).GetIndirectReference ().GetGenNumber(); PdfXrefTable xref = pdfDoc1.GetXref(); NUnit.Framework.Assert.AreEqual(8, expectObjNumber); NUnit.Framework.Assert.AreEqual(0, expectGenNumber); NUnit.Framework.Assert.IsTrue(xref.Get(5).IsFree()); pdfDoc1.Close(); }
/// <summary>Flushes all modified objects which have not been flushed yet.</summary> /// <remarks>Flushes all modified objects which have not been flushed yet. Used in case incremental updates.</remarks> protected internal virtual void FlushModifiedWaitingObjects() { PdfXrefTable xref = document.GetXref(); for (int i = 1; i < xref.Size(); i++) { PdfIndirectReference indirectReference = xref.Get(i); if (null != indirectReference) { PdfObject obj = indirectReference.GetRefersTo(false); if (obj != null && !obj.Equals(objectStream) && obj.IsModified()) { obj.Flush(); } } } if (objectStream != null && objectStream.GetSize() > 0) { objectStream.Flush(); objectStream = null; } }