Ejemplo n.º 1
0
int
main(void)
{
    try {
	/* This is where the data files are. Adjust as necessary. */
	const wstring searchpath = L"../data";

	PDFlib p;
	const wstring pdffiles[] =
	{
		L"PDFlib-real-world.pdf",
		L"PDFlib-datasheet.pdf",
		L"TET-datasheet.pdf",
		L"PLOP-datasheet.pdf",
		L"pCOS-datasheet.pdf"
	};
        const int filecount = sizeof(pdffiles) / sizeof(pdffiles[0]);
	int i;

	//  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_pdfmerge.pdf", L"") == -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_pdfmerge");

	for (i = 0; i < filecount; i++) {
	    int indoc, endpage, pageno, page; 

	    /* Open the input PDF */
	    indoc = p.open_pdi_document(pdffiles[i], L"");
	    if (indoc == -1) {
		wcerr << L"Error: " << p.get_errmsg() << endl;
                return 2;
	    }

	    endpage = static_cast<int>(p.pcos_get_number(indoc, L"length:pages"));


	    /* Loop over all pages of the input document */
	    for (pageno = 1; pageno <= endpage; pageno++)
	    {
		page = p.open_pdi_page(indoc, pageno, L"");
		if (page == -1) {
		    wcerr << L"Error: " << p.get_errmsg() << endl;
		    continue;
		}
		/* Dummy page size; will be adjusted later */
		p.begin_page_ext(10, 10, L"");

		/* Create a bookmark with the file name */
		if (pageno == 1)
		    p.create_bookmark(pdffiles[i], L"");

		/* Place the imported page on the output page, and
		 * adjust the page size
		 */
		p.fit_pdi_page(page, 0, 0, L"adjustpage");
		p.close_pdi_page(page);

		p.end_page_ext(L"");
	    }
	    p.close_pdi_document(indoc);
	}

	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;
}
Ejemplo n.º 2
0
int
main(void)
{
    try {
	int         i, stationery, page, regularfont, boldfont;
	const wstring infile = L"stationery.pdf";
	// This is where font/image/PDF input files live. Adjust as necessary.
	wstring      searchpath = L"../data";
	const double left = 55;
	const double right = 530;
	time_t      timer;
	struct tm   ltime;
	double      fontsize = 12, leading, y;
	double      sum, total;
	double      pagewidth = 595, pageheight = 842;
	wostringstream buf;
	wostringstream optlist;
	const wstring baseopt =
	    L"ruler        {   30 45     275   375   475} "
	    L"tabalignment {right left right right right} "
	    L"hortabmethod ruler fontsize 12 ";
	int         textflow;
	PDFlib      p;
	const wstring closingtext =
	    L"Terms of payment: <fillcolor={rgb 1 0 0}>30 days net. "
	    L"<fillcolor={gray 0}>90 days warranty starting at the day "
	    L"of sale. This warranty covers defects in workmanship only. "
	    L"<fontname=Helvetica-BoldOblique encoding=host>Kraxi Systems, "
	    L"Inc. <resetfont>will, at its option, repair or replace the "
	    L"product under the warranty. This warranty is not transferable. "
	    L"No returns or exchanges will be accepted for wet products.";

	struct articledata {
	    articledata(wstring n, double pr, int q):
		name(n), price(pr), quantity(q) {}
	    wstring name;
	    double price;
	    int quantity;
	};

	const articledata data[] = {
	    articledata(L"Super Kite",         20,     2),
	    articledata(L"Turbo Flyer",        40,     5),
	    articledata(L"Giga Trash",         180,    1),
	    articledata(L"Bare Bone Kit",      50,     3),
	    articledata(L"Nitty Gritty",       20,     10),
	    articledata(L"Pretty Dark Flyer",  75,     1),
	    articledata(L"Free Gift",          0,      1),
	};

        const int ARTICLECOUNT = sizeof(data) / sizeof(data[0]);

	static const wstring months[] = {
	    L"January", L"February", L"March", L"April", L"May", L"June",
	    L"July", L"August", L"September", L"October", L"November", L"December"
	};


	//  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"invoice.pdf", L"") == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl; return(2);
        }

        p.set_info(L"Creator", L"invoice.cpp");
        p.set_info(L"Author", L"Thomas Merz");
        p.set_info(L"Title", L"PDFlib invoice generation demo (C++)");

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

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

        boldfont = p.load_font(L"Helvetica-Bold", L"host", L"");
	if (boldfont == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}
        regularfont = p.load_font(L"Helvetica", L"host", L"");
	if (regularfont == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}
        leading = fontsize + 2;

        // Establish coordinates with the origin in the upper left corner.
        p.begin_page_ext(pagewidth, pageheight, L"topdown");

        p.fit_pdi_page(page, 0, pageheight, L"");
        p.close_pdi_page(page);

        p.setfont(regularfont, 12);

        // Print the address
        y = 170;
        p.set_value(L"leading", leading);

        p.show_xy(L"John Q. Doe", left, y);
        p.continue_text(L"255 Customer Lane");
        p.continue_text(L"Suite B");
        p.continue_text(L"12345 User Town");
        p.continue_text(L"Everland");

        // Print the header and date

        p.setfont(boldfont, 12);
        y = 300;
        p.show_xy(L"INVOICE", left, y);

        time(&timer);
        ltime = *localtime(&timer);

        buf.str(L"");
        buf << months[ltime.tm_mon].c_str() << " " << ltime.tm_mday
            << ", " << ltime.tm_year + 1900;

        p.fit_textline(buf.str(), right, y, L"position {100 0}");

        // Print the invoice header line
        p.setfont(boldfont, 12);

        // L"position {0 0}" is left-aligned, L"position {100 0}" right-aligned
        y = 370;
        optlist << baseopt << " font " << boldfont;
	textflow = p.create_textflow(
                L"\tITEM\tDESCRIPTION\tQUANTITY\tPRICE\tAMOUNT",
                optlist.str());
        if (textflow == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}
 
	p.fit_textflow(textflow, left, y-leading, right, y, L"");
	p.delete_textflow(textflow);

        // Print the article list

        p.setfont(regularfont, 12);
        y += 2*leading;
        total = 0;

        optlist.str(L"");
        optlist << baseopt << " font " << regularfont;

        for (i = 0; i < (int)ARTICLECOUNT; i++) {
            sum = data[i].price * data[i].quantity;

            buf.str(L"");
            buf << L"\t" << (i + 1) << L"\t" << data[i].name << L"\t"
                << data[i].quantity << L"\t";
            buf.setf(ios_base::fixed, ios_base::floatfield);
            buf.precision(2);
            buf << data[i].price << L"\t" << sum;

	    textflow = p.create_textflow(buf.str(), optlist.str());
	    if (textflow == -1) {
		wcerr << L"Error: " << p.get_errmsg() << endl;
		return 2;
	    }
	    p.fit_textflow(textflow, left, y-leading, right, y, L"");
	    p.delete_textflow(textflow);

            y += leading;
            total += sum;
        }

        y += leading;
        p.setfont(boldfont, 12);
        buf.str(L"");
        buf.setf(ios_base::fixed, ios_base::floatfield);
        buf.precision(2);
        buf << total;
        p.fit_textline(buf.str(), right, y, L"position {100 0}");

        // Print the closing text

        y += 5*leading;
        optlist.str(L"");
        optlist << L"alignment=justify leading=120% "
                << L"fontname=Helvetica fontsize=12 encoding=host ";

	textflow = p.create_textflow(closingtext, optlist.str());
	if (textflow == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
            return 2;
	}
	p.fit_textflow(textflow, left, y+6*leading, right, y, L"");
	p.delete_textflow(textflow);

        p.end_page_ext(L"");
        p.end_document(L"");
        p.close_pdi_document(stationery);
    }

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

    return 0;
}
Ejemplo n.º 3
0
int
main(void)
{
    try {
	PDFlib p;
	int         i, blockcontainer, page;
	const string 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.
         */
	const string searchpath = "../data";
	struct blockdata {
	    blockdata(string n, string v): name(n), value(v){}
	    string name;
	    string value;
	};

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

#define BLOCKCOUNT (sizeof(data)/sizeof(data[0]))
	//  This means we must check return values of load_font() etc.
	p.set_parameter("errorpolicy", "return");

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

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

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

        p.set_info("Creator", "businesscard.cpp");
        p.set_info("Author", "Thomas Merz");
        p.set_info("Title","PDFlib block processing sample (C++)");

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

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

        p.begin_page_ext(20, 20, "");              // dummy page size

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

        // Fill all text blocks with dynamic data 
        for (i = 0; i < (int) BLOCKCOUNT; i++) {
            if (p.fill_textblock(page, data[i].name, data[i].value,
		"embedding encoding=host") == -1) {
		cerr << "Error: " << p.get_errmsg() << endl;
            }
        }

        p.end_page_ext("");
	p.close_pdi_page(page);

        p.end_document("");
	p.close_pdi_document(blockcontainer);
    }

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

    return 0;
}
Ejemplo n.º 4
0
int
main(void)
{
    try {
	int         i, stationery, page, regularfont, boldfont;
	string      infile = "stationery.pdf";
	// This is where font/image/PDF input files live. Adjust as necessary.
	string      searchpath = "../data";
	const double left = 55;
	const double right = 530;
	time_t      timer;
	struct tm   ltime;
	double      fontsize = 12, leading, y;
	double      sum, total;
	double      pagewidth = 595, pageheight = 842;
	char        buf[256];
	char        optlist[256];
	char        baseopt[256] =
	    "ruler        {   30 45     275   375   475} "
	    "tabalignment {right left right right right} "
	    "hortabmethod ruler fontsize 12 ";
	int         textflow;
	PDFlib      p;
	string      closingtext =
	    "Terms of payment: <fillcolor={rgb 1 0 0}>30 days net. "
	    "<fillcolor={gray 0}>90 days warranty starting at the day of sale. "
	    "This warranty covers defects in workmanship only. "
	    "<fontname=Helvetica-BoldOblique encoding=host>Kraxi Systems, Inc. "
	    "<resetfont>will, at its option, repair or replace the "
	    "product under the warranty. This warranty is not transferable. "
	    "No returns or exchanges will be accepted for wet products.";

	struct articledata {
	    articledata(string n, double pr, int q):
		name(n), price(pr), quantity(q){}
	    string name;
	    double price;
	    int quantity;
	};

	articledata data[] = {
	    articledata("Super Kite",         20,     2),
	    articledata("Turbo Flyer",        40,     5),
	    articledata("Giga Trash",         180,    1),
	    articledata("Bare Bone Kit",      50,     3),
	    articledata("Nitty Gritty",       20,     10),
	    articledata("Pretty Dark Flyer",  75,     1),
	    articledata("Free Gift",          0,      1),
	};

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

	static const string months[] = {
	    "January", "February", "March", "April", "May", "June",
	    "July", "August", "September", "October", "November", "December"
	};


	//  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("invoice.pdf", "") == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
        }

        p.set_info("Creator", "invoice.cpp");
        p.set_info("Author", "Thomas Merz");
        p.set_info("Title", "PDFlib invoice generation demo (C++)");

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

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

        boldfont = p.load_font("Helvetica-Bold", "host", "");
	if (boldfont == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}
        regularfont = p.load_font("Helvetica", "host", "");
	if (regularfont == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}
        leading = fontsize + 2;

        // Establish coordinates with the origin in the upper left corner.
        p.begin_page_ext(pagewidth, pageheight, "topdown");

        p.fit_pdi_page(page, 0, pageheight, "");
        p.close_pdi_page(page);

        p.setfont(regularfont, 12);

        // Print the address
        y = 170;
        p.set_value("leading", leading);

        p.show_xy("John Q. Doe", left, y);
        p.continue_text("255 Customer Lane");
        p.continue_text("Suite B");
        p.continue_text("12345 User Town");
        p.continue_text("Everland");

        // Print the header and date

        p.setfont(boldfont, 12);
        y = 300;
        p.show_xy("INVOICE",       left, y);

        time(&timer);
        ltime = *localtime(&timer);
        sprintf(buf, "%s %d, %d", months[ltime.tm_mon].c_str(),
			    ltime.tm_mday, ltime.tm_year + 1900);
        p.fit_textline(buf, right, y, "position {100 0}");

        // Print the invoice header line
        p.setfont(boldfont, 12);

        // "position {0 0}" is left-aligned, "position {100 0}" right-aligned
        y = 370;
	sprintf(buf, "\tITEM\tDESCRIPTION\tQUANTITY\tPRICE\tAMOUNT");

	sprintf(optlist, "%s font %d ", baseopt, boldfont);

	textflow = p.create_textflow(buf, optlist);
        if (textflow == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}
 
	p.fit_textflow(textflow, left, y-leading, right, y, "");
	p.delete_textflow(textflow);

        // Print the article list

        p.setfont(regularfont, 12);
        y += 2*leading;
        total = 0;

	sprintf(optlist, "%s font %d ", baseopt, regularfont);

        for (i = 0; i < (int)ARTICLECOUNT; i++) {
            sum = data[i].price * data[i].quantity;

	    sprintf(buf, "\t%d\t%s\t%d\t%.2f\t%.2f", 
		i+1, (char *)data[i].name.c_str(), data[i].quantity, 
		data[i].price, sum);

	    textflow = p.create_textflow(buf, optlist);
	    if (textflow == -1) {
		cerr << "Error: " << p.get_errmsg() << endl;
		return(2);
	    }
	    p.fit_textflow(textflow, left, y-leading, right, y, "");
	    p.delete_textflow(textflow);

            y += leading;
            total += sum;
        }

        y += leading;
        p.setfont(boldfont, 12);
        sprintf(buf, "%.2f", total);
        p.fit_textline(buf, right, y, "position {100 0}");

        // Print the closing text

        y += 5*leading;
	strcpy(optlist, "alignment=justify leading=120% ");
	strcat(optlist, "fontname=Helvetica fontsize=12 encoding=host ");

	textflow = p.create_textflow(closingtext, optlist);
	if (textflow == -1) {
	    cerr << "Error: " << p.get_errmsg() << endl; return(2);
	}
	p.fit_textflow(textflow, left, y+6*leading, right, y, "");
	p.delete_textflow(textflow);

        p.end_page_ext("");
        p.end_document("");
        p.close_pdi_document(stationery);
    }

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

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

	PDFlib p;
	const wstring pdfinput = L"TET-datasheet.pdf";

	const wstring	docoptlist = L"requiredmode=minimum";
	int	count, pcosmode;
	int	i, doc;
	wstring	objtype;

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

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

	/* We do not create any output document, so no call to
	 * begin_document() is required.
	 */

	/* Open the input document */
	if ((doc = p.open_pdi_document(pdfinput, docoptlist)) == -1) {
	    wcerr << L"Error: " << p.get_errmsg() << endl;
	    return 2;
	}

	/* --------- general information (always available) */

	pcosmode = static_cast<int>(p.pcos_get_number(doc, L"pcosmode"));

	wcout << L"   File name: " <<
	    p.pcos_get_string(doc,L"filename") << endl;

	wcout << L" PDF version: " <<
	    p.pcos_get_string(doc, L"pdfversionstring") << endl;

	wcout << L"  Encryption: " <<
	    p.pcos_get_string(doc, L"encrypt/description") << endl;

	wcout << L"   Master pw: " <<
	    ((p.pcos_get_number(doc, L"encrypt/master") != 0)
	    ? L"yes":L"no") << endl;

	wcout << L"     User pw: " <<
	    ((p.pcos_get_number(doc, L"encrypt/user") != 0)
	    ? L"yes" : L"no") << endl;

	wcout << L"Text copying: " <<
	    ((p.pcos_get_number(doc, L"encrypt/nocopy") != 0)
	    ? L"no":L"yes") << endl;

	wcout << L"  Linearized: " <<
	    ((p.pcos_get_number(doc, L"linearized") != 0)
	    ? L"yes" : L"no") << endl;

	if (pcosmode == 0) {
	    wcout << L"Minimum mode: no more information available" << endl;
	    return 0;
	}
	wcout << endl;

	/* --------- more details (requires at least user password) */
	wcout << L"PDF/X status: " << p.pcos_get_string(doc, L"pdfx") << endl;

	wcout << L"PDF/A status: " << p.pcos_get_string(doc, L"pdfa") << endl;

        bool xfa_present =
            p.pcos_get_string(doc, L"type:/Root/AcroForm/XFA") != L"null";
        wcout << L"    XFA data: " << (xfa_present ? L"yes" : L"no") << endl;

	wcout << L"  Tagged PDF: " <<
            ((p.pcos_get_number(doc, L"tagged") != 0) ? L"yes" : L"no") << endl;

	wcout << L"No. of pages: " <<
	    static_cast<int>(p.pcos_get_number(doc, L"length:pages")) << endl;

	wcout << L" Page 1 size: width=" << fixed << setprecision(3) <<
	     p.pcos_get_number(doc, L"pages[0]/width") << L", height=" <<
	     p.pcos_get_number(doc, L"pages[0]/height") << endl;
	/* reset formatting to default */
	wcout << resetiosflags(ios::floatfield) << setprecision(6);

	count = static_cast<int>(p.pcos_get_number(doc, L"length:fonts"));
	wcout << L"No. of fonts: " <<  count << endl;

	for (i = 0; i < count; i++) {
	    wostringstream fontstr;

            fontstr.str(L"");
            fontstr << L"fonts[" << i << L"]";
            const wstring& font = fontstr.str();

	    if (p.pcos_get_number(doc, font + L"/embedded") != 0)
		wcout << L"embedded ";
	    else
		wcout << L"unembedded ";

	    wcout << p.pcos_get_string(doc, font + L"/type") << L" font ";
	    wcout << p.pcos_get_string(doc, font + L"/name") << endl;
	}

	wcout << endl;

	const bool plainmetadata =
	        p.pcos_get_number(doc, L"encrypt/plainmetadata") != 0;

	if (pcosmode == 1 && !plainmetadata
	            && p.pcos_get_number(doc, L"encrypt/nocopy") != 0) {
	    wcout << L"Restricted mode: no more information available" << endl;
	    return 0;
	}

	/* ----- document info keys and XMP metadata (requires master pw) */

	count = (int) p.pcos_get_number(doc, L"length:/Info");

	for (i=0; i < count; i++) {
	    wostringstream infostr;
	    wstring key;

            infostr.str(L"");
            infostr << L"/Info[" << i << L"]";
            const wstring& info = infostr.str();

	    objtype = p.pcos_get_string(doc, L"type:" + info);

	    key = p.pcos_get_string(doc, info + L".key");
	    wcout.width(12);
	    wcout << key << L": ";

	    /* Info entries can be stored as string or name objects */
	    if (objtype == L"name" || objtype == L"string") {
		wcout << L"'" + p.pcos_get_string(doc, info) << L"'" << endl;
	    }
            else {
		wcout << L"(" + objtype << L" object)" << endl;
	    }
	}

	wcout << endl << L"XMP metadata: ";

	objtype = p.pcos_get_string(doc, L"type:/Root/Metadata");
	if (objtype == L"stream") {
	    const unsigned char *contents;
	    int len;

	    contents = p.pcos_get_stream(doc, &len, L"", L"/Root/Metadata");
	    wcout << len << L" bytes ";
	}
        else {
	    wcout << L"not present";
	}
	wcout << endl;

	p.close_pdi_document(doc);
    }
    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;
}