为pdf文件添加水印,要求: 1,可以文字水印,也可以上传图片作为水印 2,可以调节水印大小(0到100),透明度(0到100),旋转角度,翻转(无,水平翻转,垂直翻转),和选择水印位置(九宫格或平铺) 3,使用python语言并显示完整代码 4,使用PyPDF4
时间: 2024-05-05 12:15:39 浏览: 148
模块
以下是实现以上要求的Python代码:
```python
from PyPDF4 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
from PIL import Image
import io
def add_watermark(input_pdf, output_pdf, watermark_text=None, watermark_image=None, size=50, opacity=50, angle=0, flip=None, position='center'):
# Open the input PDF file
input_file = open(input_pdf, 'rb')
pdf_reader = PdfFileReader(input_file)
# Create a new PDF writer
pdf_writer = PdfFileWriter()
# If watermark text is provided, create a watermark PDF
if watermark_text:
# Create a canvas to draw the watermark text
watermark_canvas = canvas.Canvas('watermark.pdf')
# Set the font and font size for the watermark text
watermark_canvas.setFont('Helvetica', size)
# Draw the watermark text in the center of the canvas
watermark_canvas.drawCentredString(105*mm, 148*mm, watermark_text)
# Save the watermark PDF
watermark_canvas.save()
# Open the watermark PDF file
watermark_file = open('watermark.pdf', 'rb')
watermark_reader = PdfFileReader(watermark_file)
# Get the first page of the input PDF file
input_page = pdf_reader.getPage(0)
# Get the first page of the watermark PDF file
watermark_page = watermark_reader.getPage(0)
# Merge the pages
input_page.mergePage(watermark_page)
# Add the merged page to the output PDF writer
pdf_writer.addPage(input_page)
# Close the watermark PDF file
watermark_file.close()
# If watermark image is provided, create a watermark PDF
if watermark_image:
# Open the watermark image file
with Image.open(watermark_image) as img:
# Create a buffer to hold the PDF data
img_buffer = io.BytesIO()
# Convert the image to PDF and save it to the buffer
img.save(img_buffer, 'PDF', resolution=100.0)
# Set the buffer position to the beginning
img_buffer.seek(0)
# Open the PDF data as a reader
watermark_reader = PdfFileReader(img_buffer)
# Get the first page of the input PDF file
input_page = pdf_reader.getPage(0)
# Get the first page of the watermark PDF file
watermark_page = watermark_reader.getPage(0)
# Merge the pages
input_page.mergePage(watermark_page)
# Add the merged page to the output PDF writer
pdf_writer.addPage(input_page)
# If neither watermark text nor watermark image is provided, simply copy the input PDF file to the output PDF file
if not watermark_text and not watermark_image:
for page in pdf_reader.pages:
pdf_writer.addPage(page)
# Set the opacity of the watermark
opacity_percent = opacity / 100.0
pdf_writer.encrypt(user_pwd=None, owner_pwd=None, use_128bit=True)
pdf_writer._encrypt_metadata=False
# Get the output PDF file object
output_file = open(output_pdf, 'wb')
# Set the watermark position
if position == 'center':
x = 0.5
y = 0.5
elif position == 'top_left':
x = 0.1
y = 0.9
elif position == 'top_center':
x = 0.5
y = 0.9
elif position == 'top_right':
x = 0.9
y = 0.9
elif position == 'center_left':
x = 0.1
y = 0.5
elif position == 'center_right':
x = 0.9
y = 0.5
elif position == 'bottom_left':
x = 0.1
y = 0.1
elif position == 'bottom_center':
x = 0.5
y = 0.1
elif position == 'bottom_right':
x = 0.9
y = 0.1
# Loop through all pages of the output PDF writer
for page in pdf_writer.pages:
# Get the page width and height
width = page.mediaBox.getWidth()
height = page.mediaBox.getHeight()
# Create a new content stream for the watermark
watermark_content = PdfFileWriter().addBlankPage(width, height)
# Set the opacity of the watermark
watermark_content.getPage(0).gfx.setAlpha(opacity_percent)
# Rotate the watermark
if angle != 0:
watermark_content.getPage(0).rotateClockwise(angle)
# Flip the watermark
if flip == 'horizontal':
watermark_content.getPage(0).scale(-1, 1)
watermark_content.getPage(0).translate(-width, 0)
elif flip == 'vertical':
watermark_content.getPage(0).scale(1, -1)
watermark_content.getPage(0).translate(0, -height)
# Draw the watermark on the content stream
if watermark_text:
watermark_content.mergePage(watermark_reader.getPage(0))
elif watermark_image:
watermark_content.mergePage(watermark_reader.getPage(0))
# Add the content stream to the output page
page.mergePage(watermark_content.getPage(0))
# Set the position of the watermark on the output page
page.mergeTranslatedPage(watermark_content.getPage(0), x*width, y*height)
# Add the modified page to the output PDF file
output_page = page
pdf_writer.addPage(output_page)
# Write the output PDF file
pdf_writer.write(output_file)
# Close the input and output PDF files
input_file.close()
output_file.close()
# Example usage
add_watermark('input.pdf', 'output.pdf', watermark_text='CONFIDENTIAL', size=50, opacity=50, angle=45, flip='horizontal', position='center')
add_watermark('input.pdf', 'output.pdf', watermark_image='watermark.png', size=50, opacity=50, angle=45, flip='horizontal', position='center')
```
阅读全文