/** * Writes the altered parent tree to a PdfWriter and updates the StructTreeRoot entry. * @param writer The writer to which the StructParents have to be written * @throws IOException */ public void WriteParentTree(PdfWriter writer) { if (structTreeRoot == null) { return; } int[] numbers = new int[parentTree.Count]; parentTree.Keys.CopyTo(numbers, 0); Array.Sort(numbers); structTreeRoot.Put(PdfName.PARENTTREENEXTKEY, new PdfNumber(numbers[numbers.Length - 1] + 1)); structTreeRoot.Put(PdfName.PARENTTREE, PdfNumberTree.WriteTree(parentTree, writer)); }
/** * Creates a list of StructuredItem objects. * @param reader the reader holding the PDF to examine */ public StructureItems(PdfReader reader) { PdfDictionary catalog = reader.Catalog; structTreeRoot = catalog.GetAsDict(PdfName.STRUCTTREEROOT); if (structTreeRoot == null) { throw new DocumentException(MessageLocalization.GetComposedMessage("can.t.read.document.structure")); } // Storing the parent tree parentTree = PdfNumberTree.ReadTree(structTreeRoot.GetAsDict(PdfName.PARENTTREE)); structTreeRoot.Remove(PdfName.STRUCTPARENTS); // Examining the StructTreeRoot PdfObject objecta = structTreeRoot.GetDirectObject(PdfName.K); if (objecta == null) { return; } switch (objecta.Type) { case PdfObject.DICTIONARY: LOGGER.Info("StructTreeRoot refers to dictionary"); ProcessStructElems((PdfDictionary)objecta, structTreeRoot.GetAsIndirectObject(PdfName.K)); break; case PdfObject.ARRAY: LOGGER.Info("StructTreeRoot refers to array"); PdfArray array = (PdfArray)objecta; for (int i = 0; i < array.Size; i++) { ProcessStructElems(array.GetAsDict(i), array.GetAsIndirectObject(i)); } break; } }