overlay one pdf or ps file on top of another

PdfPostscript

Pdf Problem Overview


I have two pdf or postscript files (I can work with either one). What I want to do is merge each page on top of the other so that page1 of document A will be combined with page 1 of document B to produce page 1 of the output document. This isn't something I necessarily want need to do programatically, although that would be helpful.

Any ideas?

Pdf Solutions


Solution 1 - Pdf

You can do this with pdf files using the command line tool pdftk using the stamp or background option.

e.g.

$ pdftk file1.pdf background file2.pdf output combinedfile.pdf

This will only work with a one-page background file. If you have multiple pages, you can use the multibackground command instead.

Solution 2 - Pdf

I had success solving this problem (PDF only and Python) by using pyPdf, specifically the mergePage operation.

From the docs:

# add page 4 from input1, but first add a watermark from another pdf:
page4 = input1.getPage(3)
watermark = PdfFileReader(file("watermark.pdf", "rb"))
page4.mergePage(watermark.getPage(0))

Should be enough to get the idea.

Solution 3 - Pdf

Many of the answers here seem extremely out of date. This is especially so for the accepted answer, which uses pdftk. Pdftk is old, has terrible error handling, requires old versions of libraries, and is no longer packaged for ubuntu. Nobody in 2022 should be using pdftk for anything if they can possibly avoid it.

Qpdf is a nice open-source program that does many of the same things as pdftk, and it can do this task:

qpdf a.pdf --overlay b.pdf -- c.pdf

Solution 4 - Pdf

If you're dealing with only postscript, chances are the only 'pagebreaks' are the 'showpage' operator.
In which case you can simply grab the postscript data from the beginning of file one to the first instance of 'showpage', do the same with the other file, then concatenate these 2 chunks of postscript to create your new page.

If the 2 files are only one page, then you may be able to simply join the 2 files.

Solution 5 - Pdf

PDFbox for Java supports a Overlay class which allows to merge PDFs this way. See this answer: https://stackoverflow.com/questions/8929954/watermarking-with-pdfbox

However, both PyPDF2 and PDFbox have been unreliable in my experience, but perhaps this is helpful for someone.

Solution 6 - Pdf

Aspose.Pdf.Kit with thePdfFileStamp class can do this, too. It works most of the time correctly.

Solution 7 - Pdf

I used the Mac OS tool PDFClerk Pro. I imported the PDF pages, then merged them with the option "Merge Pages (Stacked)." It really impressed me.

Solution 8 - Pdf

2022 onwards

PDFTK is still a strong contender for many users with simple cross platform command lines for many tasks. > PDFtk Server does not require Adobe Acrobat or Reader, and it runs on Windows, Mac OS X and Linux.

You will see multiple commands in the other answers. But one drawback is that it needs to be licensed for distribution. >A commercial license is required to distribute PDFtk with your commercial product.

Another daily updated cross platform Solution is Ghostscript (but Windows binaries are often bi-annual) and it too requires a commercial license.

Its primary strength is the ability to merge nominated pages from both PostScript AND PDF along with other formats not handled by PDFTK. However it does not profess to be the optimum PDF merge tool. Again there are many well documented ways in StackOverflow answers to use that grandfather application for different combinational tasks, but may requires more complex approaches than a quick one liner, and many users will combine PDL or PS handling with PDFTK (above) for final overlay.

So what is updated frequently, and is available for commercial use and does the task in a single call? The answer is QPDF (licensed under the Apache License, Version 2.0), which has both overlay and underlay options with too many many other abilities to list, but is PDF only.

At its simplest one method is to write a new page from 2 others lets say just page 1, and not changing either source. The downside is that with so many options including passwords the syntax can become complex, good news is it may be overhauled in next version 11. Also beware it does not like file names with spaces.

First generate a new empty input file with page 1 from file1 (easy enough)

qpdf --empty --pages "file1.pdf" 1 -- "output.pdf"

Now overlay that with first page of file2 (this is a bit more complicated)

qpdf output.pdf --overlay "file2.pdf" --from=1 -- --replace-input

The commands can be combined in one simpler line and for this example lets use page 2 of file 1

qpdf --empty --pages "file1.pdf" 2 --  --overlay "file2.pdf" --from=1 -- "output2.pdf"

You will also need to be aware that mixing page sizes will work however perhaps not as expected so second on first is centered and reduced to fit.

enter image description here

Solution 9 - Pdf

You could convert both pdfs into images and overlay one on top of the other layer like.

A suitable graphics library that you could use this would work.

Watermark suggestion above has great potential too as long as you don't run into issues in your language or graphics/pdf library of choice.

Solution 10 - Pdf

For OS X there is PDF letterhead. Doesn't do anything else than just overlaying PDF's. https://itunes.apple.com/us/app/pdf-letterhead/id976548033?mt=12

Solution 11 - Pdf

VeryPDF PDF Editor has a PDF Overlay function, look at this web page,

http://www.verypdf.com/wordpress/201304/how-to-overlay-pdf-to-another-pdf-35885.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnnyLambadaView Question on Stackoverflow
Solution 1 - PdfbmbView Answer on Stackoverflow
Solution 2 - Pdfpi.View Answer on Stackoverflow
Solution 3 - Pdfuser18021191View Answer on Stackoverflow
Solution 4 - PdfMichael GalosView Answer on Stackoverflow
Solution 5 - PdfLenar HoytView Answer on Stackoverflow
Solution 6 - PdfUwe KeimView Answer on Stackoverflow
Solution 7 - PdfJ. B. RainsbergerView Answer on Stackoverflow
Solution 8 - PdfK JView Answer on Stackoverflow
Solution 9 - PdfJas PanesarView Answer on Stackoverflow
Solution 10 - PdfmipmipView Answer on Stackoverflow
Solution 11 - PdfDavidView Answer on Stackoverflow