/* ** cg_unload_image() ** Unload one previously loaded image. */ yerr_t cg_unload_image(cg_t *carta, cg_image_t *image) { if (!image) return (YENOERR); if (image->i != -1) PDF_close_image(carta->p, image->i); if (image->mask && image->m != -1) PDF_close_image(carta->p, image->m); image->i = image->m = -1; return (YENOERR); }
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; }
void pdf_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform) { internal_t*i = (internal_t*)dev->internal; int t,size=img->width*img->height; int has_alpha=0; for(t=0;t<size;t++) { if(img->data[t].a!=255) { has_alpha=1; break; } } double w = sqrt(matrix->m00*matrix->m00+matrix->m01*matrix->m01); double h = sqrt(matrix->m10*matrix->m10+matrix->m11*matrix->m11); double l1 = w*img->width; double l2 = h*img->height; double dpi_x = 72.0 / w; double dpi_y = 72.0 / h; double dpi = dpi_x>dpi_y?dpi_x:dpi_y; gfximage_t*rescaled_image = 0; if(i->config_maxdpi && dpi > i->config_maxdpi) { int newwidth = img->width*i->config_maxdpi/dpi; int newheight = img->height*i->config_maxdpi/dpi; rescaled_image = gfximage_rescale(img, newwidth, newheight); msg("<notice> Downscaling %dx%d image (dpi %f, %.0fx%.0f on page) to %dx%d (dpi %d)", img->width, img->height, dpi, l1, l2, newwidth, newheight, i->config_maxdpi); img = rescaled_image; } if(i->config_mindpi && dpi < i->config_mindpi && img->width>1 && img->height>1) { msg("<error> Found image of size %dx%d with dpi %f, minimum allowed dpi is %d", img->width, img->height, dpi, i->config_mindpi); exit(1); } char tempfile[128]; mktempname(tempfile, "jpg"); gfximage_save_jpeg(img, tempfile, 96); int imgid=-1; if(has_alpha) { char tempfile2[128]; mktempname(tempfile2, "jpg"); int t; int size = img->width*img->height; unsigned char*alpha = malloc(size); for(t=0;t<size;t++) { alpha[t] = img->data[t].a; } jpeg_save_gray(alpha, img->width, img->height, 97, tempfile2); free(alpha); int maskid = PDF_load_image(i->p, "jpeg", tempfile2, 0, "mask"); unlink(tempfile2); char masked[80]; if(maskid<0) { msg("<error> Couldn't process mask jpeg of size %dx%d: error code %d", img->width, img->height, maskid); return; } sprintf(masked, "masked %d", maskid); imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, masked); } else { imgid = PDF_load_image(i->p, "jpeg", tempfile, 0, ""); } if(imgid<0) { msg("<error> Couldn't process jpeg of size %dx%d: error code %d, file %s", img->width, img->height, imgid, tempfile); return; } unlink(tempfile); char options[80]; set_matrix(i, matrix->m00, matrix->m01, matrix->m10, matrix->m11); /* an image's (0,0) is at the lower left corner */ double x=matrix->tx + i->config_xpad + matrix->m10*img->height; double y=matrix->ty + i->config_ypad + matrix->m11*img->height; double tx,ty; transform_back(i, x, y, &tx, &ty); PDF_place_image(i->p, imgid, tx, ty, 1.0); PDF_close_image(i->p, imgid); if(rescaled_image) gfximage_free(rescaled_image); }
int main(void) { /* This is where the data files are. Adjust as necessary. */ const char *searchpath = "../data"; const char *outfile = "starter_block.pdf"; const char *infile = "block_template.pdf"; const char *imagefile = "new.jpg"; PDF *p; int i, j, inpage, indoc, image; char optlist[1024], buf[1024]; /* Names of the person-related blocks contained on the imported page */ const char *addressblocks[] = { "name", "street", "city" }; /* number of address blocks */ const int nblocks = sizeof(addressblocks) / sizeof(addressblocks[0]); /* Data related to various persons used for personalization */ const char *persons[][3] = { {"Mr Maurizio Moroni", "Strada Provinciale 124", "Reggio Emilia"}, {"Ms Dominique Perrier", "25, rue Lauriston", "Paris"}, {"Mr Liu Wong", "55 Grizzly Peak Rd.", "Butte"} }; /* number of persons */ const int npersons = sizeof(persons) / sizeof(persons[0]); /* Static text simulates database-driven variable contents */ const char *intro = "Dear"; const char *goodbye = "Yours sincerely,\nVictor Kraxi"; const char *announcement = "Our <fillcolor=red>BEST PRICE OFFER<fillcolor=black> includes today:" "\n\n" "Long Distance Glider\nWith this paper rocket you can send all your " "messages even when sitting in a hall or in the cinema pretty near " "the back.\n\n" "Giant Wing\nAn unbelievable sailplane! It is amazingly robust and " "can even do aerobatics. But it is best suited to gliding.\n\n" "Cone Head Rocket\nThis paper arrow can be thrown with big swing. " "We launched it from the roof of a hotel. It stayed in the air a " "long time and covered a considerable distance.\n\n" "Super Dart\nThe super dart can fly giant loops with a radius of 4 " "or 5 meters and cover very long distances. Its heavy cone point is " "slightly bowed upwards to get the lift required for loops.\n\n" "Visit us on our Web site at www.kraxi.com!"; /* 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) { PDF_set_parameter(p, "SearchPath", searchpath); /* This means we must check return values of load_font() etc. */ PDF_set_parameter(p, "errorpolicy", "return"); if (PDF_begin_document(p, outfile, 0, "destination={type=fitwindow} pagelayout=singlepage") == -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_block"); /* Open the Block template which contains PDFlib Blocks */ indoc = PDF_open_pdi_document(p, infile, 0, ""); if (indoc == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } /* Open the first page and clone the page size */ inpage = PDF_open_pdi_page(p, indoc, 1, "cloneboxes"); if (inpage == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); PDF_delete(p); return(2); } image = PDF_load_image(p, "auto", imagefile, 0, ""); if (image == -1) { printf("Error: %s\n", PDF_get_errmsg(p)); return(3); } /* Based on the imported page generate several pages with the blocks * being filled with data related to different persons */ for (i = 0; i < npersons; i++) { /* Start the output page with a dummy size */ PDF_begin_page_ext(p, 10, 10, ""); /* Place the imported page on the output page, and clone all * page boxes which are present in the input page; this will * override the dummy size used in begin_page_ext(). */ PDF_fit_pdi_page(p, inpage, 0, 0, "cloneboxes"); /* Option list for text blocks */ strcpy(optlist, "encoding=winansi embedding"); /* Loop over all person-related blocks. Fill the j-th block with the * corresponding entry of the persons array. */ for (j = 0; j < nblocks; j++) { if (PDF_fill_textblock(p, inpage, addressblocks[j], persons[i][j], 0, optlist) == -1) printf("Warning: %s\n", PDF_get_errmsg(p)); } /* Fill the "intro" block */ sprintf(buf, "%s %s,", intro, persons[i][0]); if (PDF_fill_textblock(p, inpage, "intro", buf, 0, optlist) == -1) printf("Warning: %s\n", PDF_get_errmsg(p)); /* Fill the "announcement" block */ if (PDF_fill_textblock(p, inpage, "announcement", announcement, 0, optlist) == -1) printf("Warning: %s\n", PDF_get_errmsg(p)); /* Fill the "goodbye" block */ if (PDF_fill_textblock(p, inpage, "goodbye", goodbye, 0, optlist) == -1) printf("Warning: %s\n", PDF_get_errmsg(p)); /* Fill the image block */ strcpy(optlist, ""); /* Option list */ if (PDF_fill_imageblock(p, inpage, "icon", image, optlist) == -1) printf("Warning: %s\n", PDF_get_errmsg(p)); PDF_end_page_ext(p, ""); } PDF_close_pdi_page(p, inpage); PDF_close_pdi_document(p, indoc); 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) { /* 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; }