1### 2## * << Haru Free PDF Library 2.0.0 >> -- png_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 18import os, sys 19 20from ctypes import * 21up=2 22def 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 28setlibpath(up) 29 30from haru import * 31from haru.c_func import * 32from haru.hpdf_errorcode import * 33 34@HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p) 35def error_handler (error_no, detail_no, user_data): 36 global pdf 37 printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no], 38 detail_no) 39 HPDF_Free (pdf) 40 sys.exit(1) 41 42def draw_image (pdf, filename, x, y, text): 43 page = HPDF_GetCurrentPage (pdf) 44 45 filename1= "pngsuite/%s" % filename 46 47 image = HPDF_LoadPngImageFromFile (pdf, filename1) 48 49 # Draw image to the canvas. 50 HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image), 51 HPDF_Image_GetHeight (image)) 52 53 # Print the text. 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) 60 61 62def main(): 63 global pdf 64 65 fname=os.path.realpath(sys.argv[0]) 66 fname=fname[:fname.rfind('.')]+'.pdf' 67 68 pdf = HPDF_New (error_handler, NULL) 69 if (not pdf): 70 printf ("error: cannot create PdfDoc object\n") 71 return 1 72 73 HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL) 74 75 # create default-font 76 font = HPDF_GetFont (pdf, "Helvetica", NULL) 77 78 # add a new page object. 79 page = HPDF_AddPage (pdf) 80 81 HPDF_Page_SetWidth (page, 550) 82 HPDF_Page_SetHeight (page, 650) 83 84 dst = HPDF_Page_CreateDestination (page) 85 HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1) 86 HPDF_SetOpenAction(pdf, dst) 87 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) 93 94 HPDF_Page_SetFontAndSize (page, font, 12) 95 96 draw_image (pdf, "basn0g01.png", 100, HPDF_Page_GetHeight (page) - 150, 97 "1bit grayscale.") 98 draw_image (pdf, "basn0g02.png", 200, HPDF_Page_GetHeight (page) - 150, 99 "2bit grayscale.") 100 draw_image (pdf, "basn0g04.png", 300, HPDF_Page_GetHeight (page) - 150, 101 "4bit grayscale.") 102 draw_image (pdf, "basn0g08.png", 400, HPDF_Page_GetHeight (page) - 150, 103 "8bit grayscale.") 104 105 draw_image (pdf, "basn2c08.png", 100, HPDF_Page_GetHeight (page) - 250, 106 "8bit color.") 107 draw_image (pdf, "basn2c16.png", 200, HPDF_Page_GetHeight (page) - 250, 108 "16bit color.") 109 110 draw_image (pdf, "basn3p01.png", 100, HPDF_Page_GetHeight (page) - 350, 111 "1bit pallet.") 112 draw_image (pdf, "basn3p02.png", 200, HPDF_Page_GetHeight (page) - 350, 113 "2bit pallet.") 114 draw_image (pdf, "basn3p04.png", 300, HPDF_Page_GetHeight (page) - 350, 115 "4bit pallet.") 116 draw_image (pdf, "basn3p08.png", 400, HPDF_Page_GetHeight (page) - 350, 117 "8bit pallet.") 118 119 draw_image (pdf, "basn4a08.png", 100, HPDF_Page_GetHeight (page) - 450, 120 "8bit alpha.") 121 draw_image (pdf, "basn4a16.png", 200, HPDF_Page_GetHeight (page) - 450, 122 "16bit alpha.") 123 124 draw_image (pdf, "basn6a08.png", 100, HPDF_Page_GetHeight (page) - 550, 125 "8bit alpha.") 126 draw_image (pdf, "basn6a16.png", 200, HPDF_Page_GetHeight (page) - 550, 127 "16bit alpha.") 128 129 # save the document to a file 130 HPDF_SaveToFile (pdf, fname) 131 132 # clean up 133 HPDF_Free (pdf) 134 135 return 0 136 137 138if HPDF_NOPNGLIB: 139 printf("WARNING: if you want to run this demo, \n" 140 "make libhpdf with HPDF_USE_PNGLIB option.\n") 141 sys.exit(1) 142else: 143 main()