OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / if / python / demo / text_annotation.py
1 #coding=utf-8
2 ###
3 ## * << Haru Free PDF Library 2.0.0 >> -- text_annotation.c
4 ## *
5 ## * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
6 ## *
7 ## * Permission to use, copy, modify, distribute and sell this software
8 ## * and its documentation for any purpose is hereby granted without fee,
9 ## * provided that the above copyright notice appear in all copies and
10 ## * that both that copyright notice and this permission notice appear
11 ## * in supporting documentation.
12 ## * It is provided "as is" without express or implied warranty.
13 ## *
14 ##
15
16 ## port to python by Li Jun
17 ## http://groups.google.com/group/pythoncia
18
19 import os, sys
20
21 from ctypes import *
22 up=2
23 def setlibpath(up):
24     import sys
25     path=os.path.normpath(os.path.split(os.path.realpath(__file__))[0]+'\..'*up)
26     if path not in sys.path:
27         sys.path.append(path)
28
29 setlibpath(up)
30
31 from haru import *
32 from haru.c_func import *
33 from haru.hpdf_errorcode import *
34
35
36 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
37 def 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
45 def main():
46     rect1 =HPDF_Rect(50, 350, 150, 400)
47     rect2 =HPDF_Rect(210, 350, 350, 400)
48     rect3 =HPDF_Rect(50, 250, 150, 300)
49     rect4 =HPDF_Rect(210, 250, 350, 300)
50     rect5 =HPDF_Rect(50, 150, 150, 200)
51     rect6 =HPDF_Rect(210, 150, 350, 200)
52     rect7 =HPDF_Rect(50, 50, 150, 100)
53     rect8 =HPDF_Rect(210, 50, 350, 100)
54
55     global  pdf
56
57     fname=os.path.realpath(sys.argv[0])
58     fname=fname[:fname.rfind('.')]+'.pdf'
59
60     pdf = HPDF_New (error_handler, NULL)
61     if (not pdf):
62         printf ("error: cannot create PdfDoc object\n")
63         return 1
64
65     # use Times-Roman font.
66     font = HPDF_GetFont (pdf, "Times-Roman", "WinAnsiEncoding")
67
68     page = HPDF_AddPage (pdf)
69
70     HPDF_Page_SetWidth (page, 400)
71     HPDF_Page_SetHeight (page, 500)
72
73     HPDF_Page_BeginText (page)
74     HPDF_Page_SetFontAndSize (page, font, 16)
75     HPDF_Page_MoveTextPos (page, 130, 450)
76     HPDF_Page_ShowText (page, "Annotation Demo")
77     HPDF_Page_EndText (page)
78
79
80     annot = HPDF_Page_CreateTextAnnot (page, rect1, "Annotation with Comment "
81                 "Icon. \n This annotation set to be opened initially.",
82                 NULL)
83
84     HPDF_TextAnnot_SetIcon (annot, HPDF_ANNOT_ICON_COMMENT)
85     HPDF_TextAnnot_SetOpened (annot, HPDF_TRUE)
86
87     annot = HPDF_Page_CreateTextAnnot (page, rect2,
88                 "Annotation with Key Icon", NULL)
89     HPDF_TextAnnot_SetIcon (annot, HPDF_ANNOT_ICON_PARAGRAPH)
90
91     annot = HPDF_Page_CreateTextAnnot (page, rect3,
92                 "Annotation with Note Icon", NULL)
93     HPDF_TextAnnot_SetIcon (annot, HPDF_ANNOT_ICON_NOTE)
94
95     annot = HPDF_Page_CreateTextAnnot (page, rect4,
96                 "Annotation with Help Icon", NULL)
97     HPDF_TextAnnot_SetIcon (annot, HPDF_ANNOT_ICON_HELP)
98
99     annot = HPDF_Page_CreateTextAnnot (page, rect5,
100                 "Annotation with NewParagraph Icon", NULL)
101     HPDF_TextAnnot_SetIcon (annot, HPDF_ANNOT_ICON_NEW_PARAGRAPH)
102
103     annot = HPDF_Page_CreateTextAnnot (page, rect6,
104                 "Annotation with Paragraph Icon", NULL)
105     HPDF_TextAnnot_SetIcon (annot, HPDF_ANNOT_ICON_PARAGRAPH)
106
107     annot = HPDF_Page_CreateTextAnnot (page, rect7,
108                 "Annotation with Insert Icon", NULL)
109     HPDF_TextAnnot_SetIcon (annot, HPDF_ANNOT_ICON_INSERT)
110
111     encoding = HPDF_GetEncoder (pdf, "ISO8859-2")
112
113     #HPDF_Page_CreateTextAnnot (page, rect8,
114     #            "Annotation with ISO8859 text 釉罩棕?, encoding)
115
116     HPDF_Page_SetFontAndSize (page, font, 11)
117
118     HPDF_Page_BeginText (page)
119     HPDF_Page_MoveTextPos (page, rect1.left + 35, rect1.top - 20)
120     HPDF_Page_ShowText (page, "Comment Icon.")
121     HPDF_Page_EndText (page)
122
123     HPDF_Page_BeginText (page)
124     HPDF_Page_MoveTextPos (page, rect2.left + 35, rect2.top - 20)
125     HPDF_Page_ShowText (page, "Key Icon")
126     HPDF_Page_EndText (page)
127
128     HPDF_Page_BeginText (page)
129     HPDF_Page_MoveTextPos (page, rect3.left + 35, rect3.top - 20)
130     HPDF_Page_ShowText (page, "Note Icon.")
131     HPDF_Page_EndText (page)
132
133     HPDF_Page_BeginText (page)
134     HPDF_Page_MoveTextPos (page, rect4.left + 35, rect4.top - 20)
135     HPDF_Page_ShowText (page, "Help Icon")
136     HPDF_Page_EndText (page)
137
138     HPDF_Page_BeginText (page)
139     HPDF_Page_MoveTextPos (page, rect5.left + 35, rect5.top - 20)
140     HPDF_Page_ShowText (page, "NewParagraph Icon")
141     HPDF_Page_EndText (page)
142
143     HPDF_Page_BeginText (page)
144     HPDF_Page_MoveTextPos (page, rect6.left + 35, rect6.top - 20)
145     HPDF_Page_ShowText (page, "Paragraph Icon")
146     HPDF_Page_EndText (page)
147
148     HPDF_Page_BeginText (page)
149     HPDF_Page_MoveTextPos (page, rect7.left + 35, rect7.top - 20)
150     HPDF_Page_ShowText (page, "Insert Icon")
151     HPDF_Page_EndText (page)
152
153     HPDF_Page_BeginText (page)
154     HPDF_Page_MoveTextPos (page, rect8.left + 35, rect8.top - 20)
155     HPDF_Page_ShowText (page, "Text Icon(ISO8859-2 text)")
156     HPDF_Page_EndText (page)
157
158
159     # save the document to a file
160     HPDF_SaveToFile (pdf, fname)
161
162     # clean up
163     HPDF_Free (pdf)
164
165     return 0
166
167 main()