示例#1
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;
}
示例#2
0
int
main(void)
{

    /* This is where the data files are. Adjust as necessary. */
    const char * searchpath = "../data";

    PDF * p;
    double width, height;
    const char * infile = "boilerplate.pdf";
    int i, page, indoc, blockcount;
    char * optlist;

    /* 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_block.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_block");

        /* Open a PDF containing 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 */
        page = PDF_open_pdi_page(p, indoc, 1, "");
        if (page == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        width = PDF_pcos_get_number(p, indoc, "pages[0]/width");
        height = PDF_pcos_get_number(p, indoc, "pages[0]/height");

        PDF_begin_page_ext(p, width, height, "");

        /* Place the imported page on the output page */
        PDF_fit_pdi_page(p, page, 0, 0, "");

	/* Query the number of blocks on the first page */
        blockcount =
        (int) PDF_pcos_get_number(p, indoc, "length:pages[0]/blocks");

        if (blockcount == 0) {
            printf("Error: %s does not contain any PDFlib blocks", infile);
            PDF_delete(p);
            return(2);
        }

        /* Loop over all blocks on the page */
        for (i = 0; i <  blockcount; i++)
        {
            char buf[1024];
            const char * blockname;
            const char * blocktype;

            /* Fetch the name and type of the i-th block on the first page
             * (one of Text/Image/PDF)
             */
            sprintf(buf, "pages[0]/blocks[%d]/Name",i);
            blockname = PDF_pcos_get_string(p, indoc, buf);

            sprintf(buf, "pages[0]/blocks[%d]/Subtype",
                    i);
            blocktype = PDF_pcos_get_string(p, indoc, buf);

            /* Visualize all text blocks */
            if (!strcmp(blocktype, "Text"))
            {
                optlist =
                    "fontname=Helvetica encoding=winansi "
                    "fillcolor={rgb 1 0 0} "
                    "bordercolor={gray 0} linewidth=0.25";

                /* We simply use the blockname as content */
                if (PDF_fill_textblock(p, page, blockname,
                                    blockname, 0, optlist) == -1) {
                    printf("Warning: %s\n", PDF_get_errmsg(p));
                }
            }
        }

        PDF_end_page_ext(p, "");
        PDF_close_pdi_page(p, page);

        PDF_end_document(p, "");
        PDF_close_pdi_document(p, indoc);
    }

    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;
}
示例#3
0
int
main(void)
{
    PDF 	*p;
    int 	i, blockcontainer, page;
    char 	*infile = "boilerplate.pdf";

    /*
     * This is where font/image/PDF input files live. Adjust as necessary.
     *
     * Note that this directory must also contain the LuciduxSans font outline
     * and metrics files.
     */
    char *searchpath = "../data";

    typedef struct { char *name; char *value; } blockdata;

    blockdata data[] = {
    { "name",			"Victor Kraxi" },
    { "business.title",		"Chief Paper Officer" },
    { "business.address.line1",	"17, Aviation Road" },
    { "business.address.city",	"Paperfield" },
    { "business.telephone.voice","phone +1 234 567-89" },
    { "business.telephone.fax",	"fax +1 234 567-98" },
    { "business.email",		"*****@*****.**" },
    { "business.homepage",	"www.kraxi.com" },
    };

#define BLOCKCOUNT (sizeof(data)/sizeof(data[0]))

    /* 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");

	/* Set the search path for fonts and PDF files */
	PDF_set_parameter(p, "SearchPath", searchpath);

	/* This line is required to avoid problems on Japanese systems */
	PDF_set_parameter(p, "hypertextencoding", "host");

	if (PDF_begin_document(p, "businesscard.pdf", 0, "") == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
	}

	PDF_set_info(p, "Creator", "businesscard.c");
	PDF_set_info(p, "Author", "Thomas Merz");
	PDF_set_info(p, "Title","PDFlib block processing sample (C)");

	blockcontainer = PDF_open_pdi_document(p, infile, 0, "");
	if (blockcontainer == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
        }

	page = PDF_open_pdi_page(p, blockcontainer, 1, "");
	if (page == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
	}

	PDF_begin_page_ext(p, 20, 20, "");		/* dummy page size */

	/* This will adjust the page size to the block container's size. */
	PDF_fit_pdi_page(p, page, 0, 0, "adjustpage");

	/* Fill all text blocks with dynamic data */
	for (i = 0; i < (int) BLOCKCOUNT; i++) {
	    if (PDF_fill_textblock(p, page, data[i].name, data[i].value, 0,
		    "embedding encoding=host") == -1) {
		printf("Warning: %s\n", PDF_get_errmsg(p));
	    }
	}

	PDF_end_page_ext(p, "");
	PDF_close_pdi_page(p, page);

	PDF_end_document(p, "");
	PDF_close_pdi_document(p, blockcontainer);
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred in businesscard 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;
}
示例#4
0
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;
}
示例#5
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);
}