OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / encryption.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- encryption.c
3 ## *
4 ## * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
5 ## *
6 ## * Permission to use, copy, modify, distribute and sell this software
7 ## * and its documentation for any purpose is hereby granted without fee,
8 ## * provided that the above copyright notice appear in all copies and
9 ## * that both that copyright notice and this permission notice appear
10 ## * in supporting documentation.
11 ## * It is provided "as is" without express or implied warranty.
12 ## *
13 ##
14
15 ## port to python by Li Jun
16 ## http://groups.google.com/group/pythoncia
17
18 import os, sys
19
20 from ctypes import *
21 up=2
22 def setlibpath(up):
23     import sys
24     path=os.path.normpath(os.path.split(os.path.realpath(__file__))[0]+'\..'*up)
25     if path not in sys.path:
26         sys.path.append(path)
27
28 setlibpath(up)
29
30 from haru import *
31 from haru.c_func import *
32 from haru.hpdf_errorcode import *
33
34 text = "This is an encrypt document example."
35 owner_passwd = "owner"
36 user_passwd = "user"
37
38
39 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
40 def error_handler (error_no, detail_no, user_data):
41     global pdf
42     printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
43                 detail_no)
44     HPDF_Free (pdf)
45     sys.exit(1)
46
47
48 def main ():
49     global  pdf
50
51     fname=os.path.realpath(sys.argv[0])
52     fname=fname[:fname.rfind('.')]+'.pdf'
53
54     pdf = HPDF_New (error_handler, NULL)
55     if (not pdf):
56         printf ("error: cannot create PdfDoc object\n")
57         return 1
58
59     # create default-font
60     font = HPDF_GetFont (pdf, "Helvetica", NULL)
61
62     # add a new page object.
63     page = HPDF_AddPage (pdf)
64
65     HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_B5, HPDF_PAGE_LANDSCAPE)
66
67     HPDF_Page_BeginText (page)
68     HPDF_Page_SetFontAndSize (page, font, 20)
69     tw = HPDF_Page_TextWidth (page, text)
70     HPDF_Page_MoveTextPos (page, (HPDF_Page_GetWidth (page) - tw) / 2,
71                 (HPDF_Page_GetHeight (page)  - 20) / 2)
72     HPDF_Page_ShowText (page, text)
73     HPDF_Page_EndText (page)
74
75     HPDF_SetPassword (pdf, owner_passwd, user_passwd)
76
77     # save the document to a file
78     HPDF_SaveToFile (pdf, fname)
79
80     # clean up
81     HPDF_Free (pdf)
82
83     return 0
84
85 main()