While using PDFBox 1.5.0 I had the need to insert a child as the first child of an outline but there was no way to do this. You can only append to the outline.

After some investigation I determined that the new code needed to go into "PDOutlineNode.java" in package "org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline".

The following is the code for a new method I wrote and tested. Since I can not commit the code I will leave that to someone who can. The code has been tested, but another pair of eyes never hurts.

********* Begin code for new method in PDOutlineNode.java *****************
    /**
     * Insert this node before the current first child node or as the only
     * node if there are no children.
     * @param outlineNode
     */
    public void insertFirstChild( PDOutlineItem outlineNode )
    {
        PDOutlineItem wasFirstNode = this.getFirstChild();
        int currentOpenCount = getOpenCount();
        int numberOfOpenNodesWeAreAdding = 1;
        outlineNode.setParent( this );
        PDOutlineItem lastAddedNode = outlineNode;
        while(lastAddedNode.getNextSibling() != null)
        {
            lastAddedNode = lastAddedNode.getNextSibling();
            lastAddedNode.setParent(this);
        }
        setFirstChild( outlineNode );
        //  1 for the the item we are adding;
        if( outlineNode.isNodeOpen() )
        {
            numberOfOpenNodesWeAreAdding += outlineNode.getOpenCount();
        }
        // now update the count like appendChild() does
        if( isNodeOpen() )
        {
setOpenCount( currentOpenCount + numberOfOpenNodesWeAreAdding );
        }
        else
        {
setOpenCount( currentOpenCount - numberOfOpenNodesWeAreAdding );
        }
        // link the 2 subsets together
        // end of added list -> to first of old set
        lastAddedNode.setNextSibling(wasFirstNode);
        // first of old set -> back to last of added set
        wasFirstNode.setPreviousSibling(lastAddedNode);
        // tell the parent about the new node(s) we just added
        updateParentOpenCount( numberOfOpenNodesWeAreAdding );
        // update last if there were no child nodes when we started.
        if ( wasFirstNode == null )
          setLastChild( lastAddedNode );
        // else previous last is still the last one
    }

********* end code *****************

--

Charles Simon
Principal Software Engineer
Select Business Solutions, Inc.
35 Nutmeg Drive
Trumbull, CT 06611
http://www.selectbs.com

Reply via email to