I need to fill a PDF form with some data, and then apply a customized watermark to the PDF. Currently, I fill the PDF form using PyPDF, then save it and finally apply the watermark onto the PDF.
I attempted using PyPDF's indicated method of watermarking (here):
from pathlib import Path
from typing import Union, Literal, List
from PyPDF2 import PdfWriter, PdfReader
def watermark(
content_pdf: Path,
stamp_pdf: Path,
pdf_result: Path,
page_indices: Union[Literal["ALL"], List[int]] = "ALL",
):
reader = PdfReader(content_pdf)
if page_indices == "ALL":
page_indices = list(range(0, len(reader.pages)))
writer = PdfWriter()
for index in page_indices:
content_page = reader.pages[index]
mediabox = content_page.mediabox
# You need to load it again, as the last time it was overwritten
reader_stamp = PdfReader(stamp_pdf)
image_page = reader_stamp.pages[0]
image_page.merge_page(content_page)
image_page.mediabox = mediabox
writer.add_page(image_page)
with open(pdf_result, "wb") as fp:
writer.write(fp)
The expected result would be the filled out form with a watermark applied. Instead, I get a watermarked PDF, which has lost all form fields.