2 ## * << Haru Free PDF Library 2.0.0 >> -- encryption.c
4 ## * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
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.
15 ## port to python by Li Jun
16 ## http://groups.google.com/group/pythoncia
24 path=os.path.normpath(os.path.split(os.path.realpath(__file__))[0]+'\..'*up)
25 if path not in sys.path:
31 from haru.c_func import *
32 from haru.hpdf_errorcode import *
34 text = "This is an encrypt document example."
35 owner_passwd = "owner"
39 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
40 def error_handler (error_no, detail_no, user_data):
42 printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
51 fname=os.path.realpath(sys.argv[0])
52 fname=fname[:fname.rfind('.')]+'.pdf'
54 pdf = HPDF_New (error_handler, NULL)
56 printf ("error: cannot create PdfDoc object\n")
60 font = HPDF_GetFont (pdf, "Helvetica", NULL)
62 # add a new page object.
63 page = HPDF_AddPage (pdf)
65 HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_B5, HPDF_PAGE_LANDSCAPE)
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)
75 HPDF_SetPassword (pdf, owner_passwd, user_passwd)
77 # save the document to a file
78 HPDF_SaveToFile (pdf, fname)