public virtual void SmartModeSameResourcesCopyingAndFlushing() { String outFile = destinationFolder + "smartModeSameResourcesCopyingAndFlushing.pdf"; String cmpFile = sourceFolder + "cmp_smartModeSameResourcesCopyingAndFlushing.pdf"; String[] srcFiles = new String[] { sourceFolder + "indirectResourcesStructure.pdf", sourceFolder + "indirectResourcesStructure2.pdf" }; PdfDocument outputDoc = new PdfDocument(new PdfWriter(outFile, new WriterProperties().UseSmartMode())); foreach (String srcFile in srcFiles) { PdfDocument sourceDoc = new PdfDocument(new PdfReader(srcFile)); sourceDoc.CopyPagesTo(1, sourceDoc.GetNumberOfPages(), outputDoc); sourceDoc.Close(); outputDoc.FlushCopiedObjects(sourceDoc); } outputDoc.Close(); PdfDocument assertDoc = new PdfDocument(new PdfReader(outFile)); PdfIndirectReference page1ResFontObj = assertDoc.GetPage(1).GetPdfObject().GetAsDictionary(PdfName.Resources ).GetAsDictionary(PdfName.Font).GetIndirectReference(); PdfIndirectReference page2ResFontObj = assertDoc.GetPage(2).GetPdfObject().GetAsDictionary(PdfName.Resources ).GetAsDictionary(PdfName.Font).GetIndirectReference(); PdfIndirectReference page3ResFontObj = assertDoc.GetPage(3).GetPdfObject().GetAsDictionary(PdfName.Resources ).GetAsDictionary(PdfName.Font).GetIndirectReference(); NUnit.Framework.Assert.IsTrue(page1ResFontObj.Equals(page2ResFontObj)); NUnit.Framework.Assert.IsTrue(page1ResFontObj.Equals(page3ResFontObj)); assertDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder)); }
public virtual void BaseEqualsTest() { PdfIndirectReference reference = new PdfIndirectReference(null, 41, 0); NUnit.Framework.Assert.IsTrue(reference.Equals(reference)); NUnit.Framework.Assert.IsFalse(reference.Equals(null)); PdfIndirectReferenceTest.TestIndirectReference testIndirectReference = new PdfIndirectReferenceTest.TestIndirectReference (null, 41, 0); NUnit.Framework.Assert.IsFalse(reference.Equals(testIndirectReference)); NUnit.Framework.Assert.IsFalse(testIndirectReference.Equals(reference)); }
public virtual void EqualsWithNullDocsTest() { PdfIndirectReference obj41Gen0 = new PdfIndirectReference(null, 41, 0); NUnit.Framework.Assert.IsTrue(obj41Gen0.Equals(new PdfIndirectReference(null, 41, 0))); NUnit.Framework.Assert.IsFalse(obj41Gen0.Equals(new PdfIndirectReference(null, 42, 0))); NUnit.Framework.Assert.IsFalse(obj41Gen0.Equals(new PdfIndirectReference(null, 41, 1))); using (PdfDocument doc = new PdfDocument(new PdfWriter(new MemoryStream()))) { // add a page to avoid exception throwing on close doc.AddNewPage(); NUnit.Framework.Assert.IsFalse(obj41Gen0.Equals(new PdfIndirectReference(doc, 41, 0))); } }
private static bool OcgAlreadyInOCGs(PdfIndirectReference toOcgRef, PdfDictionary toOcProperties) { if (toOcProperties == null) { return false; } PdfArray toOcgs = toOcProperties.GetAsArray(PdfName.OCGs); if (toOcgs != null) { foreach (PdfObject toOcg in toOcgs) { if (toOcgRef.Equals(toOcg.GetIndirectReference())) { return true; } } } return false; }
private bool IsEqualSameNameDestExist(IDictionary <PdfPage, PdfPage> page2page, PdfDocument toDocument, String srcDestName, PdfArray srcDestArray, PdfPage oldPage) { PdfArray sameNameDest = (PdfArray)toDocument.GetCatalog().GetNameTree(PdfName.Dests).GetNames().Get(srcDestName ); bool equalSameNameDestExists = false; if (sameNameDest != null && sameNameDest.GetAsDictionary(0) != null) { PdfIndirectReference existingDestPageRef = sameNameDest.GetAsDictionary(0).GetIndirectReference(); PdfIndirectReference newDestPageRef = page2page.Get(oldPage).GetPdfObject().GetIndirectReference(); if (equalSameNameDestExists = existingDestPageRef.Equals(newDestPageRef) && sameNameDest.Size() == srcDestArray .Size()) { for (int i = 1; i < sameNameDest.Size(); ++i) { equalSameNameDestExists = equalSameNameDestExists && sameNameDest.Get(i).Equals(srcDestArray.Get(i)); } } } return(equalSameNameDestExists); }
public virtual void SmartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh() { String outFile = destinationFolder + "smartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh.pdf"; String cmpFile = sourceFolder + "cmp_smartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh.pdf"; String[] srcFiles = new String[] { sourceFolder + "indirectResourcesStructure.pdf", sourceFolder + "indirectResourcesStructure2.pdf" }; PdfDocument outputDoc = new PdfDocument(new PdfWriter(outFile, new WriterProperties().UseSmartMode())); int lastPageNum = 1; PdfFont font = PdfFontFactory.CreateFont(); foreach (String srcFile in srcFiles) { PdfDocument sourceDoc = new PdfDocument(new PdfReader(srcFile)); for (int i = 1; i <= sourceDoc.GetNumberOfPages(); ++i) { PdfDictionary srcRes = sourceDoc.GetPage(i).GetPdfObject().GetAsDictionary(PdfName.Resources); // Ensures that objects copied to the output document are fresh, // i.e. are not reused from already copied objects cache. bool ensureObjectIsFresh = true; // it's crucial to copy first inner objects and then the container object! foreach (PdfObject v in srcRes.Values()) { if (v.GetIndirectReference() != null) { // We are not interested in returned copied objects instances, they will be picked up by // general copying mechanism from copied objects cache by default. v.CopyTo(outputDoc, ensureObjectIsFresh); } } if (srcRes.GetIndirectReference() != null) { srcRes.CopyTo(outputDoc, ensureObjectIsFresh); } } sourceDoc.CopyPagesTo(1, sourceDoc.GetNumberOfPages(), outputDoc); sourceDoc.Close(); int i_1; for (i_1 = lastPageNum; i_1 <= outputDoc.GetNumberOfPages(); ++i_1) { PdfPage page = outputDoc.GetPage(i_1); PdfCanvas canvas = new PdfCanvas(page); canvas.BeginText().MoveText(36, 36).SetFontAndSize(font, 12).ShowText("Page " + i_1).EndText(); } lastPageNum = i_1; outputDoc.FlushCopiedObjects(sourceDoc); } outputDoc.Close(); PdfDocument assertDoc = new PdfDocument(new PdfReader(outFile)); PdfIndirectReference page1ResFontObj = assertDoc.GetPage(1).GetPdfObject().GetAsDictionary(PdfName.Resources ).GetAsDictionary(PdfName.Font).GetIndirectReference(); PdfIndirectReference page2ResFontObj = assertDoc.GetPage(2).GetPdfObject().GetAsDictionary(PdfName.Resources ).GetAsDictionary(PdfName.Font).GetIndirectReference(); PdfIndirectReference page3ResFontObj = assertDoc.GetPage(3).GetPdfObject().GetAsDictionary(PdfName.Resources ).GetAsDictionary(PdfName.Font).GetIndirectReference(); NUnit.Framework.Assert.IsFalse(page1ResFontObj.Equals(page2ResFontObj)); NUnit.Framework.Assert.IsFalse(page1ResFontObj.Equals(page3ResFontObj)); NUnit.Framework.Assert.IsFalse(page2ResFontObj.Equals(page3ResFontObj)); assertDoc.Close(); NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder)); }