public virtual void CopyObject1() { FileStream fos1 = new FileStream(destinationFolder + "copyObject1_1.pdf", FileMode.Create); PdfWriter writer1 = new PdfWriter(fos1); PdfDocument pdfDoc1 = new PdfDocument(writer1); PdfPage page1 = pdfDoc1.AddNewPage(); page1.Flush(); PdfDictionary catalog1 = pdfDoc1.GetCatalog().GetPdfObject(); PdfArray aDirect = new PdfArray(); List <PdfObject> tmpArray = new List <PdfObject>(2); tmpArray.Add(new PdfNumber(1)); tmpArray.Add(new PdfNumber(2)); aDirect.Add(new PdfArray(tmpArray)); aDirect.Add(new PdfBoolean(true)); SortedDictionary <PdfName, PdfObject> tmpMap = new SortedDictionary <PdfName, PdfObject>(); tmpMap[new PdfName("one")] = new PdfNumber(1); tmpMap[new PdfName("two")] = new PdfNumber(2); aDirect.Add(new PdfDictionary(tmpMap)); aDirect.Add(new PdfName("name")); aDirect.Add(new PdfNull()); aDirect.Add(new PdfNumber(100)); aDirect.Add(new PdfString("string")); catalog1.Put(new PdfName("aDirect"), aDirect); FileStream fos2 = new FileStream(destinationFolder + "copyObject1_2.pdf", FileMode.Create); PdfWriter writer2 = new PdfWriter(fos2); PdfDocument pdfDoc2 = new PdfDocument(writer2); PdfPage page2 = pdfDoc2.AddNewPage(); page2.Flush(); PdfDictionary catalog2 = pdfDoc2.GetCatalog().GetPdfObject(); catalog2.Put(new PdfName("aDirect"), ((PdfArray)aDirect.CopyTo(pdfDoc2))); pdfDoc1.Close(); pdfDoc2.Close(); PdfReader reader = new PdfReader(destinationFolder + "copyObject1_2.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt"); PdfDictionary catalog = pdfDocument.GetCatalog().GetPdfObject(); PdfArray a = (PdfArray)catalog.Get(new PdfName("aDirect")); NUnit.Framework.Assert.IsNotNull(a); NUnit.Framework.Assert.AreEqual(1, ((PdfNumber)((PdfArray)a.Get(0)).Get(0)).IntValue()); NUnit.Framework.Assert.AreEqual(2, ((PdfNumber)((PdfArray)a.Get(0)).Get(1)).IntValue()); NUnit.Framework.Assert.AreEqual(true, ((PdfBoolean)a.Get(1)).GetValue()); NUnit.Framework.Assert.AreEqual(1, ((PdfNumber)((PdfDictionary)a.Get(2)).Get(new PdfName("one"))).IntValue ()); NUnit.Framework.Assert.AreEqual(2, ((PdfNumber)((PdfDictionary)a.Get(2)).Get(new PdfName("two"))).IntValue ()); NUnit.Framework.Assert.AreEqual(new PdfName("name"), a.Get(3)); NUnit.Framework.Assert.IsTrue(a.Get(4).IsNull()); NUnit.Framework.Assert.AreEqual(100, ((PdfNumber)a.Get(5)).IntValue()); NUnit.Framework.Assert.AreEqual("string", ((PdfString)a.Get(6)).ToUnicodeString()); reader.Close(); }
internal virtual PdfDestination CopyDestination(PdfObject dest, IDictionary <PdfPage, PdfPage> page2page, PdfDocument toDocument) { PdfDestination d = null; if (dest.IsArray()) { PdfObject pageObject = ((PdfArray)dest).Get(0); foreach (PdfPage oldPage in page2page.Keys) { if (oldPage.GetPdfObject() == pageObject) { // in the copiedArray old page ref will be correctly replaced by the new page ref as this page is already copied PdfArray copiedArray = (PdfArray)dest.CopyTo(toDocument, false); d = new PdfExplicitDestination(copiedArray); break; } } } else { if (dest.IsString() || dest.IsName()) { PdfNameTree destsTree = GetNameTree(PdfName.Dests); IDictionary <String, PdfObject> dests = destsTree.GetNames(); String srcDestName = dest.IsString() ? ((PdfString)dest).ToUnicodeString() : ((PdfName)dest).GetValue(); PdfArray srcDestArray = (PdfArray)dests.Get(srcDestName); if (srcDestArray != null) { PdfObject pageObject = srcDestArray.Get(0); if (pageObject is PdfNumber) { pageObject = GetDocument().GetPage(((PdfNumber)pageObject).IntValue() + 1).GetPdfObject(); } foreach (PdfPage oldPage in page2page.Keys) { if (oldPage.GetPdfObject() == pageObject) { d = new PdfStringDestination(srcDestName); if (!IsEqualSameNameDestExist(page2page, toDocument, srcDestName, srcDestArray, oldPage)) { // in the copiedArray old page ref will be correctly replaced by the new page ref as this page is already copied PdfArray copiedArray = (PdfArray)srcDestArray.CopyTo(toDocument, false); // here we can safely replace first item of the array because array of NamedDestination or StringDestination // never refers to page in another document via PdfNumber, but should always refer to page within current document // via page object reference. copiedArray.Set(0, page2page.Get(oldPage).GetPdfObject()); toDocument.AddNamedDestination(srcDestName, copiedArray); } break; } } } } } return(d); }
internal virtual PdfDestination CopyDestination(PdfObject dest, IDictionary <PdfPage, PdfPage> page2page, PdfDocument toDocument) { PdfDestination d = null; if (dest.IsArray()) { PdfObject pageObject = ((PdfArray)dest).Get(0); foreach (PdfPage oldPage in page2page.Keys) { if (oldPage.GetPdfObject() == pageObject) { // in the copiedArray old page ref will be correctly replaced by the new page ref as this page is already copied PdfArray copiedArray = (PdfArray)dest.CopyTo(toDocument, false); d = new PdfExplicitDestination(copiedArray); break; } } } else { if (dest.IsString()) { PdfNameTree destsTree = GetNameTree(PdfName.Dests); IDictionary <String, PdfObject> dests = destsTree.GetNames(); String srcDestName = ((PdfString)dest).ToUnicodeString(); PdfArray srcDestArray = (PdfArray)dests.Get(srcDestName); if (srcDestArray != null) { PdfObject pageObject = srcDestArray.Get(0); foreach (PdfPage oldPage in page2page.Keys) { if (oldPage.GetPdfObject() == pageObject) { d = new PdfStringDestination(srcDestName); if (!IsEqualSameNameDestExist(page2page, toDocument, srcDestName, srcDestArray, oldPage)) { // in the copiedArray old page ref will be correctly replaced by the new page ref as this page is already copied PdfArray copiedArray = (PdfArray)srcDestArray.CopyTo(toDocument, false); toDocument.AddNamedDestination(srcDestName, copiedArray); } break; } } } } } return(d); }
public virtual void CopyObject3() { { FileStream fos1 = new FileStream(destinationFolder + "copyObject3_1.pdf", FileMode.Create); PdfWriter writer1 = new PdfWriter(fos1); PdfDocument pdfDoc1 = new PdfDocument(writer1); PdfPage page1 = pdfDoc1.AddNewPage(); page1.Flush(); PdfDictionary catalog1 = pdfDoc1.GetCatalog().GetPdfObject(); PdfArray arr1 = ((PdfArray) new PdfArray().MakeIndirect(pdfDoc1)); PdfArray arr2 = ((PdfArray) new PdfArray().MakeIndirect(pdfDoc1)); arr1.Add(arr2); PdfDictionary dic1 = ((PdfDictionary) new PdfDictionary().MakeIndirect(pdfDoc1)); arr2.Add(dic1); PdfDictionary dic2 = ((PdfDictionary) new PdfDictionary().MakeIndirect(pdfDoc1)); dic1.Put(new PdfName("dic2"), dic2); PdfName arr1Name = new PdfName("arr1"); dic2.Put(arr1Name, arr1); catalog1.Put(arr1Name, arr1); pdfDoc1.Close(); PdfDocument pdfDoc1R = new PdfDocument(new PdfReader(destinationFolder + "copyObject3_1.pdf")); arr1 = (PdfArray)pdfDoc1R.GetCatalog().GetPdfObject().Get(arr1Name); FileStream fos2 = new FileStream(destinationFolder + "copyObject3_2.pdf", FileMode.Create); PdfWriter writer2 = new PdfWriter(fos2); PdfDocument pdfDoc2 = new PdfDocument(writer2); PdfPage page2 = pdfDoc2.AddNewPage(); page2.Flush(); PdfDictionary catalog2 = pdfDoc2.GetCatalog().GetPdfObject(); catalog2.Put(arr1Name, ((PdfArray)arr1.CopyTo(pdfDoc2))); pdfDoc1R.Close(); pdfDoc2.Close(); } { PdfReader reader = new PdfReader(destinationFolder + "copyObject3_2.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt"); PdfDictionary catalog = pdfDocument.GetCatalog().GetPdfObject(); PdfArray arr1 = catalog.GetAsArray(new PdfName("arr1")); PdfArray arr2 = arr1.GetAsArray(0); PdfDictionary dic1 = arr2.GetAsDictionary(0); PdfDictionary dic2 = dic1.GetAsDictionary(new PdfName("dic2")); NUnit.Framework.Assert.AreEqual(arr1, dic2.GetAsArray(new PdfName("arr1"))); reader.Close(); } }
private static void CopyDArrayField(PdfName fieldToCopy, ICollection<PdfIndirectReference> fromOcgsToCopy, PdfDictionary fromDict, PdfDictionary toDict, PdfDocument toDocument) { if (fromDict.GetAsArray(fieldToCopy) == null) { return; } PdfArray fromArray = fromDict.GetAsArray(fieldToCopy); if (toDict.GetAsArray(fieldToCopy) == null) { toDict.Put(fieldToCopy, new PdfArray()); } PdfArray toArray = toDict.GetAsArray(fieldToCopy); ICollection<PdfIndirectReference> toOcgsToCopy = new HashSet<PdfIndirectReference>(); foreach (PdfIndirectReference fromRef in fromOcgsToCopy) { toOcgsToCopy.Add(fromRef.GetRefersTo().CopyTo(toDocument, false).GetIndirectReference()); } if (PdfName.Order.Equals(fieldToCopy)) { // Stage 1: delete all Order the entire branches from the output document in which the copied OCGs were IList<int> removeIndex = new List<int>(); for (int i = 0; i < toArray.Size(); i++) { PdfObject toOrderItem = toArray.Get(i); if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(toOrderItem, toArray, i, toOcgsToCopy , null, null)) { removeIndex.Add(i); } } for (int i = removeIndex.Count - 1; i > -1; i--) { toArray.Remove(removeIndex[i]); } PdfArray toOcgs = toDocument.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.OCProperties).GetAsArray( PdfName.OCGs); // Stage 2: copy all the Order the entire branches in which the copied OСGs were for (int i = 0; i < fromArray.Size(); i++) { PdfObject fromOrderItem = fromArray.Get(i); if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(fromOrderItem, fromArray, i, fromOcgsToCopy , toOcgs, toDocument)) { toArray.Add(fromOrderItem.CopyTo(toDocument, false)); } } } else { // Stage 3: remove from Order OCGs not presented in the output document. When forming // the Order dictionary in the PdfOcProperties constructor, only those OCGs that are // in the OCProperties/OCGs array will be taken into account if (PdfName.RBGroups.Equals(fieldToCopy)) { // Stage 1: delete all RBGroups from the output document in which the copied OCGs were for (int i = toArray.Size() - 1; i > -1; i--) { PdfArray toRbGroup = (PdfArray)toArray.Get(i); foreach (PdfObject toRbGroupItemObj in toRbGroup) { if (toOcgsToCopy.Contains(toRbGroupItemObj.GetIndirectReference())) { toArray.Remove(i); break; } } } // Stage 2: copy all the RBGroups in which the copied OCGs were foreach (PdfObject fromRbGroupObj in fromArray) { PdfArray fromRbGroup = (PdfArray)fromRbGroupObj; foreach (PdfObject fromRbGroupItemObj in fromRbGroup) { if (fromOcgsToCopy.Contains(fromRbGroupItemObj.GetIndirectReference())) { toArray.Add(fromRbGroup.CopyTo(toDocument, false)); break; } } } } else { // Stage 3: remove from RBGroups OCGs not presented in the output // document (is in the PdfOcProperties#fillDictionary method) foreach (PdfObject fromObj in fromArray) { iText.Kernel.Pdf.OcgPropertiesCopier.AttemptToAddObjectToArray(fromOcgsToCopy, fromObj, toArray, toDocument ); } } } if (toArray.IsEmpty()) { toDict.Remove(fieldToCopy); } }