You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2015/10/13 21:19:03 UTC

svn commit: r1708488 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageTree.java

Author: tilman
Date: Tue Oct 13 19:19:02 2015
New Revision: 1708488

URL: http://svn.apache.org/viewvc?rev=1708488&view=rev
Log:
PDFBOX-2400: add methods to insert before and after a page

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageTree.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageTree.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageTree.java?rev=1708488&r1=1708487&r2=1708488&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageTree.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageTree.java Tue Oct 13 19:19:02 2015
@@ -435,4 +435,84 @@ public class PDPageTree implements COSOb
         }
         while (node != null);
     }
+    
+    /**
+     * Insert a page before another page within a page tree.
+     *
+     * @param newPage the page to be inserted.
+     * @param nextPage the page that is to be after the new page.
+     * @throws IllegalArgumentException if one attempts to insert a page that isn't part of a page
+     * tree.
+     */
+    public void insertBefore(PDPage newPage, PDPage nextPage)
+    {
+        COSDictionary nextPageDict = nextPage.getCOSObject();
+        COSDictionary parentDict = (COSDictionary) nextPageDict.getDictionaryObject(COSName.PARENT);
+        COSArray kids = (COSArray) parentDict.getDictionaryObject(COSName.KIDS);
+        boolean found = false;
+        for (int i = 0; i < kids.size(); ++i)
+        {
+            COSDictionary pageDict = (COSDictionary) kids.getObject(i);
+            if (pageDict.equals(nextPage.getCOSObject()))
+            {
+                kids.add(i, newPage.getCOSObject());
+                newPage.getCOSObject().setItem(COSName.PARENT, parentDict);
+                found = true;
+                break;
+            }
+        }
+        if (!found)
+        {
+            throw new IllegalArgumentException("attempted to insert before orphan page");
+        }
+
+        // now increase every parent
+        do
+        {
+            int cnt = parentDict.getInt(COSName.COUNT);
+            parentDict.setInt(COSName.COUNT, cnt + 1);
+            parentDict = (COSDictionary) parentDict.getDictionaryObject(COSName.PARENT);
+        }
+        while (parentDict != null);
+    }
+
+    /**
+     * Insert a page after another page within a page tree.
+     *
+     * @param newPage the page to be inserted.
+     * @param prevPage the page that is to be before the new page.
+     * @throws IllegalArgumentException if one attempts to insert a page that isn't part of a page
+     * tree.
+     */
+    public void insertAfter(PDPage newPage, PDPage prevPage)
+    {
+        COSDictionary prevPageDict = prevPage.getCOSObject();
+        COSDictionary parentDict = (COSDictionary) prevPageDict.getDictionaryObject(COSName.PARENT);
+        COSArray kids = (COSArray) parentDict.getDictionaryObject(COSName.KIDS);
+        boolean found = false;
+        for (int i = 0; i < kids.size(); ++i)
+        {
+            COSDictionary pageDict = (COSDictionary) kids.getObject(i);
+            if (pageDict.equals(prevPage.getCOSObject()))
+            {
+                kids.add(i + 1, newPage.getCOSObject());
+                newPage.getCOSObject().setItem(COSName.PARENT, parentDict);
+                found = true;
+                break;
+            }
+        }
+        if (!found)
+        {
+            throw new IllegalArgumentException("attempted to insert before orphan page");
+        }
+
+        // now increase every parent
+        do
+        {
+            int cnt = parentDict.getInt(COSName.COUNT);
+            parentDict.setInt(COSName.COUNT, cnt + 1);
+            parentDict = (COSDictionary) parentDict.getDictionaryObject(COSName.PARENT);
+        }
+        while (parentDict != null);
+    }
 }