2 ## * << Haru Free PDF Library 2.0.0 >> -- outline_demo_jp.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 if 'CreateOutLine' in i:
39 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
40 def error_handler (error_no, detail_no, user_data):
42 printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
48 def print_page (page, page_num):
50 HPDF_Page_SetWidth (page, 200)
51 HPDF_Page_SetHeight (page, 300)
53 HPDF_Page_BeginText (page)
54 HPDF_Page_MoveTextPos (page, 50, 250)
56 buf="Page:%d" % page_num
58 HPDF_Page_ShowText (page, buf)
59 HPDF_Page_EndText (page)
64 page=[None for i in range(4)]
65 outline=[None for i in range(4)]
67 fname=os.path.realpath(sys.argv[0])
68 fname=fname[:fname.rfind('.')]+'.pdf'
71 f = open ("mbtext/sjis.txt", "rb")
73 printf ("error: cannot open 'mbtext/sjis.txt'\n")
80 pdf = HPDF_New (error_handler, NULL)
82 printf ("error: cannot create PdfDoc object\n")
85 # declaration for using Japanese encoding.
86 HPDF_UseJPEncodings (pdf)
89 font = HPDF_GetFont (pdf, "Helvetica", NULL)
91 # Set page mode to use outlines.
92 HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE)
94 # Add 3 pages to the document.
95 page[0] = HPDF_AddPage (pdf)
96 HPDF_Page_SetFontAndSize (page[0], font, 20)
97 print_page(page[0], 1)
99 page[1] = HPDF_AddPage (pdf)
100 HPDF_Page_SetFontAndSize (page[1], font, 20)
101 print_page(page[1], 2)
103 page[2] = HPDF_AddPage (pdf)
104 HPDF_Page_SetFontAndSize (page[2], font, 20)
105 print_page(page[2], 3)
107 # create outline root.
108 root = HPDF_CreateOutLine (pdf, NULL, "OutlineRoot", NULL)
109 HPDF_Outline_SetOpened (root, HPDF_TRUE)
111 outline[0] = HPDF_CreateOutLine (pdf, root, "page1", NULL)
112 outline[1] = HPDF_CreateOutLine (pdf, root, "page2", NULL)
114 # create outline with test which is encoding
115 outline[2] = HPDF_CreateOutLine (pdf, root, SAMP_TXT,
116 HPDF_GetEncoder (pdf, "90ms-RKSJ-H"))
118 # create destination objects on each pages
119 # and link it to outline items.
121 dst = HPDF_Page_CreateDestination (page[0])
122 HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page[0]), 1)
123 HPDF_Outline_SetDestination(outline[0], dst)
124 # HPDF_Catalog_SetOpenAction(dst)
126 dst = HPDF_Page_CreateDestination (page[1])
127 HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page[1]), 1)
128 HPDF_Outline_SetDestination(outline[1], dst)
130 dst = HPDF_Page_CreateDestination (page[2])
131 HPDF_Destination_SetXYZ(dst, 0, HPDF_Page_GetHeight(page[2]), 1)
132 HPDF_Outline_SetDestination(outline[2], dst)
134 # save the document to a file
135 HPDF_SaveToFile (pdf, fname)