public virtual void CopySignedDocuments() { PdfDocument pdfDoc1 = new PdfDocument(new PdfReader(sourceFolder + "hello_signed.pdf")); PdfDocument pdfDoc2 = new PdfDocument(new PdfWriter(destinationFolder + "copySignedDocuments.pdf")); pdfDoc1.CopyPagesTo(1, 1, pdfDoc2); pdfDoc2.Close(); pdfDoc1.Close(); PdfDocument pdfDocument = new PdfDocument(new PdfReader(destinationFolder + "copySignedDocuments.pdf")); PdfDictionary sig = (PdfDictionary)pdfDocument.GetPdfObject(13); PdfDictionary sigRef = sig.GetAsArray(PdfName.Reference).GetAsDictionary(0); NUnit.Framework.Assert.IsTrue(PdfName.SigRef.Equals(sigRef.GetAsName(PdfName.Type))); NUnit.Framework.Assert.IsTrue(sigRef.Get(PdfName.Data).IsNull()); }
public virtual void RemovePageWithFormFieldsTest() { String filename = sourceFolder + "docWithFields.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(filename)); pdfDoc.RemovePage(1); PdfArray fields = pdfDoc.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm).GetAsArray(PdfName. Fields); PdfDictionary field = (PdfDictionary)fields.Get(0); PdfDictionary kid = (PdfDictionary)field.GetAsArray(PdfName.Kids).Get(0); NUnit.Framework.Assert.AreEqual(6, kid.KeySet().Count); NUnit.Framework.Assert.AreEqual(3, fields.Size()); pdfDoc.Close(); }
public virtual void ReadEncryptedDocumentWithFullCompression() { PdfReader reader = new PdfReader(sourceFolder + "source.pdf", new ReaderProperties().SetPassword("123".GetBytes ())); PdfDocument pdfDocument = new PdfDocument(reader); PdfDictionary form = pdfDocument.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm); PdfDictionary field = form.GetAsArray(PdfName.Fields).GetAsDictionary(0); NUnit.Framework.Assert.AreEqual("ch", field.GetAsString(PdfName.T).ToUnicodeString()); NUnit.Framework.Assert.AreEqual("SomeStringValueInDictionary", field.GetAsDictionary(new PdfName("TestDic" )).GetAsString(new PdfName("TestString")).ToUnicodeString()); NUnit.Framework.Assert.AreEqual("SomeStringValueInArray", field.GetAsArray(new PdfName("TestArray")).GetAsString (0).ToUnicodeString()); pdfDocument.Close(); }
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(); } }
public virtual void CopySignedDocuments() { FileStream fis1 = new FileStream(sourceFolder + "hello_signed.pdf", FileMode.Open, FileAccess.Read); PdfReader reader1 = new PdfReader(fis1); PdfDocument pdfDoc1 = new PdfDocument(reader1); FileStream fos2 = new FileStream(destinationFolder + "copySignedDocuments.pdf", FileMode.Create); PdfDocument pdfDoc2 = new PdfDocument(new PdfWriter(fos2)); pdfDoc1.CopyPagesTo(1, 1, pdfDoc2); pdfDoc2.Close(); pdfDoc1.Close(); PdfDocument pdfDocument = new PdfDocument(new PdfReader(destinationFolder + "copySignedDocuments.pdf")); PdfDictionary sig = (PdfDictionary)pdfDocument.GetPdfObject(13); PdfDictionary sigRef = sig.GetAsArray(PdfName.Reference).GetAsDictionary(0); NUnit.Framework.Assert.IsTrue(PdfName.SigRef.Equals(sigRef.GetAsName(PdfName.Type))); NUnit.Framework.Assert.IsTrue(sigRef.Get(PdfName.Data).IsNull()); }
private static void GetUsedNonFlushedOCGsFromOcDict(PdfObject toObj, PdfObject fromObj, ICollection<PdfIndirectReference > fromUsedOcgs, PdfDictionary toOcProperties) { if (toObj != null && toObj.IsDictionary() && !toObj.IsFlushed()) { PdfDictionary toCurrDict = (PdfDictionary)toObj; PdfDictionary fromCurrDict = (PdfDictionary)fromObj; PdfName typeName = toCurrDict.GetAsName(PdfName.Type); if (PdfName.OCG.Equals(typeName) && !iText.Kernel.Pdf.OcgPropertiesCopier.OcgAlreadyInOCGs(toCurrDict.GetIndirectReference (), toOcProperties)) { fromUsedOcgs.Add(fromCurrDict.GetIndirectReference()); } else { if (PdfName.OCMD.Equals(typeName)) { PdfArray toOcgs = null; PdfArray fromOcgs = null; if (toCurrDict.GetAsDictionary(PdfName.OCGs) != null) { toOcgs = new PdfArray(); toOcgs.Add(toCurrDict.GetAsDictionary(PdfName.OCGs)); fromOcgs = new PdfArray(); fromOcgs.Add(fromCurrDict.GetAsDictionary(PdfName.OCGs)); } else { if (toCurrDict.GetAsArray(PdfName.OCGs) != null) { toOcgs = toCurrDict.GetAsArray(PdfName.OCGs); fromOcgs = fromCurrDict.GetAsArray(PdfName.OCGs); } } if (toOcgs != null && !toOcgs.IsFlushed()) { for (int i = 0; i < toOcgs.Size(); i++) { iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromOcDict(toOcgs.Get(i), fromOcgs.Get(i), fromUsedOcgs , toOcProperties); } } } } } }
public virtual void CopyObject2() { FileStream fos1 = new FileStream(destinationFolder + "copyObject2_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(); PdfName aDirectName = new PdfName("aDirect"); PdfArray aDirect = ((PdfArray) new PdfArray().MakeIndirect(pdfDoc1)); List <PdfObject> tmpArray = new List <PdfObject>(2); tmpArray.Add(new PdfNumber(1)); tmpArray.Add(((PdfNumber) new PdfNumber(2).MakeIndirect(pdfDoc1))); 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")] = ((PdfNumber) new PdfNumber(2).MakeIndirect(pdfDoc1)); aDirect.Add(new PdfDictionary(tmpMap)); aDirect.Add(new PdfName("name")); aDirect.Add(((PdfNull) new PdfNull().MakeIndirect(pdfDoc1))); aDirect.Add(new PdfNumber(100)); aDirect.Add(new PdfString("string")); catalog1.Put(aDirectName, aDirect); pdfDoc1.Close(); PdfDocument pdfDoc1R = new PdfDocument(new PdfReader(destinationFolder + "copyObject2_1.pdf")); aDirect = (PdfArray)pdfDoc1R.GetCatalog().GetPdfObject().Get(aDirectName); FileStream fos2 = new FileStream(destinationFolder + "copyObject2_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(aDirectName, ((PdfArray)aDirect.CopyTo(pdfDoc2))); pdfDoc1R.Close(); pdfDoc2.Close(); PdfReader reader = new PdfReader(destinationFolder + "copyObject2_2.pdf"); PdfDocument pdfDocument = new PdfDocument(reader); NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt"); PdfDictionary catalog = pdfDocument.GetCatalog().GetPdfObject(); PdfArray a = catalog.GetAsArray(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, ((PdfArray)a.Get(0)).GetAsNumber(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, ((PdfDictionary)a.Get(2)).GetAsNumber(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(); }
private void LoadPage(int pageNum) { PdfDictionary targetPage = pageRefs[pageNum]; if (targetPage != null) { return; } //if we go here, we have to split PdfPages that contains pageNum int parentIndex = FindPageParent(pageNum); PdfPages parent = parents[parentIndex]; PdfArray kids = parent.GetKids(); if (kids == null) { throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1); } int kidsCount = parent.GetCount(); // we should handle separated pages, it means every PdfArray kids must contain either PdfPage or PdfPages, // mix of PdfPage and PdfPages not allowed. bool findPdfPages = false; // NOTE optimization? when we already found needed index for (int i = 0; i < kids.Size(); i++) { PdfDictionary page = kids.GetAsDictionary(i); if (page == null) { // null values not allowed in pages tree. throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1); } PdfObject pageKids = page.Get(PdfName.Kids); if (pageKids != null) { if (pageKids.GetObjectType() == PdfObject.ARRAY) { findPdfPages = true; } else { // kids must be of type array throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1); } } } if (findPdfPages) { // handle mix of PdfPage and PdfPages. // handle count property! IList <PdfPages> newParents = new List <PdfPages>(kids.Size()); PdfPages lastPdfPages = null; for (int i_1 = 0; i_1 < kids.Size() && kidsCount > 0; i_1++) { PdfDictionary pdfPagesObject = kids.GetAsDictionary(i_1); if (pdfPagesObject.GetAsArray(PdfName.Kids) == null) { // pdfPagesObject is PdfPage if (lastPdfPages == null) { // possible if only first kid is PdfPage lastPdfPages = new PdfPages(parent.GetFrom(), document, parent); kids.Set(i_1, lastPdfPages.GetPdfObject()); newParents.Add(lastPdfPages); } lastPdfPages.AddPage(pdfPagesObject); kids.Remove(i_1); i_1--; kidsCount--; } else { // pdfPagesObject is PdfPages int from = lastPdfPages == null?parent.GetFrom() : lastPdfPages.GetFrom() + lastPdfPages.GetCount(); lastPdfPages = new PdfPages(from, kidsCount, pdfPagesObject, parent); newParents.Add(lastPdfPages); kidsCount -= lastPdfPages.GetCount(); } } parents.JRemoveAt(parentIndex); for (int i_2 = newParents.Count - 1; i_2 >= 0; i_2--) { parents.Add(parentIndex, newParents[i_2]); } // recursive call, to load needed pageRef. // NOTE optimization? add to loadPage startParentIndex. LoadPage(pageNum); } else { int from = parent.GetFrom(); // Possible exception in case kids.getSize() < parent.getCount(). // In any case parent.getCount() has higher priority. // NOTE optimization? when we already found needed index for (int i_1 = 0; i_1 < parent.GetCount(); i_1++) { pageRefs[from + i_1] = kids.GetAsDictionary(i_1); } } }
private void LoadPage(int pageNum) { PdfIndirectReference targetPage = pageRefs[pageNum]; if (targetPage != null) { return; } //if we go here, we have to split PdfPages that contains pageNum int parentIndex = FindPageParent(pageNum); PdfPages parent = parents[parentIndex]; PdfArray kids = parent.GetKids(); if (kids == null) { throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1); } int kidsCount = parent.GetCount(); // we should handle separated pages, it means every PdfArray kids must contain either PdfPage or PdfPages, // mix of PdfPage and PdfPages not allowed. bool findPdfPages = false; // NOTE optimization? when we already found needed index for (int i = 0; i < kids.Size(); i++) { PdfDictionary page = kids.GetAsDictionary(i); // null values not allowed in pages tree. if (page == null) { throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1); } PdfObject pageKids = page.Get(PdfName.Kids); if (pageKids != null) { if (pageKids.IsArray()) { findPdfPages = true; } else { // kids must be of type array throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1); } } if (document.GetReader().IsMemorySavingMode() && !findPdfPages && parent.GetFrom() + i != pageNum) { page.Release(); } } if (findPdfPages) { // handle mix of PdfPage and PdfPages. // handle count property! IList <PdfPages> newParents = new List <PdfPages>(kids.Size()); PdfPages lastPdfPages = null; for (int i = 0; i < kids.Size() && kidsCount > 0; i++) { /* * We don't release pdfPagesObject in the end of each loop because we enter this for-cycle only when parent has PdfPages kids. * If all of the kids are PdfPages, then there's nothing to release, because we don't release PdfPages at this point. * If there are kids that are instances of PdfPage, then there's no sense in releasing them: * in this case ParentTreeStructure is being rebuilt by inserting an intermediate PdfPages between the parent and a PdfPage, * thus modifying the page object by resetting its parent, thus making it impossible to release the object. */ PdfDictionary pdfPagesObject = kids.GetAsDictionary(i); if (pdfPagesObject.GetAsArray(PdfName.Kids) == null) { // pdfPagesObject is PdfPage // possible if only first kid is PdfPage if (lastPdfPages == null) { lastPdfPages = new PdfPages(parent.GetFrom(), document, parent); kids.Set(i, lastPdfPages.GetPdfObject()); newParents.Add(lastPdfPages); } else { // Only remove from kids if we did not replace the entry with new PdfPages kids.Remove(i); i--; } // decrement count first so that page is not counted twice when moved to lastPdfPages parent.DecrementCount(); lastPdfPages.AddPage(pdfPagesObject); kidsCount--; } else { // pdfPagesObject is PdfPages int from = lastPdfPages == null?parent.GetFrom() : lastPdfPages.GetFrom() + lastPdfPages.GetCount(); lastPdfPages = new PdfPages(from, kidsCount, pdfPagesObject, parent); newParents.Add(lastPdfPages); kidsCount -= lastPdfPages.GetCount(); } } parents.JRemoveAt(parentIndex); for (int i = newParents.Count - 1; i >= 0; i--) { parents.Add(parentIndex, newParents[i]); } // recursive call, to load needed pageRef. // NOTE optimization? add to loadPage startParentIndex. LoadPage(pageNum); } else { int from = parent.GetFrom(); // Possible exception in case kids.getSize() < parent.getCount(). // In any case parent.getCount() has higher priority. // NOTE optimization? when we already found needed index for (int i = 0; i < parent.GetCount(); i++) { PdfObject kid = kids.Get(i, false); if (kid is PdfIndirectReference) { pageRefs[from + i] = (PdfIndirectReference)kid; } else { pageRefs[from + i] = kid.GetIndirectReference(); } } } }
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); } }