1.设计水印图
from reportlab.pdfgen.canvas import Canvas
from reportlab.pdfbase.pdfmetrics import registerFont
from reportlab.pdfbase.ttfonts import TTFont
new_water=Canvas('files/new_water.pdf')
registerFont(TTFont('f1', 'files/font1.ttf'))
new_water.setFont('f1',12)
new_water.setFillColorRGB(180/255,56/255,12/255,0.5)
new_water.rotate(-5)
for x in range(1,5):
for y in range(1,10):
new_water.drawString(x*50,y*50,'你好!')
new_water.drawImage('files/car.png',x*100,y*200,mask='auto')
new_water.save()
2.将input里面的pdf文件全部加上水印保存到out文件中
import os
from PyPDF2 import PdfReader,PdfWriter
file=os.listdir('input')
print(file,len(file))
writer=PdfWriter()
water_reader=PdfReader('files/new_water.pdf')
water=water_reader.getPage(0)
for x in range(len(file)):
name1=('input'+'/'+str(file[x]))
name2=('out'+'/'+str(file[x]))
print(name1)
reader1=PdfReader(name1)
for y in range(reader1.getNumPages()):
page=reader1.getPage(y)
page.mergePage(water)
writer.addPage(page)
writer.write(name2)