2 ## * << Haru Free PDF Library 2.0.0 >> -- jpeg_demo.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 *
35 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
36 def error_handler (error_no, detail_no, user_data):
38 printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
43 def draw_image (pdf, filename, x, y, text):
45 page = HPDF_GetCurrentPage (pdf)
47 filename1="images/%s" % filename
49 image = HPDF_LoadJpegImageFromFile (pdf, filename1)
51 # Draw image to the canvas.
52 HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image),
53 HPDF_Image_GetHeight (image))
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)
67 fname=os.path.realpath(sys.argv[0])
68 fname=fname[:fname.rfind('.')]+'.pdf'
70 pdf = HPDF_New (error_handler, NULL)
72 printf ("error: cannot create PdfDoc object\n")
75 HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
78 font = HPDF_GetFont (pdf, "Helvetica", NULL)
80 # add a new page object.
81 page = HPDF_AddPage (pdf)
83 HPDF_Page_SetWidth (page, 650)
84 HPDF_Page_SetHeight (page, 500)
86 dst = HPDF_Page_CreateDestination (page)
87 HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1)
88 HPDF_SetOpenAction(pdf, dst)
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)
96 HPDF_Page_SetFontAndSize (page, font, 12)
98 draw_image (pdf, "rgb.jpg", 70, HPDF_Page_GetHeight (page) - 410,
100 draw_image (pdf, "gray.jpg", 340, HPDF_Page_GetHeight (page) - 410,
101 "8bit grayscale image")
103 # save the document to a file
104 HPDF_SaveToFile (pdf, fname)