int main(void) { PDF *p; int font, count; p = PDF_new(); /* open new PDF file */ if (PDF_open_file(p, "output.pdf") == -1) { fprintf(stderr, "Error: cannot open PDF file hello_c.pdf.\n"); exit(2); } PDF_set_info(p, "Creator", "hello.c"); PDF_set_info(p, "Author", "Thomas Merz"); PDF_set_info(p, "Title", "Hello, world (C)!"); font = PDF_findfont(p, "Helvetica-Bold", "host", 0); for(count = 0; count < 10000; count++){ PDF_begin_page(p, a4_width, a4_height); /* start a new page */ PDF_setfont(p, font, 24); PDF_set_text_pos(p, 10, 10); PDF_show(p, "x"); PDF_end_page(p); /* close page */ } PDF_close(p); /* close PDF document */ exit(0); }
int main(void) { PDF *p; int font; p = PDF_new(); /* open new PDF file */ if (PDF_open_file(p, "hello_c.pdf") == -1) { fprintf(stderr, "Error: cannot open PDF file hello_c.pdf.\n"); exit(2); } PDF_set_info(p, "Creator", "hello.c"); PDF_set_info(p, "Author", "Thomas Merz"); PDF_set_info(p, "Title", "Hello, world (C)!"); PDF_begin_page(p, a4_width, a4_height); /* start a new page */ font = PDF_findfont(p, "Helvetica-Bold", "default", 0); if (font == -1) { fprintf(stderr, "Couldn't set font!\n"); exit(3); } PDF_setfont(p, font, 24); PDF_set_text_pos(p, 50, 700); PDF_show(p, "Hello, world!"); PDF_continue_text(p, "(says C)"); PDF_end_page(p); /* close page */ PDF_close(p); /* close PDF document */ exit(0); }
int main(void) { PDF *p; int manual, page; int font, row, col; const int maxrow = 2; const int maxcol = 2; char optlist[128]; int startpage = 1, endpage = 4; const float width = 500, height = 770; int pageno; const char *infile = "reference.pdf"; /* This is where font/image/PDF input files live. Adjust as necessary. */ char *searchpath = "../data"; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn't create PDFlib object (out of memory)!\n"); return(2); } PDF_TRY(p) { /* open new PDF file */ if (PDF_open_file(p, "quickreference.pdf") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } PDF_set_parameter(p, "SearchPath", searchpath); /* This line is required to avoid problems on Japanese systems */ PDF_set_parameter(p, "hypertextencoding", "host"); PDF_set_info(p, "Creator", "quickreference.c"); PDF_set_info(p, "Author", "Thomas Merz"); PDF_set_info(p, "Title", "mini imposition demo (C)"); manual = PDF_open_pdi(p, infile, "", 0); if (manual == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } row = 0; col = 0; PDF_set_parameter(p, "topdown", "true"); for (pageno = startpage; pageno <= endpage; pageno++) { if (row == 0 && col == 0) { PDF_begin_page(p, width, height); font = PDF_load_font(p, "Helvetica-Bold", 0, "host", ""); PDF_setfont(p, font, 18); PDF_set_text_pos(p, 24, 24); PDF_show(p, "PDFlib Quick Reference"); } page = PDF_open_pdi_page(p, manual, pageno, ""); if (page == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } sprintf(optlist, "scale %f", (float) 1/maxrow); PDF_fit_pdi_page(p, page, width/maxcol*col, (row + 1) * height/maxrow, optlist); PDF_close_pdi_page(p, page); col++; if (col == maxcol) { col = 0; row++; } if (row == maxrow) { row = 0; PDF_end_page(p); } } /* finish the last partial page */ if (row != 0 || col != 0) PDF_end_page(p); PDF_close(p); PDF_close_pdi(p, manual); } PDF_CATCH(p) { printf("PDFlib exception occurred in quickreference sample:\n"); printf("[%d] %s: %s\n", PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_delete(p); return 0; }
int main(int argc, char *argv[]) { char *pdffilename = NULL; FILE *imagefile; PDF *p; int image; int opt; int resolution = 0; int page_numbering = 0; int current_page = 1; char optlist[128]; while ((opt = getopt(argc, argv, "r:o:p:w")) != -1) switch (opt) { case 'o': pdffilename = optarg; break; case 'p': page_numbering = 1; if (optarg) { current_page = atoi(optarg); } break; case 'r': if (!optarg || (resolution = atoi(optarg)) <= 0) { fprintf(stderr, "Error: non-positive resolution.\n"); usage(); } case '?': default: usage(); } if (optind == argc) { fprintf(stderr, "Error: no image files given.\n"); usage(); } if (pdffilename == NULL) { fprintf(stderr, "Error: no output file given.\n"); usage(); } p = PDF_new(); if (PDF_open_file(p, pdffilename) == -1) { fprintf(stderr, "Error: cannot open output file %s.\n", pdffilename); exit(1); } PDF_set_info(p, "Creator", "pdfimage"); while (optind++ < argc) { fprintf(stderr, "Processing image file '%s'...\n", argv[optind-1]); image = PDF_load_image(p, "auto", argv[optind-1], 0, ""); if (image == -1) { fprintf(stderr, "Error: %s (skipped).\n", PDF_get_errmsg(p)); continue; } /* dummy page size, will be adjusted later */ PDF_begin_page(p, 20, 20); /* define outline with filename or page number */ if (page_numbering) { char buf[32]; sprintf(buf, "Page %d", current_page++); PDF_add_bookmark(p, buf, 0, 0); } else { PDF_add_bookmark(p, argv[optind-1], 0, 0); } if (resolution != 0) sprintf(optlist, "dpi %d", resolution); else sprintf(optlist, "adjustpage"); PDF_fit_image(p, image, 0.0, 0.0, optlist); PDF_end_page(p); } PDF_close(p); PDF_delete(p); exit(0); }
int main(void) { PDF *p; int image; char *imagefile = "nesrin.jpg"; /* This is where font/image/PDF input files live. Adjust as necessary. */ char *searchpath = "../data"; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn't create PDFlib object (out of memory)!\n"); return(2); } PDF_TRY(p){ /* open new PDF file */ if (PDF_open_file(p, "image.pdf") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } PDF_set_parameter(p, "SearchPath", searchpath); /* This line is required to avoid problems on Japanese systems */ PDF_set_parameter(p, "hypertextencoding", "host"); PDF_set_info(p, "Creator", "image.c"); PDF_set_info(p, "Author", "Thomas Merz"); PDF_set_info(p, "Title", "image sample (C)"); image = PDF_load_image(p, "auto", imagefile, 0, ""); if (image == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(3); } /* dummy page size, will be adjusted by PDF_fit_image() */ PDF_begin_page(p, 10, 10); PDF_fit_image(p, image, (float) 0.0, (float) 0.0, "adjustpage"); PDF_close_image(p, image); PDF_end_page(p); /* close page */ PDF_close(p); /* close PDF document */ } PDF_CATCH(p) { printf("PDFlib exception occurred in image sample:\n"); printf("[%d] %s: %s\n", PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_delete(p); /* delete the PDFlib object */ return 0; }
int main(void) { PDF *p; float alpha; time_t timer; struct tm ltime; /* create a new PDFlib object */ if ((p = PDF_new()) == (PDF *) 0) { printf("Couldn't create PDFlib object (out of memory)!\n"); return(2); } PDF_TRY(p) { /* open new PDF file */ if (PDF_open_file(p, "pdfclock.pdf") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(2); } /* This line is required to avoid problems on Japanese systems */ PDF_set_parameter(p, "hypertextencoding", "host"); PDF_set_info(p, "Creator", "pdfclock.c"); PDF_set_info(p, "Author", "Thomas Merz"); PDF_set_info(p, "Title", "PDF clock (C)"); PDF_begin_page(p, (float) (2 * (RADIUS + MARGIN)), (float) (2 * (RADIUS + MARGIN))); PDF_translate(p, RADIUS + MARGIN, RADIUS + MARGIN); PDF_setcolor(p, "fillstroke", "rgb", 0, 0, 1, 0); PDF_save(p); /* minute strokes */ PDF_setlinewidth(p, 2); for (alpha = 0; alpha < 360; alpha += 6) { PDF_rotate(p, 6); PDF_moveto(p, RADIUS, 0); PDF_lineto(p, (float) (RADIUS-MARGIN/3), 0); PDF_stroke(p); } PDF_restore(p); PDF_save(p); /* 5 minute strokes */ PDF_setlinewidth(p, 3); for (alpha = 0; alpha < 360; alpha += 30) { PDF_rotate(p, 30); PDF_moveto(p, RADIUS, 0); PDF_lineto(p, RADIUS-MARGIN, 0); PDF_stroke(p); } time(&timer); ltime = *localtime(&timer); /* draw hour hand */ PDF_save(p); PDF_rotate(p, (float)(-((ltime.tm_min/60.0) + ltime.tm_hour - 3.0) * 30.0)); PDF_moveto(p, -RADIUS/10, -RADIUS/20); PDF_lineto(p, RADIUS/2, 0); PDF_lineto(p, -RADIUS/10, RADIUS/20); PDF_closepath(p); PDF_fill(p); PDF_restore(p); /* draw minute hand */ PDF_save(p); PDF_rotate(p, (float) (-((ltime.tm_sec/60.0) + ltime.tm_min - 15.0) * 6.0)); PDF_moveto(p, -RADIUS/10, -RADIUS/20); PDF_lineto(p, RADIUS * 0.8f, 0); PDF_lineto(p, -RADIUS/10, RADIUS/20); PDF_closepath(p); PDF_fill(p); PDF_restore(p); /* draw second hand */ PDF_setcolor(p, "fillstroke", "rgb", 1, 0, 0, 0); PDF_setlinewidth(p, 2); PDF_save(p); PDF_rotate(p, (float) -((ltime.tm_sec - 15.0) * 6.0)); PDF_moveto(p, -RADIUS/5, 0); PDF_lineto(p, RADIUS, 0); PDF_stroke(p); PDF_restore(p); /* draw little circle at center */ PDF_circle(p, 0, 0, (float) RADIUS/30); PDF_fill(p); PDF_restore(p); PDF_end_page(p); PDF_close(p); } PDF_CATCH(p) { printf("PDFlib exception occurred in pdfclock sample:\n"); printf("[%d] %s: %s\n", PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_delete(p); /* delete the PDFlib object */ return 0; }
int main(int argc, char *argv[]) { char *pdffilename = NULL; char *pdfversion = NULL; PDF *p; int opt; int doc, page; int pageno, docpages; char *filename; int quiet = 0, landscape = 0, boxes = 0, newpage = 0; int cols = 1, rows = 1; int c = 0, r = 0; float sheetwidth = 595.0f, sheetheight = 842.0f; float width, height, scale = 1.0f; float rowheight = 0.0f, colwidth = 0.0f; while ((opt = getopt(argc, argv, "bg:lnp:o:qv:")) != -1) switch (opt) { case 'b': boxes = 1; break; case 'g': if (sscanf(optarg, "%dx%d", &rows, &cols) != 2) { fprintf(stderr, "Error: Couldn't parse -g option.\n"); usage(); } if (rows <= 0 || cols <= 0) { fprintf(stderr, "Bad row or column number.\n"); usage(); } break; case 'l': landscape = 1; break; case 'n': newpage = 1; break; case 'p': for(c = 0; c < PAGESIZELISTLEN; c++) if (!strcmp((const char *) optarg, PageSizes[c].name)) { sheetheight = PageSizes[c].height; sheetwidth = PageSizes[c].width; break; } if (c == PAGESIZELISTLEN) { /* page size name not found */ fprintf(stderr, "Error: Unknown page size '%s'.\n", optarg); usage(); } break; case 'o': pdffilename = optarg; break; case 'v': pdfversion = optarg; if (strcmp(pdfversion, "1.3") && strcmp(pdfversion, "1.4") && strcmp(pdfversion, "1.5")) { fprintf(stderr, "Error: bad PDF version number '%s'.\n", optarg); usage(); } break; case 'q': quiet = 1; break; case '?': default: usage(); } if (optind == argc) { fprintf(stderr, "Error: no PDF files given.\n"); usage(); } if (pdffilename == NULL) { fprintf(stderr, "Error: no PDF output file given.\n"); usage(); } p = PDF_new(); if (pdfversion) PDF_set_parameter(p, "compatibility", pdfversion); if (PDF_open_file(p, pdffilename) == -1) { fprintf(stderr, "Error: %s.\n", PDF_get_errmsg(p)); exit(1); } PDF_set_info(p, "Creator", "pdfimpose by PDFlib GmbH"); PDF_set_parameter(p, "openaction", "fitpage"); if (!quiet) PDF_set_parameter(p, "pdiwarning", "true"); /* report PDI problems */ /* multi-page imposition: calculate scaling factor and cell dimensions */ if (rows != 1 || cols != 1) { if (landscape) { height = sheetheight; sheetheight = sheetwidth; sheetwidth = height; } if (rows > cols) scale = 1.0f / rows; else scale = 1.0f / cols; rowheight = sheetheight * scale; colwidth = sheetwidth * scale; } /* process all PDF documents */ while (optind++ < argc) { filename = argv[optind-1]; if (!quiet) fprintf(stderr, "Imposing '%s'...\n", filename); if ((doc = PDF_open_pdi(p, filename, "", 0)) == -1) { if (quiet) fprintf(stderr, "Error: %s.\n", PDF_get_errmsg(p)); continue; } /* query number of pages in the document */ docpages = (int) PDF_get_pdi_value(p, "/Root/Pages/Count", doc, -1, 0); /* single cell only: concatenate, using original page dimensions */ if (rows == 1 && cols == 1) { /* open all pages and add to the output file */ for (pageno = 1; pageno <= docpages ; pageno++) { page = PDF_open_pdi_page(p, doc, pageno, ""); if (page == -1) { /* we'll get an exception in verbose mode anyway */ if (quiet) fprintf(stderr, "Couldn't open page %d of PDF file '%s' (%s)\n", pageno, filename, PDF_get_errmsg(p)); break; } sheetwidth = PDF_get_pdi_value(p, "width", doc, page, 0); sheetheight = PDF_get_pdi_value(p, "height", doc, page, 0); PDF_begin_page(p, sheetwidth, sheetheight); /* define bookmark with filename */ if (pageno == 1) PDF_add_bookmark(p, argv[optind-1], 0, 0); PDF_place_pdi_page(p, page, 0.0f, 0.0f, 1.0f, 1.0f); PDF_close_pdi_page(p, page); PDF_end_page(p); } } else { /* impose multiple pages */ if (newpage) r = c = 0; /* open all pages and add to the output file */ for (pageno = 1; pageno <= docpages ; pageno++) { page = PDF_open_pdi_page(p, doc, pageno, ""); if (page == -1) { /* we'll get an exception in verbose mode anyway */ if (quiet) fprintf(stderr, "Couldn't open page %d of PDF file '%s' (%s)\n", pageno, filename, PDF_get_errmsg(p)); break; } /* start a new page */ if (r == 0 && c == 0) PDF_begin_page(p, sheetwidth, sheetheight); /* define bookmark with filename */ if (pageno == 1) PDF_add_bookmark(p, argv[optind-1], 0, 0); width = PDF_get_pdi_value(p, "width", doc, page, 0); height = PDF_get_pdi_value(p, "height", doc, page, 0); /* * The save/restore pair is required to get the clipping right, * and helps PostScript printing manage its memory efficiently. */ PDF_save(p); PDF_rect(p, c * colwidth, sheetheight - (r + 1) * rowheight, colwidth, rowheight); PDF_clip(p); PDF_setcolor(p, "stroke", "gray", 0.0f, 0.0f, 0.0f, 0.0f); /* TODO: adjust scaling factor if page doesn't fit into cell */ PDF_place_pdi_page(p, page, c * colwidth, sheetheight - (r + 1) * rowheight, scale, scale); PDF_close_pdi_page(p, page); /* only half of the linewidth will be drawn due to clip() */ if (boxes) { PDF_setlinewidth(p, 1.0f * scale); PDF_rect(p, c * colwidth, sheetheight - (r + 1) * rowheight, colwidth, rowheight); PDF_stroke(p); } PDF_restore(p); c++; if (c == cols) { c = 0; r++; } if (r == rows) { r = 0; PDF_end_page(p); } } } PDF_close_pdi(p, doc); } /* finish last page if multi-page imposition */ if ((rows != 1 || cols != 1) && (r != 0 || c != 0)) PDF_end_page(p); PDF_close(p); PDF_delete(p); exit(0); }