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.
21 #include "grid_sheet.h"
30 error_handler (HPDF_STATUS error_no,
31 HPDF_STATUS detail_no,
34 printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
35 (HPDF_UINT)detail_no);
42 PrintText(HPDF_Page page)
45 HPDF_Point pos = HPDF_Page_GetCurrentTextPos (page);
49 _snprintf (buf, 512, ".[%d]%0.2f %0.2f", no, pos.x, pos.y);
51 snprintf (buf, 512, ".[%d]%0.2f %0.2f", no, pos.x, pos.y);
53 HPDF_Page_ShowText(page, buf);
58 main (int argc, char **argv)
68 HPDF_REAL page_height;
72 const char* SAMP_TXT = "The quick brown fox jumps over the lazy dog. ";
74 strcpy (fname, argv[0]);
75 strcat (fname, ".pdf");
77 pdf = HPDF_New (error_handler, NULL);
79 printf ("error: cannot create PdfDoc object\n");
88 /* add a new page object. */
89 page = HPDF_AddPage (pdf);
90 HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_A5, HPDF_PAGE_PORTRAIT);
92 print_grid (pdf, page);
94 page_height = HPDF_Page_GetHeight (page);
96 font = HPDF_GetFont (pdf, "Helvetica", NULL);
97 HPDF_Page_SetTextLeading (page, 20);
99 /* text_rect method */
101 /* HPDF_TALIGN_LEFT */
105 rect.bottom = rect.top - 40;
107 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
108 rect.top - rect.bottom);
109 HPDF_Page_Stroke (page);
111 HPDF_Page_BeginText (page);
113 HPDF_Page_SetFontAndSize (page, font, 10);
114 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_LEFT");
116 HPDF_Page_SetFontAndSize (page, font, 13);
117 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
118 SAMP_TXT, HPDF_TALIGN_LEFT, NULL);
120 HPDF_Page_EndText (page);
122 /* HPDF_TALIGN_RIGTH */
126 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
127 rect.top - rect.bottom);
128 HPDF_Page_Stroke (page);
130 HPDF_Page_BeginText (page);
132 HPDF_Page_SetFontAndSize (page, font, 10);
133 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_RIGTH");
135 HPDF_Page_SetFontAndSize (page, font, 13);
136 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
137 SAMP_TXT, HPDF_TALIGN_RIGHT, NULL);
139 HPDF_Page_EndText (page);
141 /* HPDF_TALIGN_CENTER */
145 rect.bottom = rect.top - 40;
147 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
148 rect.top - rect.bottom);
149 HPDF_Page_Stroke (page);
151 HPDF_Page_BeginText (page);
153 HPDF_Page_SetFontAndSize (page, font, 10);
154 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_CENTER");
156 HPDF_Page_SetFontAndSize (page, font, 13);
157 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
158 SAMP_TXT, HPDF_TALIGN_CENTER, NULL);
160 HPDF_Page_EndText (page);
162 /* HPDF_TALIGN_JUSTIFY */
166 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
167 rect.top - rect.bottom);
168 HPDF_Page_Stroke (page);
170 HPDF_Page_BeginText (page);
172 HPDF_Page_SetFontAndSize (page, font, 10);
173 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_JUSTIFY");
175 HPDF_Page_SetFontAndSize (page, font, 13);
176 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
177 SAMP_TXT, HPDF_TALIGN_JUSTIFY, NULL);
179 HPDF_Page_EndText (page);
183 /* Skewed coordinate system */
184 HPDF_Page_GSave (page);
188 rad1 = angle1 / 180 * 3.141592;
189 rad2 = angle2 / 180 * 3.141592;
191 HPDF_Page_Concat (page, 1, tan(rad1), tan(rad2), 1, 25, 350);
197 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
198 rect.top - rect.bottom);
199 HPDF_Page_Stroke (page);
201 HPDF_Page_BeginText (page);
203 HPDF_Page_SetFontAndSize (page, font, 10);
204 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Skewed coordinate system");
206 HPDF_Page_SetFontAndSize (page, font, 13);
207 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
208 SAMP_TXT, HPDF_TALIGN_LEFT, NULL);
210 HPDF_Page_EndText (page);
212 HPDF_Page_GRestore (page);
215 /* Rotated coordinate system */
216 HPDF_Page_GSave (page);
219 rad1 = angle1 / 180 * 3.141592;
221 HPDF_Page_Concat (page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), 220, 350);
227 HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
228 rect.top - rect.bottom);
229 HPDF_Page_Stroke (page);
231 HPDF_Page_BeginText (page);
233 HPDF_Page_SetFontAndSize (page, font, 10);
234 HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Rotated coordinate system");
236 HPDF_Page_SetFontAndSize (page, font, 13);
237 HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
238 SAMP_TXT, HPDF_TALIGN_LEFT, NULL);
240 HPDF_Page_EndText (page);
242 HPDF_Page_GRestore (page);
245 /* text along a circle */
246 HPDF_Page_SetGrayStroke (page, 0);
247 HPDF_Page_Circle (page, 210, 190, 145);
248 HPDF_Page_Circle (page, 210, 190, 113);
249 HPDF_Page_Stroke (page);
251 angle1 = 360 / (strlen (SAMP_TXT));
254 HPDF_Page_BeginText (page);
255 font = HPDF_GetFont (pdf, "Courier-Bold", NULL);
256 HPDF_Page_SetFontAndSize (page, font, 30);
258 for (i = 0; i < strlen (SAMP_TXT); i++) {
263 rad1 = (angle2 - 90) / 180 * 3.141592;
264 rad2 = angle2 / 180 * 3.141592;
266 x = 210 + cos(rad2) * 122;
267 y = 190 + sin(rad2) * 122;
269 HPDF_Page_SetTextMatrix(page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), x, y);
271 buf[0] = SAMP_TXT[i];
273 HPDF_Page_ShowText (page, buf);
277 HPDF_Page_EndText (page);
279 /* save the document to a file */
280 HPDF_SaveToFile (pdf, fname);