2 ## * << Haru Free PDF Library 2.0.0 >> -- png_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 *
34 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
35 def error_handler (error_no, detail_no, user_data):
37 printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
42 def draw_image (pdf, filename, x, y, text):
43 page = HPDF_GetCurrentPage (pdf)
45 filename1= "pngsuite/%s" % filename
47 image = HPDF_LoadPngImageFromFile (pdf, filename1)
49 # Draw image to the canvas.
50 HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image),
51 HPDF_Image_GetHeight (image))
54 HPDF_Page_BeginText (page)
55 HPDF_Page_SetTextLeading (page, 16)
56 HPDF_Page_MoveTextPos (page, x, y)
57 HPDF_Page_ShowTextNextLine (page, filename)
58 HPDF_Page_ShowTextNextLine (page, text)
59 HPDF_Page_EndText (page)
65 fname=os.path.realpath(sys.argv[0])
66 fname=fname[:fname.rfind('.')]+'.pdf'
68 pdf = HPDF_New (error_handler, NULL)
70 printf ("error: cannot create PdfDoc object\n")
73 HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL)
76 font = HPDF_GetFont (pdf, "Helvetica", NULL)
78 # add a new page object.
79 page = HPDF_AddPage (pdf)
81 HPDF_Page_SetWidth (page, 550)
82 HPDF_Page_SetHeight (page, 650)
84 dst = HPDF_Page_CreateDestination (page)
85 HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1)
86 HPDF_SetOpenAction(pdf, dst)
88 HPDF_Page_BeginText (page)
89 HPDF_Page_SetFontAndSize (page, font, 20)
90 HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70)
91 HPDF_Page_ShowText (page, "PngDemo")
92 HPDF_Page_EndText (page)
94 HPDF_Page_SetFontAndSize (page, font, 12)
96 draw_image (pdf, "basn0g01.png", 100, HPDF_Page_GetHeight (page) - 150,
98 draw_image (pdf, "basn0g02.png", 200, HPDF_Page_GetHeight (page) - 150,
100 draw_image (pdf, "basn0g04.png", 300, HPDF_Page_GetHeight (page) - 150,
102 draw_image (pdf, "basn0g08.png", 400, HPDF_Page_GetHeight (page) - 150,
105 draw_image (pdf, "basn2c08.png", 100, HPDF_Page_GetHeight (page) - 250,
107 draw_image (pdf, "basn2c16.png", 200, HPDF_Page_GetHeight (page) - 250,
110 draw_image (pdf, "basn3p01.png", 100, HPDF_Page_GetHeight (page) - 350,
112 draw_image (pdf, "basn3p02.png", 200, HPDF_Page_GetHeight (page) - 350,
114 draw_image (pdf, "basn3p04.png", 300, HPDF_Page_GetHeight (page) - 350,
116 draw_image (pdf, "basn3p08.png", 400, HPDF_Page_GetHeight (page) - 350,
119 draw_image (pdf, "basn4a08.png", 100, HPDF_Page_GetHeight (page) - 450,
121 draw_image (pdf, "basn4a16.png", 200, HPDF_Page_GetHeight (page) - 450,
124 draw_image (pdf, "basn6a08.png", 100, HPDF_Page_GetHeight (page) - 550,
126 draw_image (pdf, "basn6a16.png", 200, HPDF_Page_GetHeight (page) - 550,
129 # save the document to a file
130 HPDF_SaveToFile (pdf, fname)
139 printf("WARNING: if you want to run this demo, \n"
140 "make libhpdf with HPDF_USE_PNGLIB option.\n")