Package org.apache.pdfbox.pdmodel
Class PDPageTree
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.PDPageTree
-
- All Implemented Interfaces:
java.lang.Iterable<PDPage>
,COSObjectable
public class PDPageTree extends java.lang.Object implements COSObjectable, java.lang.Iterable<PDPage>
The page tree, which defines the ordering of pages in the document in an efficient manner.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
PDPageTree.PageIterator
Iterator which walks all pages in the tree, in order.private static class
PDPageTree.SearchContext
-
Field Summary
Fields Modifier and Type Field Description private PDDocument
document
private static org.apache.commons.logging.Log
LOG
private COSDictionary
root
-
Constructor Summary
Constructors Constructor Description PDPageTree()
Constructor for embedding.PDPageTree(COSDictionary root)
Constructor for reading.PDPageTree(COSDictionary root, PDDocument document)
Constructor for reading.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(PDPage page)
Adds the given page to this page tree.private boolean
findPage(PDPageTree.SearchContext context, COSDictionary node)
PDPage
get(int index)
Returns the page at the given index.private COSDictionary
get(int pageNum, COSDictionary node, int encountered)
Returns the given COS page using a depth-first search.COSDictionary
getCOSObject()
Convert this standard java object to a COS object.int
getCount()
Returns the number of leaf nodes (page objects) that are descendants of this root within the page tree.static COSBase
getInheritableAttribute(COSDictionary node, COSName key)
Returns the given attribute, inheriting from parent tree nodes if necessary.private java.util.List<COSDictionary>
getKids(COSDictionary node)
Helper to get kids from malformed PDFs.private void
increaseParents(COSDictionary parentDict)
int
indexOf(PDPage page)
Returns the index of the given page, or -1 if it does not exist.void
insertAfter(PDPage newPage, PDPage prevPage)
Insert a page after another page within a page tree.void
insertBefore(PDPage newPage, PDPage nextPage)
Insert a page before another page within a page tree.private boolean
isPageTreeNode(COSDictionary node)
Returns true if the node is a page tree node (i.e.java.util.Iterator<PDPage>
iterator()
Returns an iterator which walks all pages in the tree, in order.void
remove(int index)
Removes the page with the given index from the page tree.private void
remove(COSDictionary node)
Removes the given COS page.void
remove(PDPage page)
Removes the given page from the page tree.private static void
sanitizeType(COSDictionary dictionary)
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
root
private final COSDictionary root
-
document
private final PDDocument document
-
-
Constructor Detail
-
PDPageTree
public PDPageTree()
Constructor for embedding.
-
PDPageTree
public PDPageTree(COSDictionary root)
Constructor for reading.- Parameters:
root
- A page tree root.
-
PDPageTree
PDPageTree(COSDictionary root, PDDocument document)
Constructor for reading.- Parameters:
root
- A page tree root.document
- The document which contains "root".
-
-
Method Detail
-
getInheritableAttribute
public static COSBase getInheritableAttribute(COSDictionary node, COSName key)
Returns the given attribute, inheriting from parent tree nodes if necessary.- Parameters:
node
- page objectkey
- the key to look up- Returns:
- COS value for the given key
-
iterator
public java.util.Iterator<PDPage> iterator()
Returns an iterator which walks all pages in the tree, in order.- Specified by:
iterator
in interfacejava.lang.Iterable<PDPage>
-
getKids
private java.util.List<COSDictionary> getKids(COSDictionary node)
Helper to get kids from malformed PDFs.- Parameters:
node
- page tree node- Returns:
- list of kids
-
get
public PDPage get(int index)
Returns the page at the given index.- Parameters:
index
- zero-based index- Throws:
java.lang.IllegalStateException
- if the requested index isn't found or doesn't point to a valid page dictionaryjava.lang.IndexOutOfBoundsException
- if the requested index is higher than the page count
-
sanitizeType
private static void sanitizeType(COSDictionary dictionary)
-
get
private COSDictionary get(int pageNum, COSDictionary node, int encountered)
Returns the given COS page using a depth-first search.- Parameters:
pageNum
- 1-based page numbernode
- page tree node to searchencountered
- number of pages encountered so far- Returns:
- COS dictionary of the Page object
- Throws:
java.lang.IllegalStateException
- if the requested page number isn't foundjava.lang.IndexOutOfBoundsException
- if the requested page number is higher than the page count
-
isPageTreeNode
private boolean isPageTreeNode(COSDictionary node)
Returns true if the node is a page tree node (i.e. and intermediate).
-
indexOf
public int indexOf(PDPage page)
Returns the index of the given page, or -1 if it does not exist.- Parameters:
page
- The page to search for.- Returns:
- the zero-based index of the given page, or -1 if the page is not found.
-
findPage
private boolean findPage(PDPageTree.SearchContext context, COSDictionary node)
-
getCount
public int getCount()
Returns the number of leaf nodes (page objects) that are descendants of this root within the page tree.- Returns:
- the number of leaf nodes.
-
getCOSObject
public COSDictionary getCOSObject()
Description copied from interface:COSObjectable
Convert this standard java object to a COS object.- Specified by:
getCOSObject
in interfaceCOSObjectable
- Returns:
- The cos object that matches this Java object.
-
remove
public void remove(int index)
Removes the page with the given index from the page tree.- Parameters:
index
- zero-based page index
-
remove
public void remove(PDPage page)
Removes the given page from the page tree.- Parameters:
page
- The page to remove.
-
remove
private void remove(COSDictionary node)
Removes the given COS page.
-
add
public void add(PDPage page)
Adds the given page to this page tree.- Parameters:
page
- The page to add.
-
insertBefore
public void insertBefore(PDPage newPage, PDPage nextPage)
Insert a page before another page within a page tree.- Parameters:
newPage
- the page to be inserted.nextPage
- the page that is to be after the new page.- Throws:
java.lang.IllegalArgumentException
- if one attempts to insert a page that isn't part of a page tree.
-
insertAfter
public void insertAfter(PDPage newPage, PDPage prevPage)
Insert a page after another page within a page tree.- Parameters:
newPage
- the page to be inserted.prevPage
- the page that is to be before the new page.- Throws:
java.lang.IllegalArgumentException
- if one attempts to insert a page that isn't part of a page tree.
-
increaseParents
private void increaseParents(COSDictionary parentDict)
-
-