1#coding=ISO8859-2 2 3### 4## * << Haru Free PDF Library 2.0.0 >> -- outline_demo.c 5## * 6## * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp> 7## * 8## * Permission to use, copy, modify, distribute and sell this software 9## * and its documentation for any purpose is hereby granted without fee, 10## * provided that the above copyright notice appear in all copies and 11## * that both that copyright notice and this permission notice appear 12## * in supporting documentation. 13## * It is provided "as is" without express or implied warranty. 14## * 15## 16 17## port to python by Li Jun 18## http://groups.google.com/group/pythoncia 19 20import os, sys 21 22from ctypes import * 23up=2 24def setlibpath(up): 25 import sys 26 path=os.path.normpath(os.path.split(os.path.realpath(__file__))[0]+'\..'*up) 27 if path not in sys.path: 28 sys.path.append(path) 29 30setlibpath(up) 31 32from haru import * 33from haru.c_func import * 34from haru.hpdf_errorcode import * 35 36@HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p) 37def error_handler (error_no, detail_no, user_data): 38 global pdf 39 printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no], 40 detail_no) 41 HPDF_Free (pdf) 42 sys.exit(1) 43 44 45def print_page (page, page_num): 46 47 HPDF_Page_SetWidth (page, 800) 48 HPDF_Page_SetHeight (page, 800) 49 50 HPDF_Page_BeginText (page) 51 HPDF_Page_MoveTextPos (page, 30, 740) 52 53 buf="Page:%d" % page_num 54 55 56 HPDF_Page_ShowText (page, buf) 57 HPDF_Page_EndText (page) 58 59 60def main(): 61 global pdf 62 page=[None for i in range(4)] 63 outline=[None for i in range(4)] 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 # create default-font 74 font = HPDF_GetFont (pdf, "Helvetica", NULL) 75 76 # Set page mode to use outlines. 77 HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE) 78 79 # Add 3 pages to the document. 80 page[0] = HPDF_AddPage (pdf) 81 HPDF_Page_SetFontAndSize (page[0], font, 30) 82 print_page(page[0], 1) 83 84 page[1] = HPDF_AddPage (pdf) 85 HPDF_Page_SetFontAndSize (page[1], font, 30) 86 print_page(page[1], 2) 87 88 page[2] = HPDF_AddPage (pdf) 89 HPDF_Page_SetFontAndSize (page[2], font, 30) 90 print_page(page[2], 3) 91 92 # create outline root. 93 root = HPDF_CreateOutline (pdf, NULL, "OutlineRoot", NULL) 94 HPDF_Outline_SetOpened (root, HPDF_TRUE) 95 96 outline[0] = HPDF_CreateOutline (pdf, root, "page1", NULL) 97 outline[1] = HPDF_CreateOutline (pdf, root, "page2", NULL) 98 99 # create outline with test which is ISO8859-2 encoding 100 #outline[2] = HPDF_CreateOutline (pdf, root, "ISO8859-2 text 釉罩棕?, 101 # HPDF_GetEncoder (pdf, "ISO8859-2")) 102 103 # create destination objects on each pages 104 # and link it to outline items. 105 106 dst = HPDF_Page_CreateDestination (page[0]) 107 HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page[0]), 1) 108 HPDF_Outline_SetDestination(outline[0], dst) 109 #HPDF_Catalog_SetOpenAction(dst) 110 111 dst = HPDF_Page_CreateDestination (page[1]) 112 HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page[1]), 1) 113 HPDF_Outline_SetDestination(outline[1], dst) 114 115 dst = HPDF_Page_CreateDestination (page[2]) 116 HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page[2]), 1) 117 HPDF_Outline_SetDestination(outline[2], dst) 118 119 # save the document to a file 120 HPDF_SaveToFile (pdf, fname) 121 122 # clean up 123 HPDF_Free (pdf) 124 125 return 0 126 127main()