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
Need an example of apply watermark #110
Comments
It's usually best to capture a page as a Form XObject: dictpage = watermark_pdf.pages[0]
page = pikepdf.Page(dictpage)
formx = page.as_form_xobject() Then attach it to The reason it's not working is likely the alpha channel. Transparency, like so many PDF features, was awkwardly bolted on after the original spec. The alpha channel information goes into a /Resources /ExtGState object (extended graphics state), and the content stream will activate it. However, if you merge with the input page, you need to check its resource dictionary for name conflicts and possibly edit the content stream. It may be that reportlab put other interesting details in to ExtGState as well. |
Hello @jbarlow83 All of them are trying to use Also, @StevenLOL if you were able to get the pdf watermarked, any of your help is appreciated! |
Here's an improved, fully functioning example. This is also an improved over the past versions that combined the content streams. This one instead captures the watermark in a "form XObject" (sort of like a sub-page) to isolate it from the rest of the document and makes it less likely to alter the rest of the document. In a future release I will add import pikepdf
from pikepdf import Name, Pdf, Object, Dictionary
from reportlab.pdfgen import canvas
from typing import Optional
def generate_watermark(msg,xy):
x, y = xy
buf = BytesIO()
c = canvas.Canvas(buf, bottomup=0)
c.setFontSize(32)
c.setFillColorCMYK(0, 0, 0, 0, alpha=0.7)
c.rect(204, 199, 157, 15, stroke=0, fill=1)
c.setFillColorCMYK(0, 0, 0, 100, alpha=0.7)
c.drawString(x, y, msg)
c.save()
buf.seek(0)
return buf
wm = generate_watermark('Watermark', (100, 100))
txt = generate_watermark('Document text', (200, 200))
with pikepdf.open(wm) as pdf_wm, pikepdf.open(txt) as pdf_txt:
wm_page = pikepdf.Page(pdf_wm.pages[0])
wm_formx = wm_page.as_form_xobject()
formx = pdf_txt.copy_foreign(wm_formx)
page = pdf_txt.pages[0]
formx_page = pikepdf.Page(page)
formx_name = formx_page.add_resource(formx, Name.XObject)
draw_watermark_content_stream = pdf_txt.make_stream(b'q 1 0 0 1 0 0 cm %s Do Q' % formx_name)
pdf_txt.pages[0].page_contents_add(draw_watermark_content_stream, prepend=True)
pdf_txt.save('out.pdf') |
Thanks @jbarlow83, I will try this out. |
Hi, |
If a document completely draws it over the page it will hide the watermark.
It might do this by drawing an image or filled rectangle.
You could draw the watermark last, as a transparent overlay, or make
whatever is on top of the watermark use a transparent background.
Could also be a page with an unusual weird MediaBox or CropBox.
…On Tue., Jan. 5, 2021, 12:01 pmg007, ***@***.***> wrote:
Hi,
I tried this template out and it has helped up to an extent, thanks!
Although for some pdfs I see that I am not able to see the watermark, when
I search using Cmd+F I can see that text is present at the bottom of the
pdf but is not visible to eye. Any hints or guess so as to what might be
the cause for that?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#110 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN5YM4AJCKCDPJ4M7W2NNDSYNVZHANCNFSM4O2UL2MQ>
.
|
Thanks for the quick reply. What do you mean by adding the watermark last? In the code template above, we are adding a watermark to the existing page afterward, or am I mistaken? |
prepend=True, so the watermark is placed before all other drawing
operations.
…On Wed., Jan. 6, 2021, 08:23 pmg007, ***@***.***> wrote:
Thanks for the quick reply. What do you mean by adding the watermark last?
In the code template above, we are adding a watermark to the existing page
afterward, or am I mistaken?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#110 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN5YM73SJEX2M6RQM4BMMTSYSFAXANCNFSM4O2UL2MQ>
.
|
Got it thanks, it mostly works now except in one of the faulty PDFs where it shows a mirror image of the watermark on the next page. Anyways, thanks a lot @jbarlow83 |
I updated the example to demonstrate the use pikepdf.Page.add_resource which is available in pikepdf 2.3.0. |
That's great! Thanks for the new release with |
Thank you very much, @jbarlow83 you rock! |
Thank you so much @jbarlow83, This helped out a lot in my case as well. However I'm curious of the purpose and meaning of the I've checked the documentation on make_stream, but the examples or descriptions don't even come close to what you have written here. Could you maybe show me a pointer to where can I read more about this kind of usages and what do they mean respectively? Please and thank you. |
It's PDF content stream operators.
q - push graphics stack
1 0 0 1 0 0 cm - multiply current transform matrix by identify matrix
%s Do - draw named object here
Q - pop stack
The identity matrix isn't strictly necessary. It's a clue that you could
use a different matrix to perform rotation, scale, translation.
See PDF manual on content streams.
…On Mon., Apr. 19, 2021, 18:15 Vincent Yang, ***@***.***> wrote:
Thank you so much @jbarlow83 <https://github.com/jbarlow83>, This helped
out a lot in my case as well.
However I'm curious of the purpose and meaning of the b'q 1 0 0 1 0 0 cm
%s Do Q' % formx_name in
draw_watermark_content_stream = pdf_txt.make_stream(b'q 1 0 0 1 0 0 cm %s
Do Q' % formx_name)
I've checked the documentation on make_stream
<https://pikepdf.readthedocs.io/en/latest/api/main.html?highlight=make_stream#pikepdf.Pdf.make_stream>,
but the examples or descriptions don't even come close to what you have
written here. Could you maybe show me a pointer to where can I read more
about this kind of usages and what do they mean respectively?
Please and thank you.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#110 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAN5YM53CRCCP6YBM2BNPQ3TJTIRLANCNFSM4O2UL2MQ>
.
|
Now implemented officially https://pikepdf.readthedocs.io/en/latest/topics/overlays.html |
hi,
Need an example of applying watermark.
Tested with following code, It doesn't work as I exptected, the "watermark" was not on the right location.
The text was updated successfully, but these errors were encountered: