OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / outline_demo_jp.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- outline_demo_jp.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
18 import os, sys
19
20 from ctypes import *
21 up=2
22 def 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
28 setlibpath(up)
29
30 from haru import *
31 from haru.c_func import *
32 from haru.hpdf_errorcode import *
33
34 for i in dir():
35     if 'CreateOutLine' in i:
36         print i
37
38
39 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
40 def error_handler (error_no, detail_no, user_data):
41     global pdf
42     printf ("ERROR: %s, detail_no=%u\n", error_detail[error_no],
43                 detail_no)
44     HPDF_Free (pdf)
45     sys.exit(1)
46
47
48 def print_page  (page,  page_num):
49
50     HPDF_Page_SetWidth (page, 200)
51     HPDF_Page_SetHeight (page, 300)
52
53     HPDF_Page_BeginText (page)
54     HPDF_Page_MoveTextPos (page, 50, 250)
55
56     buf="Page:%d" % page_num
57
58     HPDF_Page_ShowText (page, buf)
59     HPDF_Page_EndText (page)
60
61 def main():
62     global pdf
63
64     page=[None for i in range(4)]
65     outline=[None for i in range(4)]
66
67     fname=os.path.realpath(sys.argv[0])
68     fname=fname[:fname.rfind('.')]+'.pdf'
69
70     try:
71         f = open ("mbtext/sjis.txt", "rb")
72     except:
73         printf ("error: cannot open 'mbtext/sjis.txt'\n")
74         return 1
75
76
77     SAMP_TXT=f.read(2048)
78     f.close ()
79
80     pdf = HPDF_New (error_handler, NULL)
81     if (not pdf):
82         printf ("error: cannot create PdfDoc object\n")
83         return 1
84
85     # declaration for using Japanese encoding.
86     HPDF_UseJPEncodings (pdf)
87
88     # create default-font
89     font = HPDF_GetFont (pdf, "Helvetica", NULL)
90
91     # Set page mode to use outlines.
92     HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE)
93
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)
98
99     page[1] = HPDF_AddPage (pdf)
100     HPDF_Page_SetFontAndSize (page[1], font, 20)
101     print_page(page[1], 2)
102
103     page[2] = HPDF_AddPage (pdf)
104     HPDF_Page_SetFontAndSize (page[2], font, 20)
105     print_page(page[2], 3)
106
107     # create outline root.
108     root = HPDF_CreateOutLine (pdf, NULL, "OutlineRoot", NULL)
109     HPDF_Outline_SetOpened (root, HPDF_TRUE)
110
111     outline[0] = HPDF_CreateOutLine (pdf, root, "page1", NULL)
112     outline[1] = HPDF_CreateOutLine (pdf, root, "page2", NULL)
113
114     # create outline with test which is  encoding
115     outline[2] = HPDF_CreateOutLine (pdf, root, SAMP_TXT,
116                     HPDF_GetEncoder (pdf, "90ms-RKSJ-H"))
117
118     # create destination objects on each pages
119     # and link it to outline items.
120
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)
125
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)
129
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)
133
134     # save the document to a file
135     HPDF_SaveToFile (pdf, fname)
136
137     # clean up
138     HPDF_Free (pdf)
139
140     return 0
141
142 main()