Question

I have been looking for a solution for this problem : I have two landscape-oriented A3 pdfs with images and I want to overlay them in a manner that the resulting pdf contains both images merged into one as if one of them was a watermark, but with the same density. Think of it as if about printing two different pdfs on one A3 sheet of paper, I want to get exactly that effect.

In other words - just came up with a way to express it - I would like to overlay two pdfs and for the upper layer, make all the "white" area transparent.

Basically, I just followed steps in any solution from this question:

overlay one pdf or ps file on top of another

The pdftk didn't work in my case. The resulting PDF displayed the pdf that was on the top layer, but the bottom layer was not seen. So, I proceeded to programming solution and downloaded pyPdf.

The code from the site was exactly an implementation of the desired solution:

 from pyPdf import PdfFileReader,PdfFileWriter
 output = PdfFileWriter()
 input1 = PdfFileReader(file("b.pdf", "rb"))

 page1 = input1.getPage(0)
 watermark = PdfFileReader(file("a.pdf", "rb"))
 page1.mergePage(watermark.getPage(0))

 output.addPage(page1)

 outputStream = file("c.pdf", "wb")
 output.write(outputStream)
 outputStream.close()

However, the result was the same as after using pdftk.

What am I doing wrong? Maybe this is not pdf merging, multimerging, stamping, overlaying etc but something else? If so, how is it called?

Was it helpful?

Solution

White in a pdf can be a result of two fundamental situations: Either it is the result of nothing being drawn there or of something being drawn there using an effective color white. PDFs of the first type can be given a background using those page merge methods, PDFs of the latter can't.

The content stream of the page of your sample file a.pdf starts like this:

1 0 0 -1 0 841 cm
0.45 0 0 0.45 0 0 cm
1 0 0 1 0 0 cm
0 0 m 2646 0 l 2646 1870 l 0 1870 l h
q
1 1 1 rg f
Q

The first three lines change the coordinate system for the operations to come to have its origin in the upper left corner, coordinate values increasing right and down, and one unit being 1/160 inch.

The fourth line draws a rectangle covering the whole page (actually even slightly more) and the sixth line fills that rectangle with white. (The fifth and seventh line merely save and restore the graphics state.)

By overlaying this PDF over a page of another one, therefore, this PDF first of all covers all the existing content of that page with a white rectangle.

Thus, your PDF cannot be given a background by simply adding the page content to the content of a background PDF page. You have to

  • either remove the lines 4 and 6 from that content first (maybe there is some checkbox in lucidchart allowing you to switch this white background rectangle on or of)
  • or use a different watermarking procedure (like doing it the other way around, overlaying your PDF page with the watermark PDF page using transparency).

PS: Strictly speaking those content lines already are erroneous: As soon as you start constructing a path (which in the sample above happens with 0 0 m, i.e. a move to position 0, 0), you may only use path construction (or path clipping) operations until you finally use a path drawing operator (f, i.e. fill, in your sample). Cf. this answer for a reference.

Thus, the color setting 1 1 1 rg (i.e. set fill color to RGB 100%, 100%, 100%) and the special graphic state manipulation q (save graphic state) operations are not allowed here. Depending on the PDF viewer, therefore, different things may happen while displaying that page, e.g. the filling operation might be completely ignored or merely the color setting operation might be and the current fill color (black?) might be used instead. One cannot count on all PDF viewers handling this error like Adobe Reader does.

Maybe lucidchart have already fixed that issue and an update suffices. Otherwise you should ask lucidchart to start doing their PDF charts right.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top