2 ## * << Alternative PDF Library 1.0.0 >> -- text_demo2.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 *
33 from grid_sheet import *
40 @HPDF_Error_Handler(None, HPDF_UINT, HPDF_UINT, c_void_p)
41 def error_handler (error_no, detail_no, user_data):
42 printf ("ERROR: error_no=%04X, detail_no=%u\n", error_no,
52 pos = HPDF_Page_GetCurrentTextPos (page)
55 buf=".[%d]%0.2f %0.2f" %( no, pos.x, pos.y)
56 HPDF_Page_ShowText(page, buf)
64 SAMP_TXT = "The quick brown fox jumps over the lazy dog. "
66 fname=os.path.realpath(sys.argv[0])
67 fname=fname[:fname.rfind('.')]+'.pdf'
69 pdf = HPDF_New (error_handler, NULL)
71 printf ("error: cannot create PdfDoc object\n")
74 # add a new page object.
75 page = HPDF_AddPage (pdf)
76 HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_A5, HPDF_PAGE_PORTRAIT)
78 print_grid (pdf, page)
80 page_height = HPDF_Page_GetHeight (page)
82 font = HPDF_GetFont (pdf, "Helvetica", NULL)
83 HPDF_Page_SetTextLeading (page, 20)
91 rect.bottom = rect.top - 40
93 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
94 rect.top - rect.bottom)
95 HPDF_Page_Stroke (page)
97 HPDF_Page_BeginText (page)
99 HPDF_Page_SetFontAndSize (page, font, 10)
100 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_LEFT")
102 HPDF_Page_SetFontAndSize (page, font, 13)
103 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
104 SAMP_TXT, HPDF_TALIGN_LEFT, NULL)
106 HPDF_Page_EndText (page)
112 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
113 rect.top - rect.bottom)
114 HPDF_Page_Stroke (page)
116 HPDF_Page_BeginText (page)
118 HPDF_Page_SetFontAndSize (page, font, 10)
119 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_RIGTH")
121 HPDF_Page_SetFontAndSize (page, font, 13)
122 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
123 SAMP_TXT, HPDF_TALIGN_RIGHT, NULL)
125 HPDF_Page_EndText (page)
131 rect.bottom = rect.top - 40
133 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
134 rect.top - rect.bottom)
135 HPDF_Page_Stroke (page)
137 HPDF_Page_BeginText (page)
139 HPDF_Page_SetFontAndSize (page, font, 10)
140 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_CENTER")
142 HPDF_Page_SetFontAndSize (page, font, 13)
143 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
144 SAMP_TXT, HPDF_TALIGN_CENTER, NULL)
146 HPDF_Page_EndText (page)
148 # HPDF_TALIGN_JUSTIFY
152 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
153 rect.top - rect.bottom)
154 HPDF_Page_Stroke (page)
156 HPDF_Page_BeginText (page)
158 HPDF_Page_SetFontAndSize (page, font, 10)
159 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_JUSTIFY")
161 HPDF_Page_SetFontAndSize (page, font, 13)
162 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
163 SAMP_TXT, HPDF_TALIGN_JUSTIFY, NULL)
165 HPDF_Page_EndText (page)
169 # Skewed coordinate system
170 HPDF_Page_GSave (page)
174 rad1 = angle1 / 180 * 3.141592
175 rad2 = angle2 / 180 * 3.141592
177 HPDF_Page_Concat (page, 1, tan(rad1), tan(rad2), 1, 25, 350)
183 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
184 rect.top - rect.bottom)
185 HPDF_Page_Stroke (page)
187 HPDF_Page_BeginText (page)
189 HPDF_Page_SetFontAndSize (page, font, 10)
190 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Skewed coordinate system")
192 HPDF_Page_SetFontAndSize (page, font, 13)
193 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
194 SAMP_TXT, HPDF_TALIGN_LEFT, NULL)
196 HPDF_Page_EndText (page)
198 HPDF_Page_GRestore (page)
201 # Rotated coordinate system
202 HPDF_Page_GSave (page)
205 rad1 = angle1 / 180 * 3.141592
207 HPDF_Page_Concat (page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), 220, 350)
213 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
214 rect.top - rect.bottom)
215 HPDF_Page_Stroke (page)
217 HPDF_Page_BeginText (page)
219 HPDF_Page_SetFontAndSize (page, font, 10)
220 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Rotated coordinate system")
222 HPDF_Page_SetFontAndSize (page, font, 13)
223 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
224 SAMP_TXT, HPDF_TALIGN_LEFT, NULL)
226 HPDF_Page_EndText (page)
228 HPDF_Page_GRestore (page)
231 # text along a circle
232 HPDF_Page_SetGrayStroke (page, 0)
233 HPDF_Page_Circle (page, 210, 190, 145)
234 HPDF_Page_Circle (page, 210, 190, 113)
235 HPDF_Page_Stroke (page)
237 angle1 = 360.0 / (len (SAMP_TXT))
240 HPDF_Page_BeginText (page)
241 font = HPDF_GetFont (pdf, "Courier-Bold", NULL)
242 HPDF_Page_SetFontAndSize (page, font, 30)
244 for i in range(len (SAMP_TXT)):
247 rad1 = (angle2 - 90) / 180 * 3.141592
248 rad2 = angle2 / 180 * 3.141592
250 x = 210 + cos(rad2) * 122
251 y = 190 + sin(rad2) * 122
253 HPDF_Page_SetTextMatrix(page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), x, y)
257 HPDF_Page_ShowText (page, buf)
260 HPDF_Page_EndText (page)
262 # save the document to a file
263 HPDF_SaveToFile (pdf, fname)