pdf分割与pdf水印

python实现需要库pyPdfreportlab

from pyPdf import PdfFileReader, PdfFileWriter
from reportlab.pdfgen import canvas

生成水印文件

water_fp = 'watermark.pdf'
# 加图片
water_img = 'watermark.jpg'
c = canvas.Canvas(water_fp)
c.drawInlineImage(water_img, left, bottom, width, height)
# 加文字
c.setFont('Courier', 10)
c.saveState()
c.translate(300, 15)
c.drawCentredString(0, 0, content)
c.restoreState()
c.save()

水印合成pdf

pdf_watermark = PdfFileReader(file(watermark_pdf_name, 'rb')).getPage(0)
pdf = PdfFileReader(file(pdf_name, "rb"))
output = PdfFileWriter()
page = pdf.getPage(i) # 如果是全部页面用for i in range(1,pdf.getNumPages()+1)
page.mergePage(watermark_page)
output.addPage(page)
outputStream = file(output_pdf_name, "wb")
output.write(outputStream)
outputStream.close()

pdf分割

pdf = PdfFileReader(file(pdf_name, "rb"))
output = PdfFileWriter()
page = pdf.getPage(i) 
page = pdf.getPage(j)
...
output.addPage(page)
outputStream = file(output_pdf_name, "wb")
output.write(outputStream)
outputStream.close()