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):
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("")
|
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
;
}
|
相关阅读:
*** walker ***
本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1385138如需转载请自行联系原作者
RQSLT