Step 1: Install the necessary libraries
First, make sure you have installedPyPDF2
andreportlab
Library. If they are not installed, you can install them via pip using the following command:
pip install PyPDF2 reportlab
Step 2: Prepare the font file
Since we want to use Chinese in watermarks, we need to prepare a Chinese-supported font file, for example (bold). Make sure that this font file is in the same directory as your Python script, or you know its full path.
Step 3: Write Python scripts
Next, we write a Python script to generate a watermark and add it to an existing PDF document.
import io from PyPDF2 import PdfWriter, PdfReader from import pagesizes from import cm from import pdfmetrics from import TTFont from import canvas # Register fonts to use Chinese in PDF(TTFont('SimHei', '')) # Functions that generate watermark filesdef create_water_mark(text): packet = () # Create a new PDF page as a watermark my_canvas = (packet, pagesizes.A4) # Set the watermark font and size my_canvas.setFont("SimHei", 20) # Set the fill color to black my_canvas.setFillColorRGB(0, 0, 0) # Set transparency (0.0 is completely transparent, 1.0 is completely opaque) my_canvas.setFillAlpha(0.1) # Rotate the font to increase the watermark effect my_canvas.rotate(30) # Draw watermark text multiple times on the page to form a watermark pattern for i in range(3, 24, 10): for j in range(-5, 30, 5): my_canvas.drawString(i * cm, j * cm, text) my_canvas.save() (0) # Return to the PdfReader object of the watermark page return PdfReader(packet) # Functions that add watermark to PDF filesdef add_watermark(input_pdf_path, output_pdf_path, watermark_text): # Create a watermark watermark = create_water_mark(watermark_text) # Read input PDF file pdf_reader = PdfReader(input_pdf_path) pdf_writer = PdfWriter() # traverse each page of the input PDF and add a watermark for page in pdf_reader.pages: page.merge_page([0]) pdf_writer.add_page(page) # Write a PDF with a watermark to the output file with open(output_pdf_path, "wb") as output_pdf: pdf_writer.write(output_pdf) # Main Programif __name__ == '__main__': # Input and output PDF file paths input_pdf = r"" # Replace with your input PDF path output_pdf = r"output_with_watermark.pdf" # Expected output PDF path watermark_text = "Add watermark test text" # Watermark text content # Call the function to add watermark add_watermark(input_pdf, output_pdf, watermark_text)
Step 4: Run the script
Save the above script as a.py
Documentation and ensureThe font file and the script are located in the same directory. Then, run the script on the command line:
python your_script_name.py
Willyour_script_name.py
Replace with the file name of the script you saved.
Step 5: Check the results
After the script is completed, check the specified output directory and you should see a name calledoutput_with_watermark.pdf
new file. Open it and you will see that the specified watermark text is added to each page.
Things to note
- make sure
The font file is in the same directory as your script, or its path is correctly specified.
-
reportlab
The library is used to generate watermark pages, andPyPDF2
Library is used to read and write PDF files and merge pages. - The transparency and rotation angle of the watermark can be adjusted as needed.
- The code in this tutorial is a basic example that may need to be modified and optimized according to the situation.
This is the article about the code steps for adding watermarks to PDFs in Python. For more related content on adding watermarks to Python PDFs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!