2

The following code adds a watermark on every page and also adds metadata (or better should do). The watermarking works perfectly fine, but there is no metadata in the output document and no error

from PyPDF2 import PdfFileReader as PdfReader, PdfFileWriter as PdfWriter

nf = []
sources = ["example.pdf", "example2.pdf"]

for i in sources:
    # new pdf file name
    new_file_name = i + " " + row["name"] + ".pdf"
    nf.append(new_file_name)

    print(f"Registering {i} watermarked version for {row['name']}.")

    reader = PdfReader(i)
    writer = PdfWriter()

    # adding watermark to each page
    for page in reader.pages:
        # creating watermarked page object
        wmpageObj = add_watermark(packet, page)

        # adding watermarked page object to pdf writer
        writer.addPage(wmpageObj)

    # Write Metadate
    writer.addMetadata({"/Registered to": row["name"]})
    writer.addMetadata({"/ATC": "ACME Inc."})

    # writing watermarked pages to new file
    with open(new_file_name, "wb") as newFile:
        writer.write(newFile)

Adding print(pdfWriter._info) before and after adding the metadata gives me only:

IndirectObject(2, 0)

IndirectObject(2, 0)

Also interesting: I tried Adobe Acrobat Reader DC on Mac and Windows and it's not possible to show the metadata of the output file (the window just won't open), but works fine with the source file, i.e. before adding watermark and metadata.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
f0rd42
  • 1,429
  • 4
  • 19
  • 30
  • Possible duplicate of [reading/writing xmp metadatas on pdf files through pypdf](https://stackoverflow.com/questions/466692/reading-writing-xmp-metadatas-on-pdf-files-through-pypdf) – stovfl Oct 03 '18 at 15:23
  • Don't understand why this should be a duplicate. pyPDF2 has a addMetadata function build in and the post you refer to is about pyPDF not pyPDF2 – f0rd42 Nov 02 '18 at 08:55
  • @stovfl added the outcome in my original question – f0rd42 Nov 04 '18 at 17:16
  • What about pyPDF2 Version? *" but works fine with the source file"*: Try without `addMetadata(...` if this persists. – stovfl Nov 04 '18 at 19:48

1 Answers1

0

PyPDF2 was merged back into pypdf. PyPDF2 is now deprecated.

This is how you should write metadata now: https://pypdf.readthedocs.io/en/latest/user/metadata.html

from pypdf import PdfReader, PdfWriter

reader = PdfReader("example.pdf")
writer = PdfWriter()

# Add all pages to the writer
for page in reader.pages:
    writer.add_page(page)

# Add the metadata
writer.add_metadata(
    {
        "/Author": "Martin",
        "/Producer": "Libre Writer",
    }
)

# Save the new PDF to a file
with open("meta-pdf.pdf", "wb") as f:
    writer.write(f)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958