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