我正在尝试使用PyPDF2将水印添加到pdf的每一页。 输入文件和水印文件均采用pdf格式 我尝试使用代码:
import PyPDF2
inputf = "output.pdf"
outputf = "hfout.pdf"
wmark = "Doc1.pdf"
with open (inputf, "rb") as fileinput:
pdf = PyPDF2.PdfFileReader(fileinput)
with open(wmark, "rb") as fwmark:
watermark = PyPDF2.PdfFileReader(fwmark)
outputfile = pdf.getPage(0)
pdfWriter = PyPDF2.PdfFileWriter()
for i in range(1, pdf.getNumPages()):
watmark = watermark.getPage(0)
page = pdf.getPage(i)
watmark.mergePage(page)
pdfWriter.insertPage(watmark)
with open(outputf, "wb") as fout:
pdfWriter.write(fout)
输出文件应该是每页带有水印的pdf,但是我得到了一个pdf文件,其中原始输入文件的所有页面都被覆盖在一个页面上