给PDF添加水印(Python+C#)
1、python + pdflib
以下是用PDFlib给pdf添加水印的速记,另外PDFStamp是个很好用的pdf水印工具。PDFlib功能比较多、杂;PDFStamp功能单一,更方便使用。据walker测试,PDFlib会比PDFStamp快一些。
#encoding=utf-8
#author: walker
#date: 2014-03-27
from PDFlib.PDFlib import PDFlib
from PDFlib.PDFlib import PDFlibException
#给单个文件添加水印,在右上角和左下角各添加一个水印
#所有参数均为全路径文件名
def add_watermark(pdf_file_in, pdf_file_out, p_w_picpath_file):
p = PDFlib()
p.set_option("license=xxxxx") #your key
p.set_option("errorpolicy=return");
if (p.begin_document(pdf_file_out, "") == -1):
raise PDFlibException("Error: " + p.get_errmsg())
p.set_info("Author", "walker");
p.set_info("Title", "");
p.set_info("Creator", "walker");
p.set_info("Subject", "");
p.set_info("KeyWords", "");
#p.set_info("Producer", "walker");
#输入文件
indoc = p.open_pdi_document(pdf_file_in, "");
if (indoc == -1):
raise PDFlibException("Error: " + p.get_errmsg())
endpage = p.pcos_get_number(indoc, "length:pages");
endpage = int(endpage)
p_w_picpath = p.load_p_w_picpath("auto", p_w_picpath_file, "")
if p_w_picpath == -1:
raise PDFlibException("Error: " + p.get_errmsg())
for pageno in range(1, endpage+1):
page = p.open_pdi_page(indoc, pageno, "");
if (page == -1):
raise PDFlibException("Error: " + p.get_errmsg())
p.begin_page_ext(0, 0, ""); #添加一页
p.fit_pdi_page(page, 0, 0, "adjustpage")
page_width = p.get_value("pagewidth", 0) #单位为像素72dpi下像素值
page_height = p.get_value("pageheight", 0) #单位为像素72dpi下像素值
p_w_picpathwidth = p.info_p_w_picpath(p_w_picpath, "p_w_picpathwidth", "");
p_w_picpathheight = p.info_p_w_picpath(p_w_picpath, "p_w_picpathheight", "");
margin = 1000 #用于设置水印边距
optlist_top = "boxsize={" + str(page_width) + " " + str(page_height) + "} "
optlist_top += "position={" + str(margin/page_width) + " " + str(margin/ page_height) + "} "
optlist_top += " fitmethod=clip dpi=96"
optlist_bottom = "boxsize={" + str(page_width) + " " + str(page_height) + "} "
optlist_bottom += "position={" + str(100 - margin/page_width) + " " + str(100 - margin/ page_height) + "} "
optlist_bottom += " fitmethod=clip dpi=96"
p.fit_p_w_picpath(p_w_picpath, 0, 0, optlist_bottom)
p.fit_p_w_picpath(p_w_picpath, 0, 0, optlist_top)
p.close_pdi_page(page);
p.end_page_ext("");
p.close_p_w_picpath(p_w_picpath)
p.end_document("")
2、C# + iTextSharp
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
//给单个文件添加水印,在右上角和左下角各添加一个水印
//所有参数均为全路径文件名
bool add_watermark(string srcPdf, string dstPdf, string p_w_picpathpath)
{
iTextSharp.text.Image img = Image.GetInstance(p_w_picpathpath);
PdfReader reader = new PdfReader(srcPdf);
PdfStamper stamp = new PdfStamper(reader, new FileStream(dstPdf, FileMode.Create));
PdfContentByte page;
float width = reader.GetPageSize(1).Width;
float height = reader.GetPageSize(1).Height;
int num = reader.NumberOfPages;
for (int i = 1; i <= num; ++i)
{
page = stamp.GetOverContent(i);
img.SetAbsolutePosition(margin, margin);
page.AddImage(img);
img.SetAbsolutePosition(width - img.Width - margin, height - img.Height - margin);
page.AddImage(img);
}
stamp.Close();
reader.Close();
return true;
}
相关阅读:
1、Python添加pdf水印
2、PDFlib8注册机和序列号产生规则
*** walker ***
相关文章