OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / link_annotation.py
1 ###
2 ## * << Haru Free PDF Library 2.0.0 >> -- link_annotation.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 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
35 def 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
42
43
44 def print_page  (page, font, page_num):
45
46     HPDF_Page_SetWidth (page, 200)
47     HPDF_Page_SetHeight (page, 200)
48
49     HPDF_Page_SetFontAndSize (page, font, 20)
50
51     HPDF_Page_BeginText (page)
52     HPDF_Page_MoveTextPos (page, 50, 150)
53
54     buf= "Page:%d" % page_num
55
56     HPDF_Page_ShowText (page, buf)
57     HPDF_Page_EndText (page)
58
59 def main():
60
61     global pdf
62
63     fname=os.path.realpath(sys.argv[0])
64     fname=fname[:fname.rfind('.')]+'.pdf'
65
66     page=[None for i in range(9)]
67     rect=HPDF_Rect()
68
69     uri = "http://libharu.org"
70
71
72
73     pdf = HPDF_New (error_handler, NULL)
74     if (not pdf):
75         printf ("error: cannot create PdfDoc object\n")
76         return 1
77
78     # create default-font
79     font = HPDF_GetFont (pdf, "Helvetica", NULL)
80
81     # create index page
82     index_page = HPDF_AddPage (pdf)
83     HPDF_Page_SetWidth (index_page, 300)
84     HPDF_Page_SetHeight (index_page, 220)
85
86     # Add 7 pages to the document.
87     for i in range(7):
88         page[i] = HPDF_AddPage (pdf)
89         print_page(page[i], font, i + 1)
90
91     HPDF_Page_BeginText (index_page)
92     HPDF_Page_SetFontAndSize (index_page, font, 10)
93     HPDF_Page_MoveTextPos (index_page, 15, 200)
94     HPDF_Page_ShowText (index_page, "Link Annotation Demo")
95     HPDF_Page_EndText (index_page)
96
97     ##
98     # * Create Link-Annotation object on index page.
99     #
100     HPDF_Page_BeginText(index_page)
101     HPDF_Page_SetFontAndSize (index_page, font, 8)
102     HPDF_Page_MoveTextPos (index_page, 20, 180)
103     HPDF_Page_SetTextLeading (index_page, 23)
104
105     # page1 (HPDF_ANNOT_NO_HIGHTLIGHT)
106     tp = HPDF_Page_GetCurrentTextPos (index_page)
107
108     HPDF_Page_ShowText (index_page, "Jump to Page1 (HilightMode=HPDF_ANNOT_NO_HIGHTLIGHT)")
109     rect.left = tp.x - 4
110     rect.bottom = tp.y - 4
111     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4
112     rect.top = tp.y + 10
113
114     HPDF_Page_MoveToNextLine (index_page)
115
116     dst = HPDF_Page_CreateDestination (page[0])
117
118     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst)
119
120     HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_NO_HIGHTLIGHT)
121
122
123     # page2 (HPDF_ANNOT_INVERT_BOX)
124     tp = HPDF_Page_GetCurrentTextPos (index_page)
125
126     HPDF_Page_ShowText (index_page, "Jump to Page2 (HilightMode=HPDF_ANNOT_INVERT_BOX)")
127     rect.left = tp.x - 4
128     rect.bottom = tp.y - 4
129     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4
130     rect.top = tp.y + 10
131
132     HPDF_Page_MoveToNextLine (index_page)
133
134     dst = HPDF_Page_CreateDestination (page[1])
135
136     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst)
137
138     HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_INVERT_BOX)
139
140
141     # page3 (HPDF_ANNOT_INVERT_BORDER)
142     tp = HPDF_Page_GetCurrentTextPos (index_page)
143
144     HPDF_Page_ShowText (index_page, "Jump to Page3 (HilightMode=HPDF_ANNOT_INVERT_BORDER)")
145     rect.left = tp.x - 4
146     rect.bottom = tp.y - 4
147     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4
148     rect.top = tp.y + 10
149
150     HPDF_Page_MoveToNextLine (index_page)
151
152     dst = HPDF_Page_CreateDestination (page[2])
153
154     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst)
155
156     HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_INVERT_BORDER)
157
158
159     # page4 (HPDF_ANNOT_DOWN_APPEARANCE)
160     tp = HPDF_Page_GetCurrentTextPos (index_page)
161
162     HPDF_Page_ShowText (index_page, "Jump to Page4 (HilightMode=HPDF_ANNOT_DOWN_APPEARANCE)")
163     rect.left = tp.x - 4
164     rect.bottom = tp.y - 4
165     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4
166     rect.top = tp.y + 10
167
168     HPDF_Page_MoveToNextLine (index_page)
169
170     dst = HPDF_Page_CreateDestination (page[3])
171
172     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst)
173
174     HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_DOWN_APPEARANCE)
175
176
177     # page5 (dash border)
178     tp = HPDF_Page_GetCurrentTextPos (index_page)
179
180     HPDF_Page_ShowText (index_page, "Jump to Page5 (dash border)")
181     rect.left = tp.x - 4
182     rect.bottom = tp.y - 4
183     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4
184     rect.top = tp.y + 10
185
186     HPDF_Page_MoveToNextLine (index_page)
187
188     dst = HPDF_Page_CreateDestination (page[4])
189
190     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst)
191
192     HPDF_LinkAnnot_SetBorderStyle (annot, 1, 3, 2)
193
194
195     # page6 (no border)
196     tp = HPDF_Page_GetCurrentTextPos (index_page)
197
198     HPDF_Page_ShowText (index_page, "Jump to Page6 (no border)")
199     rect.left = tp.x - 4
200     rect.bottom = tp.y - 4
201     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4
202     rect.top = tp.y + 10
203
204     HPDF_Page_MoveToNextLine (index_page)
205
206     dst = HPDF_Page_CreateDestination (page[5])
207
208     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst)
209
210     HPDF_LinkAnnot_SetBorderStyle (annot, 0, 0, 0)
211
212
213     # page7 (bold border)
214     tp = HPDF_Page_GetCurrentTextPos (index_page)
215
216     HPDF_Page_ShowText (index_page, "Jump to Page7 (bold border)")
217     rect.left = tp.x - 4
218     rect.bottom = tp.y - 4
219     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4
220     rect.top = tp.y + 10
221
222     HPDF_Page_MoveToNextLine (index_page)
223
224     dst = HPDF_Page_CreateDestination (page[6])
225
226     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst)
227
228     HPDF_LinkAnnot_SetBorderStyle (annot, 2, 0, 0)
229
230
231     # URI link
232     tp = HPDF_Page_GetCurrentTextPos (index_page)
233
234     HPDF_Page_ShowText (index_page, "URI (")
235     HPDF_Page_ShowText (index_page, uri)
236     HPDF_Page_ShowText (index_page, ")")
237
238     rect.left = tp.x - 4
239     rect.bottom = tp.y - 4
240     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4
241     rect.top = tp.y + 10
242
243     HPDF_Page_CreateURILinkAnnot (index_page, rect, uri)
244
245     HPDF_Page_EndText (index_page)
246
247     # save the document to a file
248     HPDF_SaveToFile (pdf, fname)
249
250     # clean up
251     HPDF_Free (pdf)
252
253     return 0
254
255 main()