void pdf_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit) { internal_t*i = (internal_t*)dev->internal; if(width<1e-6) return; reset_matrix(i); PDF_setlinewidth(i->p, width); PDF_setlinecap(i->p, cap_style==gfx_capButt?0:(cap_style==gfx_capRound?1:2)); PDF_setlinejoin(i->p, joint_style==gfx_joinMiter?0:(joint_style==gfx_joinRound?1:2)); PDF_setrgbcolor_stroke(i->p, color->r/255.0, color->g/255.0, color->b/255.0); if(joint_style==gfx_joinMiter) PDF_setmiterlimit(i->p, miterLimit); if(mkline(line, i->p, i->config_xpad, i->config_ypad, 1.0, 0)) PDF_stroke(i->p); }
int main(void) { /* This is where the data files are. Adjust as necessary.*/ const char * searchpath = "../data"; const char *targetname = "x5target.pdf"; PDF *p; char optlist[1024]; int font, proxy; double linewidth=2; double width, height; /* 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) { /* This means we must check return values of load_font() etc. */ PDF_set_parameter(p, "errorpolicy", "return"); PDF_set_parameter(p, "SearchPath", searchpath); if (PDF_begin_document(p, "starter_pdfx5g.pdf", 0, "pdfx=PDF/X-5g") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_set_info(p, "Creator", "PDFlib starter sample"); PDF_set_info(p, "Title", "starter_pdfx5g"); /* Open the output intent profile */ if (PDF_load_iccprofile(p, "ISOcoated.icc", 0, "usage=outputintent") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); printf("Please install the ICC profile package from " "www.pdflib.com to run the PDF/X-5g starter sample.\n"); PDF_delete(p); return(2); } /* Font embedding is required for PDF/X */ font = PDF_load_font(p, "LuciduxSans-Oblique", 0, "winansi", "embedding"); if (font == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } /* Create a template which will serve as proxy. The referenced * page (the target) is attached to the proxy. * The template width and height will be determined automatically, * so we don't have to supply them. */ sprintf(optlist, "reference={filename=%s pagenumber=1}", targetname); proxy = PDF_begin_template_ext(p, 0, 0, optlist); if (proxy == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } width = PDF_info_image(p, proxy, "imagewidth", ""); height = PDF_info_image(p, proxy, "imageheight", ""); /* Draw a crossed-out rectangle to visualize the proxy. * Attention: if we use the exact corner points, one half of the * linewidth would end up outside the template, and therefore be * clipped. */ PDF_setlinewidth(p, linewidth); PDF_moveto(p, linewidth/2, linewidth/2); PDF_lineto(p, width-linewidth/2, linewidth/2); PDF_lineto(p, width-linewidth/2, height-linewidth/2); PDF_lineto(p, linewidth/2, height-linewidth/2); PDF_lineto(p, linewidth/2, linewidth/2); PDF_lineto(p, width-linewidth/2, height-linewidth/2); PDF_moveto(p, width-linewidth/2, linewidth/2); PDF_lineto(p, linewidth/2, height-linewidth/2); PDF_stroke(p); PDF_setfont(p, font, 24); sprintf(optlist, "fitmethod=auto position=center boxsize={%f %f}", width, height); PDF_fit_textline(p, "Proxy replaces target here", 0, 0, 0, optlist); PDF_end_template_ext(p, 0, 0); /* Create the page */ PDF_begin_page_ext(p, 595, 842, ""); PDF_setfont(p, font, 18); PDF_fit_textline(p, "PDF/X-5 starter sample with reference to an external page", 0, 50, 700, ""); /* Place the proxy on the page */ PDF_fit_image(p, proxy, 50, 50, "boxsize={500 500} fitmethod=meet"); PDF_end_page_ext(p, ""); PDF_end_document(p, ""); } PDF_CATCH(p) { printf("PDFlib exception occurred:\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(void) { /* This is where the data files are. Adjust as necessary. */ const char* searchpath = "../data"; PDF * p; const char* imagefile = "nesrin.jpg"; char* optlist; int font, image; /* 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) { /* This means we must check return values of load_font() etc. */ PDF_set_parameter(p, "errorpolicy", "return"); PDF_set_parameter(p, "SearchPath", searchpath); if (PDF_begin_document(p, "starter_basic.pdf", 0, "") == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_set_info(p, "Creator", "PDFlib starter sample"); PDF_set_info(p, "Title", "starter_basic"); /* We load the image before the first page, and use it * on all pages */ image = PDF_load_image(p, "auto", imagefile, 0, ""); if (image == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } /* Page 1 */ PDF_begin_page_ext(p, 595, 842, ""); font = PDF_load_font(p, "Helvetica-Bold", 0, "winansi", ""); if (font == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } PDF_setfont(p, font, 24); PDF_set_text_pos(p, 50, 700); PDF_show(p, "Hello world!"); PDF_fit_image(p, image, (float) 0.0, (float) 0.0, "scale=0.25"); PDF_end_page_ext(p, ""); /* Page 2 */ PDF_begin_page_ext(p, 595, 842, ""); /* red rectangle */ PDF_setcolor(p, "fill", "rgb", 1.0, 0.0, 0.0, 0.0); PDF_rect(p, 200, 200, 250, 150); PDF_fill(p); /* blue circle */ PDF_setcolor(p, "fill", "rgb", 0.0, 0.0, 1.0, 0.0); PDF_arc(p, 400, 600, 100, 0, 360); PDF_fill(p); /* thick gray line */ PDF_setcolor(p, "stroke", "gray", 0.5, 0.0, 0.0, 0.0); PDF_setlinewidth(p, 10); PDF_moveto(p, 100, 500); PDF_lineto(p, 300, 700); PDF_stroke(p); /* Using the same image handle means the data will be copied * to the PDF only once, which saves space. */ PDF_fit_image(p, image, 150.0, 25.0, "scale=0.25"); PDF_end_page_ext(p, ""); /* Page 3 */ PDF_begin_page_ext(p, 595, 842, ""); /* Fit the image to a box of predefined size (without distortion) */ optlist = "boxsize={400 400} position={center} fitmethod=meet"; PDF_fit_image(p, image, 100, 200, optlist); PDF_end_page_ext(p, ""); PDF_close_image(p, image); PDF_end_document(p, ""); } PDF_CATCH(p) { printf("PDFlib exception occurred:\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(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); }