1 /* $Id: starter_table.c,v 1.4.2.5 2007/11/15 15:09:44 rjs Exp $
2  *
3  * Table starter:
4  * Create table which may span multiple pages
5  *
6  * required software: PDFlib/PDFlib+PDI/PPS 7
7  * required data: image file (dummy text created within the program)
8  */
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 
14 #include "pdflib.h"
15 
16 
17 int
main(void)18 main(void)
19 {
20 
21     /* This is where the data files are. Adjust as necessary. */
22     const char * searchpath = "../data";
23 
24     PDF * p;
25 
26     const char * imagefile = "nesrin.jpg";
27 
28     int row, col, font, image, tf=-1, tbl=-1;
29     int rowmax=50, colmax=5;
30 
31     double llx= 50, lly=50, urx=550, ury=800;
32     const char * headertext = "Table header (centered across all columns)";
33     const char * result;
34     char optlist[1024];
35 
36     /* Dummy text for filling a cell with multi-line Textflow */
37     const char * tf_text =
38 "Lorem ipsum dolor sit amet, consectetur adi&shy;pi&shy;sicing elit, \
39 sed do eius&shy;mod tempor incidi&shy;dunt ut labore et dolore magna \
40 ali&shy;qua. Ut enim ad minim ve&shy;niam, quis nostrud exer&shy;citation \
41 ull&shy;amco la&shy;bo&shy;ris nisi ut ali&shy;quip ex ea commodo \
42 con&shy;sequat. \
43 Duis aute irure dolor in repre&shy;henderit in voluptate velit esse \
44 cillum dolore \
45 eu fugiat nulla pari&shy;atur. Excep&shy;teur sint occae&shy;cat \
46 cupi&shy;datat \
47 non proident, sunt in culpa qui officia dese&shy;runt mollit anim id est \
48 laborum. ";
49 
50     /* create a new PDFlib object */
51     if ((p = PDF_new()) == (PDF *) 0) {
52         printf("Couldn't create PDFlib object (out of memory)!\n");
53         return(2);
54     }
55 
56     PDF_TRY(p) {
57         /* This means we must check return values of load_font() etc. */
58         PDF_set_parameter(p, "errorpolicy", "return");
59 
60         PDF_set_parameter(p, "SearchPath", searchpath);
61 
62         if (PDF_begin_document(p, "starter_table.pdf", 0, "") == -1) {
63             printf("Error: %s\n", PDF_get_errmsg(p));
64             PDF_delete(p);
65             return(2);
66         }
67 
68         PDF_set_info(p, "Creator", "PDFlib starter sample");
69         PDF_set_info(p, "Title", "starter_table");
70 
71         /* -------------------- Add table cells -------------------- */
72 
73         /* ---------- Row 1: table header (spans all columns) */
74         row = 1; col = 1;
75         font = PDF_load_font(p, "Times-Bold", 0, "winansi", "");
76 
77         if (font == -1) {
78             printf("Error: %s\n", PDF_get_errmsg(p));
79             PDF_delete(p);
80             return(2);
81         }
82 
83         sprintf(optlist,
84             "fittextline={position=center font=%d fontsize=14} colspan=%d",
85             font, colmax);
86 
87         tbl = PDF_add_table_cell(p, tbl, col, row, headertext, 0, optlist);
88 
89         if (tbl == -1) {
90             printf("Error: adding cell: %s\n", PDF_get_errmsg(p));
91             PDF_delete(p);
92             return(2);
93         }
94 
95         /* ---------- Row 2: various kinds of content */
96         /* ----- Simple text cell */
97         row++; col=1;
98 
99         sprintf(optlist, "fittextline={font=%d fontsize=10 orientate=west}",
100                 font);
101 
102         tbl = PDF_add_table_cell(p, tbl, col, row, "vertical line", 0, optlist);
103 
104         if (tbl == -1) {
105             printf("Error: adding cell: %s\n", PDF_get_errmsg(p));
106             PDF_delete(p);
107             return(2);
108         }
109         /* ----- Colorized background */
110         col++;
111 
112         sprintf(optlist, "fittextline={font=%d fontsize=10} "
113             "matchbox={fillcolor={rgb 0.9 0.5 0}}", font);
114 
115         tbl = PDF_add_table_cell(p, tbl, col, row, "some color", 0, optlist);
116 
117         if (tbl == -1) {
118             printf("Error: adding cell: %s\n", PDF_get_errmsg(p));
119             PDF_delete(p);
120             return(2);
121         }
122         /* ----- Multi-line text with Textflow */
123         col++;
124         font = PDF_load_font(p, "Times-Roman", 0, "winansi", "");
125 
126         if (font == -1) {
127             printf("Error: %s\n", PDF_get_errmsg(p));
128             PDF_delete(p);
129             return(2);
130         }
131 
132         sprintf(optlist,
133             "charref fontname=Times-Roman encoding=winansi fontsize=8");
134 
135         tf = PDF_add_textflow(p, tf, tf_text, 0, optlist);
136         if (tf == -1) {
137             printf("Error: adding cell: %s\n", PDF_get_errmsg(p));
138             PDF_delete(p);
139             return(2);
140         }
141 
142         sprintf(optlist, "margin=2 textflow=%d", tf);
143 
144         tbl = PDF_add_table_cell(p, tbl, col, row, "", 0, optlist);
145 
146         if (tbl == -1) {
147             printf("Error: adding cell: %s\n", PDF_get_errmsg(p));
148             PDF_delete(p);
149             return(2);
150         }
151         /* ----- Rotated image */
152         col++;
153 
154         image = PDF_load_image(p, "auto", imagefile, 0, "");
155 
156         if (image == -1) {
157             printf("Couldn't load image: %s\n", PDF_get_errmsg(p));
158             PDF_delete(p);
159             return(2);
160         }
161 
162         sprintf(optlist, "image=%d fitimage={orientate=west}", image);
163 
164         tbl = PDF_add_table_cell(p, tbl, col, row, "", 0, optlist);
165 
166         if (tbl == -1) {
167             printf("Error: adding cell: %s\n", PDF_get_errmsg(p));
168             PDF_delete(p);
169             return(2);
170         }
171         /* ----- Diagonal stamp */
172         col++;
173 
174         sprintf(optlist,
175             "fittextline={font=%d fontsize=10 stamp=ll2ur}", font);
176 
177         tbl = PDF_add_table_cell(p, tbl, col, row, "entry void", 0, optlist);
178 
179         if (tbl == -1) {
180             printf("Error: adding cell: %s\n", PDF_get_errmsg(p));
181             PDF_delete(p);
182             return(2);
183         }
184         /* ---------- Fill row 3 and above with their numbers */
185         for (row++; row <= rowmax; row++)
186         {
187             for (col = 1; col <= colmax; col++)
188             {
189                 char num[1024];
190 
191                 sprintf(num, "Col %d/Row %d", col, row);
192                 sprintf(optlist,
193                     "colwidth=20%% fittextline={font=%d fontsize=10}", font);
194                 tbl = PDF_add_table_cell(p, tbl, col, row, num, 0, optlist);
195 
196                 if (tbl == -1) {
197                     printf("Error: adding cell: %s\n", PDF_get_errmsg(p));
198                     PDF_delete(p);
199                     return(2);
200                 }
201             }
202         }
203 
204         /* ---------- Place the table on one or more pages ---------- */
205 
206         /*
207          * Loop until all of the table is placed; create new pages
208          * as long as more table instances need to be placed.
209          */
210         do {
211             PDF_begin_page_ext(p, 0, 0, "width=a4.width height=a4.height");
212 
213             /* Shade every other row; draw lines for all table cells.
214              * Add "showcells showborder" to visualize cell borders
215              */
216             sprintf(optlist, "header=1 fill={{area=rowodd "
217                 "fillcolor={gray 0.9}}} stroke={{line=other}} ");
218 
219             /* Place the table instance */
220             result = PDF_fit_table(p, tbl, llx, lly, urx, ury, optlist);
221 
222             if (!strcmp(result, "_error"))
223             {
224                 printf("Could't place table: %s\n", PDF_get_errmsg(p));
225                 PDF_delete(p);
226                 return(2);
227             }
228 
229             PDF_end_page_ext(p, "");
230 
231         } while (!strcmp(result, "_boxfull"));
232 
233         /* Check the result; "_stop" means all is ok. */
234         if (strcmp(result, "_stop"))
235         {
236             if (!strcmp(result, "_error"))
237             {
238                 printf("Error when placing table: %s\n", PDF_get_errmsg(p));
239                 PDF_delete(p);
240                 return(2);
241             }
242             else
243             {
244                 /* Any other return value is a user exit caused by
245                  * the "return" option; this requires dedicated code to
246                  * deal with.
247                  */
248                 printf("User return found in Textflow\n");
249                 PDF_delete(p);
250                 return(2);
251             }
252         }
253 
254         /* This will also delete Textflow handles used in the table */
255         PDF_delete_table(p, tbl, "");
256 
257         PDF_end_document(p, "");
258     }
259 
260     PDF_CATCH(p) {
261         printf("PDFlib exception occurred:\n");
262         printf("[%d] %s: %s\n",
263             PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
264         PDF_delete(p);
265         return(2);
266     }
267 
268     PDF_delete(p);
269 
270     return 0;
271 }
272