Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Question: How to merge cropped PDF page with full pdf page in PyPDF? #163

Closed
lananelson opened this issue Dec 16, 2014 · 7 comments · Fixed by #240
Closed

Question: How to merge cropped PDF page with full pdf page in PyPDF? #163

lananelson opened this issue Dec 16, 2014 · 7 comments · Fixed by #240
Labels
is-question Rather a question than an issue. Should usually be a Discussion instead

Comments

@lananelson
Copy link

I am trying to merge two pdf pages using PyPDF2:

background_page = background_pdf.getPage(0)
overlay = some_pdf.getPage(0)

# crop overlaying page vertically
overlay.mediaBox.lowerRight = (
    overlay.mediaBox.getUpperRight_x(),
    overlay.mediaBox.getUpperRight_y() / 2
)
# merge
background_page.mergePage(overlay)

Unfortunately cropping doesn't happen. mediaBox of the overlaying page appears to be ignored and I see entire page in the output.

Does anyone have any idea of how I can achieve this?

I posted this on SO as well http://stackoverflow.com/questions/27512246/how-to-merge-cropped-pdf-page-with-full-pdf-page-in-pypdf

@mstamy2
Copy link
Collaborator

mstamy2 commented Dec 30, 2014

Try

background_page = background_pdf.getPage(0)
overlay = some_pdf.getPage(0)

# crop overlaying page vertically
overlay.mediaBox.lowerRight = (
    overlay.mediaBox.getUpperRight_x(),
    overlay.mediaBox.getUpperRight_y() / 2
)
# merge
overlay.mergePage(background_page) # Switched

output = PdfFileWriter()
output.addPage(overlay) # Add the Overlay Page

@lananelson
Copy link
Author

If I merge pages this way then my overlay ends up being underneath the background. Is there a way to crop a page and save it in a new pdf without any data for the entire page? Right now if I crop a page using mediaBox and write it to the output file I can easily get entire page again if I change the mediaBox back to its original values. I hope this makes sense. Thanks!

@martinmanzo
Copy link

Hi @lananelson, I'm having a similar problem but can't figure out how to solve it. Did you find a way around it?

I'm cropping a page mediaBox and saving the PDF. If I open the file using any standard PDF reader I see the content cropped the way I wanted, but if I print it I get the original page as if no cropping had occurred.

@lananelson
Copy link
Author

@tincho89 I moved on from this a while ago and unfortunately don't exactly remember what the solution was (I don't have access to the repo anymore either). I think in your case you could try creating a blank page with the desired cropped dimensions and then merge your cropped page on top of it.

@martinmanzo
Copy link

@lananelson thanks for the response.

I was doing a proof of concept so I'm going to work on it again on some weeks, that approach looks good though. Will try it!

Thanks!

@forrestv
Copy link
Contributor

See PR #240 for a fix.

@felle9900
Copy link

Hi @lananelson, I'm having a similar problem but can't figure out how to solve it. Did you find a way around it?

I'm cropping a page mediaBox and saving the PDF. If I open the file using any standard PDF reader I see the content cropped the way I wanted, but if I print it I get the original page as if no cropping had occurred.

I have a similar problem - did you ever find a solution to your problem?

@MartinThoma MartinThoma added the is-question Rather a question than an issue. Should usually be a Discussion instead label Apr 8, 2022
@py-pdf py-pdf locked and limited conversation to collaborators Apr 9, 2022
@MartinThoma MartinThoma converted this issue into discussion #717 Apr 9, 2022
@py-pdf py-pdf deleted a comment from claird Apr 16, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
is-question Rather a question than an issue. Should usually be a Discussion instead
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants