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 IList <PdfAnnotation> GetAnnotations() { IList <PdfAnnotation> annotations = new List <PdfAnnotation>(); PdfArray annots = GetPdfObject().GetAsArray(PdfName.Annots); if (annots != null) { for (int i = 0; i < annots.Size(); i++) { PdfDictionary annot = annots.GetAsDictionary(i); annotations.Add(PdfAnnotation.MakeAnnotation(annot).SetPage(this)); } } return(annotations); }
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_"); }
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 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); }
private PdfNumber IterateItems(PdfDictionary dictionary, PdfNumber leftOver) { PdfArray nums = dictionary.GetAsArray(PdfName.Nums); if (nums != null) { for (int k = 0; k < nums.Size(); k++) { PdfNumber number; if (leftOver == null) { number = nums.GetAsNumber(k++); } else { number = leftOver; leftOver = null; } if (k < nums.Size()) { items.Put(number.IntValue(), nums.Get(k)); } else { return(number); } } } else { if ((nums = dictionary.GetAsArray(PdfName.Kids)) != null) { for (int k = 0; k < nums.Size(); k++) { PdfDictionary kid = nums.GetAsDictionary(k); leftOver = IterateItems(kid, leftOver); } } } return(null); }
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_"); }
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); }
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(); } } } }