In modern office environments, the security of PDF documents has become increasingly important. Adding watermarks is an effective means to ensure data security and prevent unauthorized copying. This article will introduce how to use Python's Spire.PDF library to add text watermarks to PDF documents.
Introduction to Spire.PDF
Spire.PDF is a powerful PDF processing library that supports various PDF operations, including creating, editing, converting, and printing PDF documents. For developers looking to perform PDF operations in Python, Spire.PDF provides a concise API that allows users to easily access and manipulate PDF files.
Installing Spire.PDF
Before using Spire.PDF, it needs to be installed. You can install the library using pip with the following command in your terminal:
pip install Spire.PDF
Make sure you have a Python environment and pip installed before executing the command above.
Example Code to Add Watermarks to PDF Documents
Next, we will demonstrate how to add text watermarks to a PDF document through example code. Below is a simplified code snippet:
from spire.pdf import PdfDocument
from spire.pdf.common import PdfTrueTypeFont, PdfBrushes, PointF
# Create an instance of the PdfDocument class and load the PDF
doc = PdfDocument()
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf")
# Create watermark font
font = PdfTrueTypeFont("SimHei", 48.0, 0, True)
text = "For Internal Use Only"
# Calculate text dimensions
text_width = font.MeasureString(text).Width
text_height = font.MeasureString(text).Height
# Iterate through each page to add the watermark
for i in range(doc.Pages.Count):
page = doc.Pages.get_Item(i)
state = page.Canvas.Save() # Save the current canvas state
# Calculate the center coordinates of the page
x = page.Canvas.Size.Width / 2
y = page.Canvas.Size.Height / 2
# Adjust the coordinate system, setting the page center as the origin
page.Canvas.TranslateTransform(x, y)
page.Canvas.RotateTransform(-45.0) # Rotate counter-clockwise by 45 degrees
page.Canvas.SetTransparency(0.4) # Set transparency
# Draw the watermark text
page.Canvas.DrawString(text, font, PdfBrushes.get_Blue(), PointF(-text_width / 2, -text_height / 2))
page.Canvas.Restore(state) # Restore the canvas state
# Save the modified document
doc.SaveToFile("output/TextWatermark.pdf")
doc.Dispose() # Release resources
Code Explanation
-
Loading the PDF Document : First, we load the specified path of the PDF document using the
PdfDocumentclass. -
Setting the Watermark Font and Text : Next, we create a
PdfTrueTypeFontobject, specifying the font, size, and style, and define the watermark text. -
Calculating Text Dimensions : We use the
MeasureStringmethod to get the text's width and height for proper watermark positioning. - Iterating through Each Page of the Document : We use a for loop to iterate through each page of the document, drawing the watermark on each one.
- Saving and Releasing Resources : Finally, we save the modified document to a new PDF file and release resources.
Conclusion
Using the above code, developers can easily add text watermarks to PDF documents. This not only enhances the security of the documents but also adds a professional touch. The Spire.PDF library offers rich functionalities, greatly facilitating PDF file handling. Whether for personal projects or enterprise-level solutions, Spire.PDF is a worthy consideration.
I hope this article helps you quickly get familiar with how to use Python to add watermarks to PDFs. When you experiment by yourself, make sure you have permission to modify the relevant PDF documents!
Top comments (0)