Permalink
Browse files

Add a method to insert pages to PdfFileWriter

The new insertPage method inserts a page at a given position.

Patch from Kjo Hansi Glaz <kjo@a4nancy.net.eu.org>
  • Loading branch information...
mfenniak committed Oct 15, 2009
1 parent 6b6ca78 commit ae35d71b3652675b880f78fc07016cd42546219d
Showing with 26 additions and 6 deletions.
  1. +26 −6 pyPdf/pdf.py
View
@@ -96,21 +96,41 @@ def getObject(self, ido):
return self._objects[ido.idnum - 1]
##
- # Adds a page to this PDF file. The page is usually acquired from a
- # {@link #PdfFileReader PdfFileReader} instance.
- # <p>
- # Stability: Added in v1.0, will exist for all v1.x releases.
+ # Common method for inserting or adding a page to this PDF file.
#
# @param page The page to add to the document. This argument should be
# an instance of {@link #PageObject PageObject}.
- def addPage(self, page):
+ # @param action The function which will insert the page in the dictionnary.
+ # Takes: page list, page to add.
+ def _addPage(self, page, action):
assert page["/Type"] == "/Page"
page[NameObject("/Parent")] = self._pages
page = self._addObject(page)
pages = self.getObject(self._pages)
- pages["/Kids"].append(page)
+ action(pages["/Kids"], page)
pages[NameObject("/Count")] = NumberObject(pages["/Count"] + 1)
+ ##
+ # Adds a page to this PDF file. The page is usually acquired from a
+ # {@link #PdfFileReader PdfFileReader} instance.
+ # <p>
+ # Stability: Added in v1.0, will exist for all v1.x releases.
+ #
+ # @param page The page to add to the document. This argument should be
+ # an instance of {@link #PageObject PageObject}.
+ def addPage(self, page):
+ self._addPage(page, list.append)
+
+ ##
+ # Insert a page in this PDF file. The page is usually acquired from a
+ # {@link #PdfFileReader PdfFileReader} instance.
+ #
+ # @param page The page to add to the document. This argument should be
+ # an instance of {@link #PageObject PageObject}.
+ # @param index Position at which the page will be inserted.
+ def insertPage(self, page, index=0):
+ self._addPage(page, lambda l, p: l.insert(index, p))
+
##
# Encrypt this PDF file with the PDF Standard encryption handler.
# @param user_pwd The "user password", which allows for opening and reading

0 comments on commit ae35d71

Please sign in to comment.