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 bool OrderBranchContainsSetElements(PdfObject arrayObj, PdfArray array, int currentIndex, ICollection <PdfIndirectReference> ocgs, PdfArray toOcgs, PdfDocument toDocument) { if (arrayObj.IsDictionary()) { if (ocgs.Contains(arrayObj.GetIndirectReference())) { return true; } else { if (currentIndex < (array.Size() - 1) && array.Get(currentIndex + 1).IsArray()) { PdfArray nextArray = array.GetAsArray(currentIndex + 1); if (!nextArray.Get(0).IsString()) { bool result = iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(nextArray, array, currentIndex + 1, ocgs, toOcgs, toDocument); if (result && toOcgs != null && !ocgs.Contains(arrayObj.GetIndirectReference())) { // Add the OCG to the OCGs array to register the OCG in document, since it is not used // directly in the document, but is used as a parent for the order group. If it is not added // to the OCGs array, then the OCG will be deleted at the 3rd stage of the /Order entry coping. toOcgs.Add(arrayObj.CopyTo(toDocument, false)); } return result; } } } } else { if (arrayObj.IsArray()) { PdfArray arrayItem = (PdfArray)arrayObj; for (int i = 0; i < arrayItem.Size(); i++) { PdfObject obj = arrayItem.Get(i); if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(obj, arrayItem, i, ocgs, toOcgs, toDocument )) { return true; } } if (!arrayItem.IsEmpty() && !arrayItem.Get(0).IsString()) { if (currentIndex > 0 && array.Get(currentIndex - 1).IsDictionary()) { PdfDictionary previousDict = (PdfDictionary)array.Get(currentIndex - 1); return ocgs.Contains(previousDict.GetIndirectReference()); } } } } return false; }