OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / jpeg_demo.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- jpeg_demo.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
35 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
36 def error_handler (error_no, detail_no, user_data):
37     global pdf
38     printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
39                 detail_no)
40     HPDF_Free (pdf)
41     sys.exit(1)
42
43 def draw_image (pdf, filename, x, y, text):
44
45     page = HPDF_GetCurrentPage (pdf)
46
47     filename1="images/%s" % filename
48
49     image = HPDF_LoadJpegImageFromFile (pdf, filename1)
50
51     # Draw image to the canvas.
52     HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image),
53                 HPDF_Image_GetHeight (image))
54
55     # Print the text.
56     HPDF_Page_BeginText (page)
57     HPDF_Page_SetTextLeading (page, 16)
58     HPDF_Page_MoveTextPos (page, x, y)
59     HPDF_Page_ShowTextNextLine (page, filename)
60     HPDF_Page_ShowTextNextLine (page, text)
61     HPDF_Page_EndText (page)
62
63
64 def main():
65     global pdf
66
67     fname=os.path.realpath(sys.argv[0])
68     fname=fname[:fname.rfind('.')]+'.pdf'
69
70     pdf = HPDF_New (error_handler, NULL)
71     if (not pdf):
72         printf ("error: cannot create PdfDoc object\n")
73         return 1
74
75     HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
76
77     # create default-font
78     font = HPDF_GetFont (pdf, "Helvetica", NULL)
79
80     # add a new page object.
81     page = HPDF_AddPage (pdf)
82
83     HPDF_Page_SetWidth (page, 650)
84     HPDF_Page_SetHeight (page, 500)
85
86     dst = HPDF_Page_CreateDestination (page)
87     HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1)
88     HPDF_SetOpenAction(pdf, dst)
89
90     HPDF_Page_BeginText (page)
91     HPDF_Page_SetFontAndSize (page, font, 20)
92     HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70)
93     HPDF_Page_ShowText (page, "JpegDemo")
94     HPDF_Page_EndText (page)
95
96     HPDF_Page_SetFontAndSize (page, font, 12)
97
98     draw_image (pdf, "rgb.jpg", 70, HPDF_Page_GetHeight (page) - 410,
99                 "24bit color image")
100     draw_image (pdf, "gray.jpg", 340, HPDF_Page_GetHeight (page) - 410,
101                 "8bit grayscale image")
102
103     # save the document to a file
104     HPDF_SaveToFile (pdf, fname)
105
106     # clean up
107     HPDF_Free (pdf)
108
109     return 0
110
111 main()