/** * Copyright (c) 2003, www.pdfbox.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of pdfbox; nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * http://www.pdfbox.org * */ package org.pdfbox.pdmodel; import org.pdfbox.cos.COSArray; import org.pdfbox.cos.COSBase; import org.pdfbox.cos.COSDictionary; import org.pdfbox.cos.COSName; import org.pdfbox.cos.COSNumber; import org.pdfbox.cos.COSInteger; import org.pdfbox.pdmodel.common.COSArrayList; import org.pdfbox.pdmodel.common.COSObjectable; import org.pdfbox.pdmodel.common.PDRectangle; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * This represents a page node in a pdf document. * * @author Ben Litchfield (ben@csh.rit.edu) * @version $Revision: 1.6 $ */ public class PDPageNode implements COSObjectable { private COSDictionary page; /** * Creates a new instance of PDPage. */ public PDPageNode() { page = new COSDictionary(); page.setItem( COSName.TYPE, COSName.PAGES ); page.setItem( COSName.KIDS, new COSArray() ); page.setItem( COSName.COUNT, new COSInteger( 0 ) ); } /** * Creates a new instance of PDPage. * * @param pages The dictionary pages. */ public PDPageNode( COSDictionary pages ) { page = pages; } /** * This will update the count attribute of the page node. This only needs to * be called if you add or remove pages. The PDDocument will call this for you * when you use the PDDocumnet persistence methods. So, basically most clients * will never need to call this. * * @return The update count for this node. */ public long updateCount() { long totalCount = 0; List kids = getKids(); Iterator kidIter = kids.iterator(); while( kidIter.hasNext() ) { Object next = kidIter.next(); if( next instanceof PDPage ) { totalCount++; } else { PDPageNode node = (PDPageNode)next; totalCount += node.updateCount(); } } page.setItem( COSName.COUNT, new COSInteger( totalCount ) ); return totalCount; } /** * This will get the count of descendent page objects. * * @return The total number of descendent page objects. */ public long getCount() { return ((COSNumber)page.getDictionaryObject( COSName.COUNT )).intValue(); } /** * This will get the underlying dictionary that this class acts on. * * @return The underlying dictionary for this class. */ public COSDictionary getDictionary() { return page; } /** * This is the parent page node. * * @return The parent to this page. */ public PDPageNode getParent() { PDPageNode parent = null; COSDictionary parentDic = (COSDictionary)page.getDictionaryObject( COSName.PARENT ); if( parentDic != null ) { parent = new PDPageNode( parentDic ); } return parent; } /** * This will set the parent of this page. * * @param parent The parent to this page node. */ public void setParent( PDPageNode parent ) { page.setItem( COSName.PARENT, parent.getDictionary() ); } /** * @see COSObjectable#getCOSObject() */ public COSBase getCOSObject() { return page; } /** * This will return all kids of this node, either PDPageNode or PDPage. * * @return All direct descendents of this node. */ public List getKids() { List actuals = new ArrayList(); COSArray kids = getAllKids(actuals, page, false); return new COSArrayList( actuals, kids ); } /** * This will return all kids of this node as PDPage. * * @param result All direct and indirect descendents of this node are added to this list. */ public void getAllKids(List result) { getAllKids(result, page, true); } /** * This will return all kids of the given page node as PDPage. * * @param result All direct and optionally indirect descendents of this node are added to this list. * @param page Page dictionary of a page node. * @param recurse if true indirect descendents are processed recursively */ private static COSArray getAllKids(List result, COSDictionary page, boolean recurse) { COSArray kids = (COSArray)page.getDictionaryObject( COSName.KIDS ); for( int i=0; i