Example #1
0
int
main(void)
{
    try {
	PDFlib *p;
	int image;
	char *imagefile = (char *) "nesrin.jpg";
	// This is where font/image/PDF input files live. Adjust as necessary. 
	char *searchpath = (char *) "../data";

	p = new PDFlib();

	//  This means we must check return values of load_font() etc.
	p->set_parameter("errorpolicy", "return");

	p->set_parameter("SearchPath", searchpath);

	// This line is required to avoid problems on Japanese systems
	p->set_parameter("hypertextencoding", "host");

	if (p->begin_document("image.pdf", "") == -1) {
	    cerr << "Error: " << p->get_errmsg() << endl; return 2;
	}

	p->set_info("Creator", "image.cpp");
	p->set_info("Author", "Thomas Merz");
	p->set_info("Title", "image sample (C++)!");

	image = p->load_image("auto", imagefile, "");

	if (image == -1) {
	    cerr << "Error: " << p->get_errmsg() << endl;
	    exit(3);
	}

	// dummy page size, will be adjusted by PDF_fit_image()
	p->begin_page_ext(10, 10, "");
	p->fit_image(image, 0.0, 0.0, "adjustpage");
	p->close_image(image);
	p->end_page_ext("");

	p->end_document("");
    }

    catch (PDFlib::Exception &ex) {
	cerr << "PDFlib exception occurred in hello sample: " << endl;
	cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
	    << ": " << ex.get_errmsg() << endl;
	return 2;
    }

    return 0;
}
Example #2
0
int
main(void)
{
    try {

        /* This is where the data files are. Adjust as necessary. */
        string searchpath = "../data";
        string outfile = "starter_layer.pdf";

        PDFlib p;
        string rgb = "nesrin.jpg";
        string gray = "nesrin_gray.jpg";

#define BUFLEN 1024
        char buf[BUFLEN];
        int font, imageRGB, imageGray, layerRGB, layerGray, layerEN, layerDE;

        /* This means we must check return values of load_font() etc. */
        p.set_parameter("errorpolicy", "return");

        p.set_parameter("SearchPath", searchpath);


        /* Open the document with the "Layers" navigation tab visible */
        if (p.begin_document(outfile, "openmode=layers") == -1) {
            cerr << "Error: " << p.get_errmsg() << endl; return 2;
        }

        p.set_info("Creator", "PDFlib starter sample");
        p.set_info("Title", "starter_layer");


        /* Load the font */
        font = p.load_font("Helvetica", "winansi", "");

        if (font == -1) {
            cerr << "Error: " << p.get_errmsg() << endl; return 2;
        }

        /* Load the Grayscale image */
        imageGray = p.load_image("auto", gray, "");
        if (imageGray == -1) {
            cerr << "Error: " << p.get_errmsg() << endl; return 2;
        }

        /* Load the RGB image */
        imageRGB = p.load_image("auto", rgb, "");
        if (imageRGB == -1) {
            cerr << "Error: " << p.get_errmsg() << endl; return 2;
        }

	/*
	 * Define all layers which will be used, and their relationships.
         * This should be done before the first page if the layers are
	 * used on more than one page.
	 */

        /* Define the layer "RGB" */
        layerRGB = p.define_layer("RGB", "");

        /* Define the layer "Grayscale" which is hidden when opening the
         * document or printing it.
         */
        layerGray = p.define_layer("Grayscale",
                    "initialviewstate=false initialprintstate=false");

        /* At most one of the "Grayscale" and "RGB" layers should be visible */
        sprintf(buf, "group={%d %d}", layerGray, layerRGB);
        p.set_layer_dependency("Radiobtn", buf);

        /* Define the layer "English" */
        layerEN = p.define_layer("English", "");

        /* Define the layer "German" which is hidden when opening the document
         * or printing it.
         */
        layerDE = p.define_layer("German",
                    "initialviewstate=false initialprintstate=false");

        /* At most one of the "English" and "German" layers should be visible */
        sprintf(buf, "group={%d %d}", layerEN, layerDE);
        p.set_layer_dependency("Radiobtn", buf);

        /* Start page */
        p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

	/* Place the RGB image on the layer "RGB" */
        p.begin_layer(layerRGB);
        p.fit_image(imageRGB, 100, 400,
                    "boxsize={400 300} fitmethod=meet");

	/* Place the Grayscale image on the layer "Grayscale" */
        p.begin_layer(layerGray);
        p.fit_image(imageGray, 100, 400,
                    "boxsize={400 300} fitmethod=meet");

	/* Place an English image caption on the layer "English" */
        p.begin_layer(layerEN);
        sprintf(buf, "font=%d fontsize=20", font);
        p.fit_textline("This is the Nesrin image.", 100, 370, buf);

	/* Place a German image caption on the layer "German".  */
        p.begin_layer(layerDE);
        sprintf(buf, "font=%d fontsize=20", font);
        p.fit_textline("Das ist das Nesrin-Bild.", 100, 370, buf);

        p.end_layer();

        p.end_page_ext("");

        p.end_document("");

    }

    catch (PDFlib::Exception &ex) {
        cerr << "PDFlib exception occurred:" << endl;
        cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
            << ": " << ex.get_errmsg() << endl;
        return 2;
    }

    return 0;
}
Example #3
0
int
main(void)
{
    try {
	/* This is where the data files are. Adjust as necessary.*/
	const wstring searchpath = L"../data";

	PDFlib p;
	const wstring imagefile = L"nesrin.jpg";
	int font, image, spot, icc;
	wostringstream optlist;

	//  This means we must check return values of load_font() etc.
	p.set_parameter(L"errorpolicy", L"return");

	p.set_parameter(L"SearchPath", searchpath);

	if (p.begin_document(L"starter_pdfx3.pdf", L"pdfx=PDF/X-3:2003")
                                                                    == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}

	p.set_info(L"Creator", L"PDFlib starter sample");
	p.set_info(L"Title", L"starter_pdfx3");

	/*
	 * You can use one of the Standard output intents (e.g. for SWOP
	 * printing) which do not require an ICC profile:

	p.load_iccprofile(L"CGATS TR 001", L"usage=outputintent");

	 * However, if you use ICC or Lab color you must load an ICC
	 * profile as output intent:
	 */
	if (p.load_iccprofile(L"ISOcoated.icc", L"usage=outputintent") == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
	    wcerr << L"Please install the ICC profile package from " <<
		L"www.pdflib.com to run the PDF/X starter sample." << endl;
	    return 2;
	}

	p.begin_page_ext(595, 842, L"");

	/* Font embedding is required for PDF/X */
	font = p.load_font(L"LuciduxSans-Oblique", L"winansi", L"embedding");
	if (font == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}
	p.setfont(font, 24);

	spot = p.makespotcolor(L"PANTONE 123 C");
	p.setcolor(L"fill", L"spot", spot, 1.0, 0.0, 0.0);
	p.fit_textline(L"PDF/X-3:2003 starter", 50, 700, L"");

	/* The RGB image below needs an ICC profile; we use sRGB. */
	icc = p.load_iccprofile(L"sRGB", L"");
        optlist.str(L"");
        optlist << L"iccprofile=" << icc;
	image = p.load_image(L"auto", imagefile, optlist.str());

	if (image == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}

	p.fit_image(image, 0.0, 0.0, L"scale=0.5");
	p.end_page_ext(L"");
	p.end_document(L"");
    }
    catch (PDFlib::Exception &ex) {
	wcerr << L"PDFlib exception occurred:" << endl
	      << L"[" << ex.get_errnum() << L"] " << ex.get_apiname()
	      << L": " << ex.get_errmsg() << endl;
	return 2;
    }

    return 0;
}
Example #4
0
int
main(void)
{
    try {
	/* This is where the data files are. Adjust as necessary. */
	const wstring searchpath = L"../data";

	PDFlib p;
	const wstring imagefile = L"nesrin.jpg";

	int font;
	int image;

	//  This means we must check return values of load_font() etc.
	p.set_parameter(L"errorpolicy", L"return");

	p.set_parameter(L"SearchPath", searchpath);

	if (p.begin_document(L"starter_pdfa1b.pdf",
                                        L"pdfa=PDF/A-1b:2005") == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}

	/*
	 * We use sRGB as output intent since it allows the color
	 * spaces CIELab, ICC-based, grayscale, and RGB.
	 *
	 * If you need CMYK color you must use a CMYK output profile.
	 */

	p.load_iccprofile(L"sRGB", L"usage=outputintent");

	p.set_info(L"Creator", L"PDFlib starter sample");
	p.set_info(L"Title", L"starter_pdfa1b");

	p.begin_page_ext(595, 842, L"");

	/* Font embedding is required for PDF/A */
	font = p.load_font(L"LuciduxSans-Oblique", L"winansi", L"embedding");
	if (font == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}
	p.setfont(font, 24);

	p.fit_textline(L"PDF/A-1b:2005 starter", 50, 700, L"");

	/* We can use an RGB image since we already supplied an
	 * output intent profile.
	 */
	image = p.load_image(L"auto", imagefile, L"");

	if (image == -1){
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}

	/* Place the image at the bottom of the page */
	p.fit_image(image, 0.0, 0.0, L"scale=0.5");

	p.end_page_ext(L"");
	p.close_image(image);

	p.end_document(L"");
    }
    catch (PDFlib::Exception &ex) {
	wcerr << L"PDFlib exception occurred:" << endl
	      << L"[" << ex.get_errnum() << L"] " << ex.get_apiname()
	      << L": " << ex.get_errmsg() << endl;
	return 2;
    }

    return 0;
}
Example #5
0
int
main(void)
{
    try {

        /* This is where the data files are. Adjust as necessary. */
        string searchpath = "../data";
        string outfile = "starter_image.pdf";

#define BUFLEN 1024
        char buf[BUFLEN];
        PDFlib p;
        string imagefile = "lionel.jpg";
        int font, image;
        int bw = 400, bh = 200;
        int x = 20, y = 580, yoffset = 260;

        p.set_parameter("SearchPath", searchpath);

        /* This means we must check return values of load_font() etc. */
        p.set_parameter("errorpolicy", "return");

        if (p.begin_document(outfile, "") == -1) {
            cerr << "Error: " << p.get_errmsg() << endl; return 2;
        }

        p.set_info("Creator", "PDFlib starter sample");
        p.set_info("Title", "starter_image");

        /* For PDFlib Lite: change "unicode" to "winansi" */
        font = p.load_font("Helvetica", "winansi", "");
        if (font == -1) {
            cerr << "Error: " << p.get_errmsg() << endl; return 2;
        }

        /* Load the image */
        image = p.load_image("auto", imagefile, "");
        if (image == -1) {
            cerr << "Error: " << p.get_errmsg() << endl; return 2;
        }

        /* Start page 1 */
        p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
        p.setfont(font, 12);


        /* ------------------------------------
         * Place the image in its original size
         * ------------------------------------
         */

        /* Position the image in its original size with its lower left corner
         * at the reference point (20, 380)
         */
        p.fit_image(image, 20, 380, "");

        /* Output some descriptive text */
        p.fit_textline(
            "The image is placed with the lower left corner in its original "
            "size at reference point (20, 380):", 20, 820, "");
        p.fit_textline("fit_image(image, 20, 380, \"\");", 20, 800, "");


        /* --------------------------------------------------------
         * Place the image with scaling and orientation to the west
         * --------------------------------------------------------
         */

        /* Position the image with its lower right corner at the reference
         * point (580, 20).
         * "scale=0.5" scales the image by 0.5.
         * "orientate=west" orientates the image to the west.
         */
        p.fit_image(image, 580, 20,
            "scale=0.5 position={right bottom} orientate=west");

        /* Output some descriptive text */
        p.fit_textline(
            "The image is placed with a scaling of 0.5 and an orientation to "
            "the west with the lower right corner", 580, 320,
            "position={right bottom}");
        p.fit_textline(
            " at reference point (580, 20): fit_image(image, 580, 20, "
            "\"scale=0.5 orientate=west position={right bottom}\");",
            580, 300, "position={right bottom}");

        p.end_page_ext("");

        /* Start page 2 */
        p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
        p.setfont(font, 12);


        /* --------------------------------------
         * Fit the image into a box with clipping
         * --------------------------------------
         */

        /* The "boxsize" option defines a box with a given width and height and
         * its lower left corner located at the reference point.
         * "position={right top}" positions the image on the top right of the
         * box.
         * "fitmethod=clip" clips the image to fit it into the box.
         */
        sprintf(buf, "boxsize={%d %d} position={right top} fitmethod=clip",
            bw, bh);
        p.fit_image(image, x, y, buf);

        /* Output some descriptive text */
        p.fit_textline(
            "fit_image(image, x, y, \"boxsize={400 200} position={right top} "
            "fitmethod=clip\");", 20, y+bh+10, "");


        /* ---------------------------------------------------
         * Fit the image into a box with proportional resizing
         * ---------------------------------------------------
         */

        /* The "boxsize" option defines a box with a given width and height and
         * its lower left corner located at the reference point.
         * "position={center}" positions the image in the center of the
         * box.
         * "fitmethod=meet" resizes the image proportionally until its height
         * or width completely fits into the box.
         * The "showborder" option is used to illustrate the borders of the box.
         */
        sprintf(buf,
            "boxsize={%d %d} position={center} fitmethod=meet showborder",
            bw, bh);
        p.fit_image(image, x, y-=yoffset, buf);

        /* Output some descriptive text */
        p.fit_textline(
            "fit_image(image, x, y, \"boxsize={400 200} position={center} "
            "fitmethod=meet showborder\");", 20, y+bh+10, "");


        /* ---------------------------------
         * Fit the image into a box entirely
         * ---------------------------------
         */

        /* The "boxsize" option defines a box with a given width and height and
         * its lower left corner located at the reference point.
         * By default, the image is positioned in the lower left corner of the
         * box.
         * "fitmethod=entire" resizes the image proportionally until its height
         * or width completely fits into the box.
         */
        sprintf(buf, "boxsize={%d %d} fitmethod=entire", bw, bh);
        p.fit_image(image, x, y-=yoffset, buf);

        /* Output some descriptive text */
        p.fit_textline(
            "fit_image(image, x, y, \"boxsize={400 200} fitmethod=entire\");",
            20, y+bh+10, "");

        p.end_page_ext("");

        p.close_image(image);

        p.end_document("");

    }

    catch (PDFlib::Exception &ex) {
        cerr << "PDFlib exception occurred:" << endl;
        cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
            << ": " << ex.get_errmsg() << endl;
        return 2;
    }

    return 0;
}
Example #6
0
int
main(void)
{
    try {
	/* This is where the data files are. Adjust as necessary. */
	string searchpath = "../data";

	string imagefile = "nesrin.jpg";

	int row, col, font, image, tf=-1, tbl=-1;
	int rowmax=50, colmax=5;
	PDFlib p;
	double llx= 50, lly=50, urx=550, ury=800;
	string headertext = "Table header (centered across all columns)";
	string result;
	char optlist[256];

	/* Dummy text for filling a cell with multi-line Textflow */
	string tf_text = 
"Lorem ipsum dolor sit amet, consectetur adi&shy;pi&shy;sicing elit, \
sed do eius&shy;mod tempor incidi&shy;dunt ut labore et dolore magna \
ali&shy;qua. Ut enim ad minim ve&shy;niam, quis nostrud exer&shy;citation \
ull&shy;amco la&shy;bo&shy;ris nisi ut ali&shy;quip ex ea commodo \
con&shy;sequat. Duis aute irure dolor in repre&shy;henderit in voluptate \
velit esse cillum dolore eu fugiat nulla pari&shy;atur. Excep&shy;teur \
sint occae&shy;cat cupi&shy;datat non proident, sunt in culpa qui officia \
dese&shy;runt mollit anim id est laborum. ";

	//  This means we must check return values of load_font() etc.
	p.set_parameter("errorpolicy", "return");

	p.set_parameter("SearchPath", searchpath);

	if (p.begin_document("starter_table.pdf", "") == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}
	p.set_info("Creator", "PDFlib starter sample");
	p.set_info("Title", "starter_table");

	/* -------------------- Add table cells -------------------- */

	/* ---------- Row 1: table header (spans all columns) */
	row = 1; col = 1;
	font = p.load_font("Times-Bold", "winansi", "");
	if (font == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}

	sprintf(optlist,"fittextline={position=center font=%d fontsize=14} "
			 "colspan=%d", font, colmax);

	tbl = p.add_table_cell(tbl, col, row, headertext, optlist);

	/* ---------- Row 2: various kinds of content */
	/* ----- Simple text cell */
	row++; col=1;

	sprintf(optlist, "fittextline={font=%d fontsize=10 orientate=west}", 
			 font);

	tbl = p.add_table_cell(tbl, col, row, "vertical line", optlist);
	if (tbl == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}

	/* ----- Colorized background */
	col++;

	sprintf(optlist, "fittextline={font=%d fontsize=10} "
			 "matchbox={fillcolor={rgb 0.9 0.5 0}}", font);

	tbl = p.add_table_cell(tbl, col, row, "some color", optlist); 
	/* ----- Multi-line text with Textflow */
	col++;
	font = p.load_font("Times-Roman", "winansi", "");
	if (font == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}

	sprintf(optlist, 
	    "charref fontname=Times-Roman encoding=winansi fontsize=8 ");

	tf = p.add_textflow(tf, tf_text, optlist);
	if (tf == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}

	sprintf(optlist, 
	    "marginleft=2 marginright=2 margintop=2 marginbottom=2 " 
	    "textflow=%d",  tf);

	tbl = p.add_table_cell(tbl, col, row, "", optlist);
	if (tbl == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}

	/* ----- Rotated image */
	col++;

	image = p.load_image("auto", imagefile, "");
	if (image == -1) {
	    cerr << "Couldn't load image: " << p.get_errmsg() << endl; return 2;
	}

	sprintf(optlist, "image=%d fitimage={orientate=west}", image);

	tbl = p.add_table_cell(tbl, col, row, "", optlist);
	if (tbl == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}

	/* ----- Diagonal stamp */
	col++;

	sprintf(optlist, "fittextline={font=%d  fontsize=10 stamp=ll2ur}", 
			 font);

	tbl = p.add_table_cell(tbl, col, row, "entry void", optlist);
	if (tbl == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return 2;
	}

	/* ---------- Fill row 3 and above with their numbers */
	for (row++; row <= rowmax; row++) {
	    for (col = 1; col <= colmax; col++) {
		char num[80];

		sprintf(num, "Col %d/Row %d", col, row);
		sprintf(optlist,
		    "colwidth=20%% fittextline={font=%d fontsize=10}", font);
		tbl = p.add_table_cell(tbl, col, row, num, optlist);
	    }
	}

	/* ---------- Place the table on one or more pages ---------- */

	/*
	 * Loop until all of the table is placed; create new pages
	 * as long as more table instances need to be placed.
	 */
	do {
	    p.begin_page_ext(0, 0, "width=a4.width height=a4.height");

	    /* Shade every other row; draw lines for all table cells.
	     * Add "showcells showborder" to visualize cell borders 
	     */
	    sprintf(optlist,
	    "header=1 fill={{area=rowodd fillcolor={gray 0.9}}} " 
	    "stroke={{line=other}} ");

	    /* Place the table instance */
	    result = p.fit_table(tbl, llx, lly, urx, ury, optlist);
	    if (result == "_error") {
		cerr << "Couldn't place table: " << p.get_errmsg()
		    << endl; return 2;
	    }

	    p.end_page_ext("");

	} while (result == "_boxfull");

	/* Check the result; "_stop" means all is ok. */
	if (result != "_stop") {
	    if (result == "_error") {
		cerr << "Error when placing table: " << p.get_errmsg()
		    << endl; return 2;
	    } else {
		/* Any other return value is a user exit caused by
		 * the "return" option; this requires dedicated code to
		 * deal with.
		 */
		cerr << "User return found in Textflow" << endl; return 2;
	    }
	}
	/* This will also delete Textflow handles used in the table */
	p.delete_table(tbl, "");

	p.end_document("");
    }

    catch (PDFlib::Exception &ex) {
	cerr << "PDFlib exception occurred:" << endl;
	cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
	    << ": " << ex.get_errmsg() << endl;
	return 2;
    }

    return 0;
}