/* ** cg_cut_lines() ** Write the cut lines in a page. */ yerr_t cg_cut_lines(cg_t *carta, cg_deck_t *deck, cg_back_t back) { int i; double color_luminance = 0; /* hidden-ditch */ PDF_save(carta->p); if ((back == BACK_NO && deck->ditch_odd) || (back != BACK_NO && deck->ditch_even)) { unsigned int hex1, hex2, hex3; double col1, col2, col3; char *used_color = (back == BACK_NO) ? deck->ditch_odd : deck->ditch_even; sscanf(used_color, "#%02x%02x%02x", &hex1, &hex2, &hex3); col1 = (double)hex1 / 255; col2 = (double)hex2 / 255; col3 = (double)hex3 / 255; color_luminance = (col1 * LUMINANCE_RED) + (col2 * LUMINANCE_GREEN) + (col3 * LUMINANCE_BLUE); PDF_setcolor(carta->p, "fill", "rgb", col1, col2, col3, 0.0); PDF_rect(carta->p, YVAL2PT(deck->paper_margin_w) / 2.0, YVAL2PT(deck->paper_margin_h) / 2.0, YVAL2PT(deck->paper_width) - YVAL2PT(deck->paper_margin_w), YVAL2PT(deck->paper_height) - YVAL2PT(deck->paper_margin_h)); PDF_fill(carta->p); } PDF_restore(carta->p); /* external cut lines */ PDF_save(carta->p); PDF_setcolor(carta->p, "fillstroke", "rgb", 0.0, 0.0, 0.0, 0.0); for (i = 0; i <= deck->cols; ++i) { double x, y; if (i == deck->cols && YVAL2PT(deck->space_width)) break ; x = YVAL2PT(deck->paper_margin_w) + (i * YVAL2PT(deck->card_width)) + (i * YVAL2PT(deck->space_width)); if (back == BACK_HEIGHT || back == BACK_WIDTH) x = YVAL2PT(deck->paper_width) - YVAL2PT(deck->paper_margin_w) - (i * YVAL2PT(deck->card_width)) - (i * YVAL2PT(deck->space_width)); y = 0.0; PDF_moveto(carta->p, x, y); y = YVAL2PT(deck->paper_margin_h) / 2.0; PDF_lineto(carta->p, x, y); y = YVAL2PT(deck->paper_height); PDF_moveto(carta->p, x, y); y = YVAL2PT(deck->paper_height) - (YVAL2PT(deck->paper_margin_h) / 2.0); PDF_lineto(carta->p, x, y); /* second cut line - space between cards */ if (i < deck->cols && YVAL2PT(deck->space_width)) { if (back == BACK_HEIGHT || back == BACK_WIDTH) x -= YVAL2PT(deck->card_width); else x += YVAL2PT(deck->card_width); y = 0.0; PDF_moveto(carta->p, x, y); y = YVAL2PT(deck->paper_margin_h) / 2.0; PDF_lineto(carta->p, x, y); y = YVAL2PT(deck->paper_height); PDF_moveto(carta->p, x, y); y = YVAL2PT(deck->paper_height) - (YVAL2PT(deck->paper_margin_h) / 2.0); PDF_lineto(carta->p, x, y); } } for (i = 0; i <= deck->rows; ++i) { double x, y; if (i == deck->rows && YVAL2PT(deck->space_height)) break ; x = 0.0; y = YVAL2PT(deck->paper_margin_h) + (i * YVAL2PT(deck->card_height)) + (i * YVAL2PT(deck->space_height)); PDF_moveto(carta->p, x, y); x = YVAL2PT(deck->paper_margin_w) / 2.0; PDF_lineto(carta->p, x, y); x = YVAL2PT(deck->paper_width); PDF_moveto(carta->p, x, y); x = YVAL2PT(deck->paper_width) - (YVAL2PT(deck->paper_margin_w) / 2.0); PDF_lineto(carta->p, x, y); /* second cut line - space between cards */ if (i < deck->rows && YVAL2PT(deck->space_height)) { x = 0.0; y += YVAL2PT(deck->card_height); PDF_moveto(carta->p, x, y); x = YVAL2PT(deck->paper_margin_w) / 2.0; PDF_lineto(carta->p, x, y); x = YVAL2PT(deck->paper_width); PDF_moveto(carta->p, x, y); x = YVAL2PT(deck->paper_width) - (YVAL2PT(deck->paper_margin_w) / 2.0); PDF_lineto(carta->p, x, y); } } PDF_stroke(carta->p); PDF_restore(carta->p); /* internal cut lines */ PDF_save(carta->p); if (color_luminance >= LUMINANCE_THRESHOLD) PDF_setcolor(carta->p, "fillstroke", "rgb", 0.0, 0.0, 0.0, 0.0); else PDF_setcolor(carta->p, "fillstroke", "rgb", 1.0, 1.0, 1.0, 0.0); for (i = 0; i <= deck->cols; ++i) { double x, y; if (i == deck->cols && YVAL2PT(deck->space_width)) break ; x = YVAL2PT(deck->paper_margin_w) + (i * YVAL2PT(deck->card_width)) + (i * YVAL2PT(deck->space_width)); if (back == BACK_HEIGHT || back == BACK_WIDTH) x = YVAL2PT(deck->paper_width) - YVAL2PT(deck->paper_margin_w) - (i * YVAL2PT(deck->card_width)) - (i * YVAL2PT(deck->space_width)); y = YVAL2PT(deck->paper_margin_h) / 2.0; PDF_moveto(carta->p, x, y); y = YVAL2PT(deck->paper_margin_h) * 2.0 / 3.0; PDF_lineto(carta->p, x, y); y = YVAL2PT(deck->paper_height) - (YVAL2PT(deck->paper_margin_h) / 2.0); PDF_moveto(carta->p, x, y); y = YVAL2PT(deck->paper_height) - (YVAL2PT(deck->paper_margin_h) * 2.0 / 3.0); PDF_lineto(carta->p, x, y); /* second cut line - space between cards */ if (i < deck->cols && YVAL2PT(deck->space_width)) { if (back == BACK_HEIGHT || back == BACK_WIDTH) x -= YVAL2PT(deck->card_width); else x += YVAL2PT(deck->card_width); y = YVAL2PT(deck->paper_margin_h) / 2.0; PDF_moveto(carta->p, x, y); y = YVAL2PT(deck->paper_margin_h) * 2.0 / 3.0; PDF_lineto(carta->p, x, y); y = YVAL2PT(deck->paper_height) - (YVAL2PT(deck->paper_margin_h) / 2.0); PDF_moveto(carta->p, x, y); y = YVAL2PT(deck->paper_height) - (YVAL2PT(deck->paper_margin_h) * 2.0 / 3.0); PDF_lineto(carta->p, x, y); } } for (i = 0; i <= deck->rows; ++i) { double x, y; if (i == deck->rows && YVAL2PT(deck->space_height)) break ; x = YVAL2PT(deck->paper_margin_w) / 2.0; y = YVAL2PT(deck->paper_margin_h) + (i * YVAL2PT(deck->card_height)) + (i * YVAL2PT(deck->space_height)); PDF_moveto(carta->p, x, y); x = YVAL2PT(deck->paper_margin_w) * 2.0 / 3.0; PDF_lineto(carta->p, x, y); x = YVAL2PT(deck->paper_width) - (YVAL2PT(deck->paper_margin_w) / 2.0); PDF_moveto(carta->p, x, y); x = YVAL2PT(deck->paper_width) - (YVAL2PT(deck->paper_margin_w) * 2.0 / 3.0); PDF_lineto(carta->p, x, y); /* second cut line - space between cards */ if (i < deck->rows && YVAL2PT(deck->space_height)) { x = YVAL2PT(deck->paper_margin_w) / 2.0; y += YVAL2PT(deck->card_height); PDF_moveto(carta->p, x, y); x = YVAL2PT(deck->paper_margin_w) * 2.0 / 3.0; PDF_lineto(carta->p, x, y); x = YVAL2PT(deck->paper_width) - (YVAL2PT(deck->paper_margin_w) / 2.0); PDF_moveto(carta->p, x, y); x = YVAL2PT(deck->paper_width) - (YVAL2PT(deck->paper_margin_w) * 2.0 / 3.0); PDF_lineto(carta->p, x, y); } } PDF_stroke(carta->p); PDF_restore(carta->p); return (YENOERR); }
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(int argc, char *argv[]) { char *pdffilename = NULL; char *imagetype = "auto"; char *useroptions = ""; PDF *p; int image; #ifndef PDFLIB_LITE int tagged, opt; int item=0; char *pdfalevel = NULL, *intent = "sRGB"; #endif /* PDFLIB_LITE */ int resolution = 0; int page_numbering = 0; double graylevel = -1.0; int frame; int current_page = 1; #define BUFLEN 1024 char optlist[BUFLEN]; char buf[BUFLEN]; #ifndef PDFLIB_LITE while ((opt = getopt(argc, argv, "a:g:I:i:o:p:r:t:")) != -1) #else while ((opt = getopt(argc, argv, "g:i:o:p:r:t:")) != -1) #endif /* PDFLIB_LITE */ switch (opt) { #ifndef PDFLIB_LITE case 'a': if (strcmp(optarg, "none")) pdfalevel = optarg; break; #endif /* PDFLIB_LITE */ case 'g': if (optarg) { graylevel = atof(optarg); if (graylevel < 0.0 || graylevel > 1.0) { fprintf(stderr, "Error: Bad gray level %.2g for -g.\n", graylevel); usage(); } } break; #ifndef PDFLIB_LITE case 'I': if (!strcmp(optarg, "none")) intent = NULL; else intent = optarg; break; #endif /* PDFLIB_LITE */ case 'i': useroptions = optarg; if (strlen(useroptions) > BUFLEN-20) { fprintf(stderr, "Error: image option list too long.\n"); usage(); } break; 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(); } break; case 't': imagetype = optarg; break; 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(); } if ((p = PDF_new()) == (PDF *) 0) { fprintf(stderr, "Couldn't create PDFlib object (out of memory)!\n"); exit(99); } PDF_TRY(p) { optlist[0] = 0; #ifndef PDFLIB_LITE tagged = pdfalevel && !strcmp(pdfalevel, "PDF/A-1a:2005"); if (pdfalevel) { /* disabled (see bug #1577) if (tagged) sprintf(optlist, "pdfa=%s lang=en", pdfalevel); else */ sprintf(optlist, "pdfa=%s", pdfalevel); } #endif /* PDFLIB_LITE */ if (PDF_begin_document(p, pdffilename, 0, optlist) == -1) { fprintf(stderr, "Error: cannot open output file %s.\n", pdffilename); exit(1); } #ifndef PDFLIB_LITE if (pdfalevel && intent) { if (strcmp("sRGB", intent)) { sprintf(optlist, "Intent=%s", intent); PDF_set_parameter(p, "ICCProfile", optlist); } fprintf(stderr, "Using '%s' as PDF/A output intent.\n", intent); PDF_load_iccprofile(p, intent, 0, "usage=outputintent"); } if (tagged) item = PDF_begin_item(p, "Document", "Alt={Images converted with pdfimage by PDFlib GmbH}"); #endif /* PDFLIB_LITE */ PDF_set_info(p, "Creator", "pdfimage"); PDF_set_parameter(p, "warning", "false"); while (optind++ < argc) { #ifndef PDFLIB_LITE int item2=0; #endif /* PDFLIB_LITE */ int parent=0; fprintf(stderr, "Processing image file '%s'...", argv[optind-1]); /* process all frames in a multi-page image file */ for (frame=1; /* */; frame++) { sprintf(optlist, "%s page %d", useroptions, frame); image = PDF_load_image(p, imagetype, argv[optind-1],0, optlist); if (image == -1) { if (frame == 1) fprintf(stderr, "\n%s (skipped).\n", PDF_get_errmsg(p)); break; } /* dummy page size, will be adjusted later */ PDF_begin_page_ext(p, 20, 20, ""); if (graylevel >= 0.0) { if (pdfalevel) { PDF_setcolor(p, "fill", "lab", graylevel, 0.0, 0.0, 0.0); } else { PDF_setcolor(p, "fill", "gray", graylevel, 0.0, 0.0, 0.0); } PDF_rect(p, 0, 0, 10000, 10000); PDF_fill(p); } /* define outline with filename or page number */ if (page_numbering) { sprintf(buf, "Page %d", current_page++); PDF_create_bookmark(p, buf, 0, ""); } else { if (frame == 1) { parent = PDF_create_bookmark(p, argv[optind-1], 0, "open"); } else { sprintf(buf, "page %d", frame); sprintf(optlist, "parent=%d", parent); PDF_create_bookmark(p, buf, 0, optlist); } } #ifndef PDFLIB_LITE if (tagged) { /* The file name is the best /Alt value we can provide */ sprintf(optlist, "Alt={%s}", argv[optind-1]); item2 = PDF_begin_item(p, "Figure", optlist); } #endif /* PDFLIB_LITE */ if (resolution != 0) sprintf(optlist, "dpi %d adjustpage", resolution); else sprintf(optlist, "adjustpage"); PDF_fit_image(p, image, 0.0, 0.0, optlist); #ifndef PDFLIB_LITE if (tagged) PDF_end_item(p, item2); #endif /* PDFLIB_LITE */ PDF_end_page_ext(p, ""); } if (frame > 2) fprintf(stderr, "(%d frames)", frame-1); fprintf(stderr, "\n"); } #ifndef PDFLIB_LITE if (tagged) PDF_end_item(p, item); #endif /* PDFLIB_LITE */ PDF_end_document(p, ""); } PDF_CATCH(p) { printf("\npdfimage: error while creating PDF output (%s(): %s)\n", PDF_get_apiname(p), PDF_get_errmsg(p)); PDF_delete(p); exit(99); } PDF_delete(p); exit(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); }