OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / permission.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- permission.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 = "User cannot print and copy this document."
35 owner_passwd = "owner"
36 user_passwd = ""
37
38 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
39 def error_handler (error_no, detail_no, user_data):
40     global pdf
41     printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
42                 detail_no)
43     HPDF_Free (pdf)
44     sys.exit(1)
45
46
47 def main ():
48     global  pdf
49
50     fname=os.path.realpath(sys.argv[0])
51     fname=fname[:fname.rfind('.')]+'.pdf'
52
53     pdf = HPDF_New (error_handler, NULL)
54     if (not pdf):
55         printf ("error: cannot create PdfDoc object\n")
56         return 1
57
58     # create default-font
59     font = HPDF_GetFont (pdf, "Helvetica", NULL)
60
61     # add a new page object.
62     page = HPDF_AddPage (pdf)
63
64     HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_B5, HPDF_PAGE_LANDSCAPE)
65
66     HPDF_Page_BeginText (page)
67     HPDF_Page_SetFontAndSize (page, font, 20)
68     tw = HPDF_Page_TextWidth (page, text)
69     HPDF_Page_MoveTextPos (page, (HPDF_Page_GetWidth (page) - tw) / 2,
70                 (HPDF_Page_GetHeight (page)  - 20) / 2)
71     HPDF_Page_ShowText (page, text)
72     HPDF_Page_EndText (page)
73
74     HPDF_SetPassword (pdf, owner_passwd, user_passwd)
75     HPDF_SetPermission (pdf, HPDF_ENABLE_READ)
76     HPDF_SetEncryptionMode (pdf, HPDF_ENCRYPT_R3, 16)
77
78     # save the document to a file
79     HPDF_SaveToFile (pdf, fname)
80
81     # clean up
82     HPDF_Free (pdf)
83
84     return 0
85
86 main()