Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import PyPDF2
- from reportlab.pdfgen import canvas
- from reportlab.lib.utils import ImageReader
- import io
- # Step 1 - define what it means to add a watermark to an input-doc and to save it to an output-doc
- # This does not require any changes, except where indicated below
- def add_watermark(input_pdf, output_pdf, watermark_png):
- with open(input_pdf, 'rb') as pdf_file:
- pdf_reader = PyPDF2.PdfReader(pdf_file)
- pdf_writer = PyPDF2.PdfWriter()
- watermark_pdf = canvas.Canvas(io.BytesIO())
- watermark_pdf.setFillAlpha(0.5)
- # the number we input into setFillAlpha determines the transparency of the image
- # 0.0 - fully transparent
- # 1.0 - fully opaque
- # my default setting is
- # watermark_pdf.setFillAlpha(0.5)
- watermark_pdf.drawImage(ImageReader(watermark_png), 100, 200, width=400, height=400)
- # the numbers here determine the position and size of the watermark, you can play around with these
- # my default setting is
- # watermark_pdf.drawImage(ImageReader(watermark_png), 100, 200, width=400, height=400)
- watermark_pdf.save()
- watermark_page = PyPDF2.PdfReader(io.BytesIO(watermark_pdf.getpdfdata())).pages[0]
- for page_num in range(len(pdf_reader.pages)):
- page = pdf_reader.pages[page_num]
- page.merge_page(watermark_page)
- pdf_writer.add_page(page)
- with open(output_pdf, 'wb') as output_file:
- pdf_writer.write(output_file)
- # Step 2 - We must define the paths where the program is supposed to work
- # You NEED to edit the value for these 3 variables
- input_folder = r'C:\Users\Anwender\Downloads\Gavel\Input'
- # you NEED to edit this
- # the value be of the format r'[the address of your folder for documents you want watermarked]'
- # Example:
- # [the address of your folder for documents you want watermarked] = C:\Users\Anwender\Downloads\Gavel\Input
- # then
- # input_folder = r'C:\Users\Anwender\Downloads\Gavel\Input'
- output_folder = r'C:\Users\Anwender\Downloads\Gavel\Output'
- # you NEED to edit this
- # the value should be of the format r'[the address of your folder for the documents once the watermark has been added]'
- # Example:
- # [the address of your folder for the documents once the watermark has been added] = C:\Users\Anwender\Downloads\Gavel\Output
- # then
- # output_folder = r'C:\Users\Anwender\Downloads\Gavel\Output'
- watermark_png_path = r'C:\Users\Anwender\Downloads\Gavel\Watermark_Gavel.png'
- # you NEED to edit this
- # the value should be of the format r'[the address of your file that contains the watermark image]'
- # Example:
- # [the address of your file that contains the watermark image] = C:\Users\Anwender\Downloads\Gavel\Watermark_Gavel.png
- # then
- # watermark_png_path = r'C:\Users\Anwender\Downloads\Gavel\Watermark_Gavel.png'
- # Step 3 - Loop through all PDF files in the input folder
- # You don't need to do anything here
- for filename in os.listdir(input_folder):
- if filename.endswith(".pdf"):
- # Extract filename without extension
- file_name, _ = os.path.splitext(filename)
- # Construct input and output paths
- input_pdf_path = os.path.join(input_folder, filename)
- output_pdf_path = os.path.join(output_folder, f"{file_name}_WM.pdf")
- # Add watermark to the current file
- add_watermark(input_pdf_path, output_pdf_path, watermark_png_path)
- print(f"Watermarked: {filename} -> {file_name}_WM.pdf")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement