用PDFlib给PDF添加水印(Python)

   以下是用PDFlib给pdf添加水印的速记,另外PDFStamp是个很好用的pdf水印工具。PDFlib功能比较多、杂;PDFStamp功能单一,更方便使用。据walker测试,PDFlib会比PDFStamp快一些。

#encoding=utf-8
#author: walker
#date: 2014-03-27
#给单个文件添加水印,在右上角和左下角各添加一个水印
#所有参数均为全路径文件名
def add_watermark(pdf_file_in, pdf_file_out, image_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)
                 
    image = p.load_image("auto", image_file, "")
    if image == -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下像素值
                     
        imagewidth = p.info_image(image, "imagewidth", "");
        imageheight = p.info_image(image, "imageheight", "");
                     
        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_image(image, 0, 0, optlist_bottom)
        p.fit_image(image, 0, 0, optlist_top)
                     
        p.close_pdi_page(page);
        p.end_page_ext("");
                 
    p.close_image(image)
    p.end_document("")


相关阅读:

1、Python添加pdf水印

2、PDFlib8注册机和序列号产生规则


*** walker ***


本文出自 “walker的流水账” 博客,请务必保留此出处http://walkerqt.blog.51cto.com/1310630/1385138

用PDFlib给PDF添加水印(Python),布布扣,bubuko.com

用PDFlib给PDF添加水印(Python)

上一篇:Node.js环境配置,Npm切换


下一篇:微信小程序返回上一页面并且修改上一页面数据变量