private void MarkArrayContentToFlush(PdfArray array) { for (int i = 0; i < array.Size(); i++) { MarkObjectToFlush(array.Get(i, false)); } }
public virtual void TestContains() { PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream())); PdfArray array = new PdfArray(); array.Add(new PdfNumber(1).MakeIndirect(doc).GetIndirectReference()); array.Add(new PdfNumber(2).MakeIndirect(doc).GetIndirectReference()); array.Add(new PdfNumber(3).MakeIndirect(doc)); array.Add(new PdfNumber(4).MakeIndirect(doc)); array.Add(new PdfNumber(5)); array.Add(new PdfNumber(6)); PdfArray array2 = new PdfArray(); array2.Add(new PdfNumber(1).MakeIndirect(doc).GetIndirectReference()); array2.Add(new PdfNumber(2).MakeIndirect(doc).GetIndirectReference()); array2.Add(new PdfNumber(3).MakeIndirect(doc)); array2.Add(new PdfNumber(4).MakeIndirect(doc)); array2.Add(new PdfNumber(5)); array2.Add(new PdfNumber(6)); foreach (PdfObject obj in array2) { NUnit.Framework.Assert.IsTrue(array.Contains(obj)); } for (int i = 0; i < array2.Size(); i++) { NUnit.Framework.Assert.IsTrue(array.Contains(array2.Get(i))); } }
public virtual void TestRemove2() { PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream())); PdfArray array = new PdfArray(); array.Add(new PdfNumber(1).MakeIndirect(doc).GetIndirectReference()); array.Add(new PdfNumber(2).MakeIndirect(doc).GetIndirectReference()); array.Add(new PdfNumber(3).MakeIndirect(doc)); array.Add(new PdfNumber(4).MakeIndirect(doc)); array.Add(new PdfNumber(5)); array.Add(new PdfNumber(6)); PdfArray array2 = new PdfArray(); array2.Add(new PdfNumber(1).MakeIndirect(doc).GetIndirectReference()); array2.Add(new PdfNumber(2).MakeIndirect(doc).GetIndirectReference()); array2.Add(new PdfNumber(3).MakeIndirect(doc)); array2.Add(new PdfNumber(4).MakeIndirect(doc)); array2.Add(new PdfNumber(5)); array2.Add(new PdfNumber(6)); for (int i = 0; i < array2.Size(); i++) { array.Remove(array2.Get(i)); } NUnit.Framework.Assert.AreEqual(0, array.Size()); }
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); }
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; }
public virtual void PdfNumberInPageContentArrayTest() { String src = sourceFolder + "pdfNumberInPageContentArray.pdf"; String dest = destinationFolder + "pdfNumberInPageContentArray_saved.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); pdfDoc.Close(); // test is mainly to ensure document is successfully opened-and-closed without exceptions pdfDoc = new PdfDocument(new PdfReader(dest)); PdfObject pageDictWithInvalidContents = pdfDoc.GetPdfObject(10); PdfArray invalidContentsArray = ((PdfDictionary)pageDictWithInvalidContents).GetAsArray(PdfName.Contents); NUnit.Framework.Assert.AreEqual(5, invalidContentsArray.Size()); NUnit.Framework.Assert.IsFalse(invalidContentsArray.Get(0).IsStream()); NUnit.Framework.Assert.IsFalse(invalidContentsArray.Get(1).IsStream()); NUnit.Framework.Assert.IsFalse(invalidContentsArray.Get(2).IsStream()); NUnit.Framework.Assert.IsFalse(invalidContentsArray.Get(3).IsStream()); NUnit.Framework.Assert.IsTrue(invalidContentsArray.Get(4).IsStream()); }
public virtual void PdtIndirectReferenceLateInitializing3() { MemoryStream baos = new MemoryStream(); PdfWriter writer = new PdfWriter(baos); PdfDocument document = new PdfDocument(writer); document.AddNewPage(); PdfDictionary catalog = document.GetCatalog().GetPdfObject(); PdfIndirectReference indRef1 = document.CreateNextIndirectReference(); PdfIndirectReference indRef2 = document.CreateNextIndirectReference(); PdfArray array = new PdfArray(); catalog.Put(new PdfName("array1"), array); PdfString @string = new PdfString("array string"); array.Add(@string); array.Add(indRef1); array.Add(indRef2); PdfDictionary dict = new PdfDictionary(); dict.MakeIndirect(document, indRef1); PdfArray arrayClone = (PdfArray)array.Clone(); PdfObject object0 = arrayClone.Get(0, false); PdfObject object1 = arrayClone.Get(1, false); PdfObject object2 = arrayClone.Get(2, false); NUnit.Framework.Assert.IsTrue(object0 is PdfString); NUnit.Framework.Assert.IsTrue(object1 is PdfDictionary); NUnit.Framework.Assert.IsTrue(object2 is PdfNull); PdfString string1 = (PdfString)object0; NUnit.Framework.Assert.IsTrue(@string != string1); NUnit.Framework.Assert.IsTrue(@string.GetValue().Equals(string1.GetValue())); PdfDictionary dict1 = (PdfDictionary)object1; NUnit.Framework.Assert.IsTrue(dict1.GetIndirectReference().GetObjNumber() == dict.GetIndirectReference().GetObjNumber ()); NUnit.Framework.Assert.IsTrue(dict1.GetIndirectReference().GetGenNumber() == dict.GetIndirectReference().GetGenNumber ()); NUnit.Framework.Assert.IsTrue(dict1 == dict); document.Close(); }
private void SerArray(PdfArray array, int level, ByteBufferOutputStream bb) { bb.Append("$A"); if (level <= 0) { return; } for (int k = 0; k < array.Size(); ++k) { SerObject(array.Get(k, false), level, bb); } }
private static int GetAmountOfReadPages(PdfArray pageIndRefArray) { int amountOfLoadedPages = 0; for (int i = 0; i < pageIndRefArray.Size(); i++) { if (((PdfIndirectReference)pageIndRefArray.Get(i, false)).refersTo != null) { amountOfLoadedPages++; } } return(amountOfLoadedPages); }
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()); }
private void SerArray(PdfArray array, ByteBuffer bb, int level, IDictionary <PdfIndirectReference, byte[]> serializedCache) { bb.Append("$A"); if (level <= 0) { return; } for (int k = 0; k < array.Size(); ++k) { SerObject(array.Get(k, false), bb, level, serializedCache); } bb.Append("$\\A"); }
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 TagStructureFlushingTest03() { PdfReader reader = new PdfReader(sourceFolder + "taggedDocument.pdf"); PdfWriter writer = new PdfWriter(destinationFolder + "tagStructureFlushingTest03.pdf"); writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION); PdfDocument document = new PdfDocument(reader, writer); document.GetPage(2).Flush(); document.GetPage(1).Flush(); PdfArray kids = document.GetStructTreeRoot().GetKidsObject(); NUnit.Framework.Assert.IsFalse(kids.Get(0).IsFlushed()); NUnit.Framework.Assert.IsTrue(kids.GetAsDictionary(0).GetAsDictionary(PdfName.K).IsFlushed()); document.Close(); CompareResult("tagStructureFlushingTest03.pdf", "taggedDocument.pdf", "diffFlushing03_"); }
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) { PdfArray array = new PdfArray((PdfArray)dest); array.Set(0, page2page.Get(oldPage).GetPdfObject()); d = new PdfExplicitDestination(array); } } } else { if (dest.IsString()) { PdfNameTree destsTree = GetNameTree(PdfName.Dests); IDictionary <String, PdfObject> dests = destsTree.GetNames(); String name = ((PdfString)dest).ToUnicodeString(); PdfArray array = (PdfArray)dests.Get(name); if (array != null) { PdfObject pageObject = array.Get(0); foreach (PdfPage oldPage in page2page.Keys) { if (oldPage.GetPdfObject() == pageObject) { array.Set(0, page2page.Get(oldPage).GetPdfObject()); d = new PdfStringDestination(name); toDocument.AddNamedDestination(name, array); } } } } } return(d); }
private PdfString IterateItems(PdfDictionary dictionary, IDictionary <String, PdfObject> items, PdfString leftOver ) { PdfArray names = dictionary.GetAsArray(PdfName.Names); if (names != null) { for (int k = 0; k < names.Size(); k++) { PdfString name; if (leftOver == null) { name = names.GetAsString(k++); } else { name = leftOver; leftOver = null; } if (k < names.Size()) { items.Put(name.ToUnicodeString(), names.Get(k)); } else { return(name); } } } else { if ((names = dictionary.GetAsArray(PdfName.Kids)) != null) { for (int k = 0; k < names.Size(); k++) { PdfDictionary kid = names.GetAsDictionary(k); leftOver = IterateItems(kid, items, leftOver); } } } return(null); }
public virtual void TagStructureFlushingTest02() { PdfReader reader = new PdfReader(sourceFolder + "taggedDocument.pdf"); PdfWriter writer = new PdfWriter(destinationFolder + "tagStructureFlushingTest02.pdf"); writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION); PdfDocument document = new PdfDocument(reader, writer); TagStructureContext tagStructure = document.GetTagStructureContext(); tagStructure.FlushPageTags(document.GetPage(1)); IList <IStructureNode> kids = document.GetStructTreeRoot().GetKids(); NUnit.Framework.Assert.IsTrue(!((PdfStructElem)kids[0]).GetPdfObject().IsFlushed()); NUnit.Framework.Assert.IsTrue(!((PdfStructElem)kids[0].GetKids()[0]).GetPdfObject().IsFlushed()); PdfArray rowsTags = (PdfArray)((PdfStructElem)kids[0].GetKids()[0]).GetK(); NUnit.Framework.Assert.IsTrue(rowsTags.Get(0).IsFlushed()); document.Close(); CompareResult("tagStructureFlushingTest02.pdf", "taggedDocument.pdf", "diffFlushing02_"); }
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 TagStructureFlushingTest04() { PdfReader reader = new PdfReader(sourceFolder + "taggedDocument.pdf"); PdfWriter writer = new PdfWriter(destinationFolder + "tagStructureFlushingTest04.pdf"); writer.SetCompressionLevel(CompressionConstants.NO_COMPRESSION); PdfDocument document = new PdfDocument(reader, writer); TagTreePointer tagPointer = new TagTreePointer(document); tagPointer.MoveToKid(StandardRoles.TABLE).MoveToKid(2, StandardRoles.TR).FlushTag(); // intended redundant call to flush page tags separately from page. Page tags are flushed when the page is flushed. document.GetTagStructureContext().FlushPageTags(document.GetPage(1)); document.GetPage(1).Flush(); tagPointer.MoveToKid(5).FlushTag(); document.GetPage(2).Flush(); PdfArray kids = document.GetStructTreeRoot().GetKidsObject(); NUnit.Framework.Assert.IsFalse(kids.Get(0).IsFlushed()); NUnit.Framework.Assert.IsTrue(kids.GetAsDictionary(0).GetAsDictionary(PdfName.K).IsFlushed()); document.Close(); CompareResult("tagStructureFlushingTest04.pdf", "taggedDocument.pdf", "diffFlushing04_"); }
private void Write(PdfArray pdfArray) { WriteByte('['); for (int i = 0; i < pdfArray.Size(); i++) { PdfObject value = pdfArray.Get(i, false); PdfIndirectReference indirectReference; if ((indirectReference = value.GetIndirectReference()) != null) { Write(indirectReference); } else { Write(value); } if (i < pdfArray.Size() - 1) { WriteSpace(); } } WriteByte(']'); }
protected internal virtual bool CheckEncryption(PdfStream pdfStream) { if (crypto == null || crypto.IsEmbeddedFilesOnly()) { return(false); } else { if (IsXRefStream(pdfStream)) { // The cross-reference stream shall not be encrypted return(false); } else { PdfObject filter = pdfStream.Get(PdfName.Filter, true); if (filter != null) { if (PdfName.Crypt.Equals(filter)) { return(false); } else { if (filter.GetObjectType() == PdfObject.ARRAY) { PdfArray filters = (PdfArray)filter; if (!filters.IsEmpty() && PdfName.Crypt.Equals(filters.Get(0, true))) { return(false); } } } } return(true); } } }
public virtual void TestValuesIndirectContains() { PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream())); PdfArray array = new PdfArray(); array.Add(new PdfNumber(0).MakeIndirect(doc).GetIndirectReference()); array.Add(new PdfNumber(1).MakeIndirect(doc).GetIndirectReference()); array.Add(new PdfNumber(2).MakeIndirect(doc)); array.Add(new PdfNumber(3).MakeIndirect(doc)); array.Add(new PdfNumber(4)); array.Add(new PdfNumber(5)); NUnit.Framework.Assert.IsTrue(array.Contains(array.Get(0, false))); NUnit.Framework.Assert.IsTrue(array.Contains(array.Get(1, false))); NUnit.Framework.Assert.IsTrue(array.Contains(array.Get(2).GetIndirectReference())); NUnit.Framework.Assert.IsTrue(array.Contains(array.Get(3).GetIndirectReference())); NUnit.Framework.Assert.IsTrue(array.Contains(array.Get(4))); NUnit.Framework.Assert.IsTrue(array.Contains(array.Get(5))); }
public virtual void TestValuesIndirectRemove() { PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream())); PdfArray array = new PdfArray(); array.Add(new PdfNumber(1).MakeIndirect(doc).GetIndirectReference()); array.Add(new PdfNumber(2).MakeIndirect(doc).GetIndirectReference()); array.Add(new PdfNumber(3).MakeIndirect(doc)); array.Add(new PdfNumber(4).MakeIndirect(doc)); array.Add(new PdfNumber(5)); array.Add(new PdfNumber(6)); array.Remove(array.Get(0, false)); array.Remove(array.Get(0, false)); array.Remove(array.Get(0).GetIndirectReference()); array.Remove(array.Get(0).GetIndirectReference()); array.Remove(array.Get(0)); array.Remove(array.Get(0)); NUnit.Framework.Assert.AreEqual(0, array.Size()); }
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); } }
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(); } } } }
protected internal virtual byte[] DecodeFlateBytes(PdfStream stream, byte[] bytes) { PdfObject filterObject = stream.Get(PdfName.Filter); if (filterObject == null) { return(bytes); } // check if flateDecode filter is on top PdfName filterName; PdfArray filtersArray = null; if (filterObject is PdfName) { filterName = (PdfName)filterObject; } else { if (filterObject is PdfArray) { filtersArray = (PdfArray)filterObject; filterName = filtersArray.GetAsName(0); } else { throw new PdfException(PdfException.FilterIsNotANameOrArray); } } if (!PdfName.FlateDecode.Equals(filterName)) { return(bytes); } // get decode params if present PdfDictionary decodeParams; PdfArray decodeParamsArray = null; PdfObject decodeParamsObject = stream.Get(PdfName.DecodeParms); if (decodeParamsObject == null) { decodeParams = null; } else { if (decodeParamsObject.GetObjectType() == PdfObject.DICTIONARY) { decodeParams = (PdfDictionary)decodeParamsObject; } else { if (decodeParamsObject.GetObjectType() == PdfObject.ARRAY) { decodeParamsArray = (PdfArray)decodeParamsObject; decodeParams = decodeParamsArray.GetAsDictionary(0); } else { throw new PdfException(PdfException.DecodeParameterType1IsNotSupported).SetMessageParams(decodeParamsObject .GetType().ToString()); } } } // decode byte[] res = FlateDecodeFilter.FlateDecode(bytes, true); if (res == null) { res = FlateDecodeFilter.FlateDecode(bytes, false); } bytes = FlateDecodeFilter.DecodePredictor(res, decodeParams); //remove filter and decode params filterObject = null; if (filtersArray != null) { filtersArray.Remove(0); if (filtersArray.Size() == 1) { filterObject = filtersArray.Get(0); } else { if (!filtersArray.IsEmpty()) { filterObject = filtersArray; } } } decodeParamsObject = null; if (decodeParamsArray != null) { decodeParamsArray.Remove(0); if (decodeParamsArray.Size() == 1 && decodeParamsArray.Get(0).GetObjectType() != PdfObject.NULL) { decodeParamsObject = decodeParamsArray.Get(0); } else { if (!decodeParamsArray.IsEmpty()) { decodeParamsObject = decodeParamsArray; } } } if (filterObject == null) { stream.Remove(PdfName.Filter); } else { stream.Put(PdfName.Filter, filterObject); } if (decodeParamsObject == null) { stream.Remove(PdfName.DecodeParms); } else { stream.Put(PdfName.DecodeParms, decodeParamsObject); } return(bytes); }