Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merges "on bottom" #11

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions pyPdf/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,19 @@ def getContents(self):
def mergePage(self, page2):
self._mergePage(page2)

##
# Merges the content streams of two pages into one. Resource references
# (i.e. fonts) are maintained from both pages. The mediabox/cropbox/etc
# of this page are not altered. The parameter page's content stream will
# be added to the begin of this page's content stream, meaning that it
# will be drawn before, or "on bottom" of this page.
# <p>
# Stability: Added in v1.13, will exist for all future 1.x releases.
# @param page2 An instance of {@link #PageObject PageObject} to be merged
# into this one.
def mergePageBottom(self, page2,):
self._mergePage(page2, onbottom=True)

##
# Actually merges the content streams of two pages into one. Resource
# references (i.e. fonts) are maintained from both pages. The
Expand All @@ -1139,7 +1152,10 @@ def mergePage(self, page2):
# contents stream. Must return: new contents
# stream. If omitted, the content stream will
# not be modified.
def _mergePage(self, page2, page2transformation=None):
# @param onbottom A boolean to permit merge content stream before original
# page content, meaning that it will be drawn before, or
# "on bottom" of this page.
def _mergePage(self, page2, page2transformation=None, onbottom=False):
# First we work on merging the resource dictionaries. This allows us
# to find out what symbols in the content streams we might need to
# rename.
Expand All @@ -1165,7 +1181,7 @@ def _mergePage(self, page2, page2transformation=None):
newContentArray = ArrayObject()

originalContent = self.getContents()
if originalContent is not None:
if originalContent is not None and not onbottom:
newContentArray.append(PageObject._pushPopGS(
originalContent, self.pdf))

Expand All @@ -1178,6 +1194,11 @@ def _mergePage(self, page2, page2transformation=None):
page2Content = PageObject._pushPopGS(page2Content, self.pdf)
newContentArray.append(page2Content)

# Permit to merge "on bottom"
if originalContent is not None and onbottom:
newContentArray.append(PageObject._pushPopGS(
originalContent, self.pdf))

self[NameObject('/Contents')] = ContentStream(newContentArray, self.pdf)
self[NameObject('/Resources')] = newResources

Expand Down