1 /* $Id: quickreference.c,v 1.30 2006/10/01 10:27:02 rjs Exp $
2 *
3 * PDFlib+PDI client: mini imposition demo
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "pdflib.h"
10
11 int
main(void)12 main(void)
13 {
14 const double width = 500, height = 770;
15
16 PDF *p;
17 int manual, page;
18 int font, row, col;
19 const int maxrow = 2;
20 const int maxcol = 2;
21 char optlist[128];
22 int endpage=0;
23 int pageno;
24 const char *infile = "reference.pdf";
25
26 /* This is where font/image/PDF input files live. Adjust as necessary. */
27 char *searchpath = "../data";
28
29 /* create a new PDFlib object */
30 if ((p = PDF_new()) == (PDF *) 0)
31 {
32 printf("Couldn't create PDFlib object (out of memory)!\n");
33 return(2);
34 }
35
36 PDF_TRY(p)
37 {
38 /* This means we must check return values of load_font() etc. */
39 PDF_set_parameter(p, "errorpolicy", "return");
40
41 if (PDF_begin_document(p, "quickreference.pdf", 0, "") == -1)
42 {
43 printf("Error: %s\n", PDF_get_errmsg(p));
44 return(2);
45 }
46
47 PDF_set_parameter(p, "SearchPath", searchpath);
48
49 /* This line is required to avoid problems on Japanese systems */
50 PDF_set_parameter(p, "hypertextencoding", "host");
51
52 PDF_set_info(p, "Creator", "quickreference.c");
53 PDF_set_info(p, "Author", "Thomas Merz");
54 PDF_set_info(p, "Title", "mini imposition demo (C)");
55
56 manual = PDF_open_pdi_document(p, infile, 0, "");
57 if (manual == -1)
58 {
59 printf("Error: %s\n", PDF_get_errmsg(p));
60 return(2);
61 }
62
63 row = 0;
64 col = 0;
65
66 PDF_set_parameter(p, "topdown", "true");
67
68 /* Get the number of pages in the input document */
69 endpage = (int) PDF_pcos_get_number(p, manual, "length:pages");
70
71 for (pageno = 1; pageno <= endpage; pageno++)
72 {
73 if (row == 0 && col == 0)
74 {
75 PDF_begin_page_ext(p, width, height, "");
76 font = PDF_load_font(p, "Helvetica-Bold", 0, "host", "");
77 if (font == -1) {
78 printf("Error: %s\n", PDF_get_errmsg(p));
79 PDF_delete(p);
80 return(2);
81 }
82 PDF_setfont(p, font, 18);
83 PDF_set_text_pos(p, 24, 24);
84 PDF_show(p, "PDFlib Quick Reference");
85 }
86
87 page = PDF_open_pdi_page(p, manual, pageno, "");
88
89 if (page == -1)
90 {
91 printf("Error: %s\n", PDF_get_errmsg(p));
92 return(2);
93 }
94
95 sprintf(optlist, "scale %f", 1.0 / maxrow);
96 PDF_fit_pdi_page(p, page,
97 width/maxcol*col, (row + 1) * height/maxrow, optlist);
98 PDF_close_pdi_page(p, page);
99
100 col++;
101 if (col == maxcol)
102 {
103 col = 0;
104 row++;
105 }
106 if (row == maxrow)
107 {
108 row = 0;
109 PDF_end_page_ext(p, "");
110 }
111 }
112
113 /* finish the last partial page */
114 if (row != 0 || col != 0)
115 PDF_end_page_ext(p, "");
116
117 PDF_end_document(p, "");
118 PDF_close_pdi_document(p, manual);
119 }
120
121 PDF_CATCH(p)
122 {
123 printf("PDFlib exception occurred in quickreference sample:\n");
124 printf("[%d] %s: %s\n",
125 PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
126 PDF_delete(p);
127 return(2);
128 }
129
130 PDF_delete(p);
131
132 return 0;
133 }
134