给PDF添加水印(Python+C#)

简介:

1、Python + PDFlib

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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, image_file):
     =  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("")

2、C# + iTextSharp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using  System;
using  System.IO;
using  iTextSharp.text;
using  iTextSharp.text.pdf;
 
//给单个文件添加水印,在右上角和左下角各添加一个水印
//所有参数均为全路径文件名
bool  add_watermark( string  srcPdf,  string  dstPdf,  string  imagepath)
{         
     iTextSharp.text.Image img = Image.GetInstance(imagepath);
     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 ***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1385138如需转载请自行联系原作者

RQSLT
相关文章
|
1月前
|
数据挖掘 数据安全/隐私保护 开发者
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
使用Spire.PDF for Python插件从PDF文件提取文字和图片信息
62 0
|
1月前
|
存储 缓存 Python
如何使用Python抓取PDF文件并自动下载到本地
如何使用Python抓取PDF文件并自动下载到本地
31 0
|
1月前
|
数据安全/隐私保护 Python
Python3给图片添加水印
Python3给图片添加水印
60 1
|
2月前
|
编解码 数据可视化 数据挖掘
【办公自动化】用Python将PDF文件转存为图片
【办公自动化】用Python将PDF文件转存为图片
62 1
|
1月前
|
前端开发 JavaScript API
基于ElectronEgg&Python,从零开始打造一款免费的PDF桌面工具
基于ElectronEgg&Python,从零开始打造一款免费的PDF桌面工具
|
1月前
|
JSON 数据可视化 Linux
安利3款Python三方库!轻松实现PDF转图片,最快的只需一行代码!
安利3款Python三方库!轻松实现PDF转图片,最快的只需一行代码!
|
1月前
|
机器学习/深度学习 文字识别 数据安全/隐私保护
Python实现从PDF和图片提取文字的方法总结
Python实现从PDF和图片提取文字的方法总结
43 0
|
1月前
|
数据安全/隐私保护 Python
使用Python脚本实现图片合成PDF功能
使用Python脚本实现图片合成PDF功能
28 0
|
1月前
|
数据安全/隐私保护 计算机视觉 Python
如何使用Python给图片添加水印
如何使用Python给图片添加水印
18 0
|
1月前
|
数据安全/隐私保护 Python
python怎么使用Pillow库来添加图片水印
python怎么使用Pillow库来添加图片水印
21 0

热门文章

最新文章