June 25, 2018
Hot Topics:

A PDF Solution for All Programming Platforms

  • July 12, 2006
  • By Victor Volkman
  • Send Email »
  • More Articles »

Now you are ready to use a PDF Import function ("pdi") to import your template file. Note that this code won't link on PDFLite as this is a PDFlib function:

51       blockcontainer = p.open_pdi(infile, "", 0);
52       if (blockcontainer == -1) {
53       cerr << "Error: " << p.get_errmsg() << endl;
54          return(2);
55       }

Your stuff belongs on the first page, so fetch that page and make it active for writing:

57       page = p.open_pdi_page(blockcontainer, 1, "");
58       if (page == -1) {
59       cerr << "Error: " << p.get_errmsg() << endl;
60          return(2);
61       }
63       p.begin_page_ext(20, 20, "");    // dummy page size
65       // This will adjust the page size to the block container's size.
66       p.fit_pdi_page(page, 0, 0, "adjustpage");

Now, you use the block filling functions, part of the PDFlib Personalization Server (PPS) that allows you to handle variable data blocks of type Text, Image, and PDF. Each data block is addressed by name (for example, "business.telephone.fax"). In this simplistic scheme, you have all of this just sitting in a static structure (go back to Line 23 for details):

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

Because you are now fairly deep into the document, you must remember to finish the page, close the imported document page, finalize your own document, and then close the imported file handle:

76       p.end_page_ext("");
77    p.close_pdi_page(page);
79       p.end_document("");
80    p.close_pdi(blockcontainer);
81    }
83    catch (PDFlib::Exception &ex) {
84       cerr << "PDFlib exception occurred in businesscard sample: "
         << endl;
85       cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname()
86       << ": " << ex.get_errmsg() << endl;
87    return 99;
88    }
90    return 0;
91 }

Now, you can open or print your output file (businesscard.pdf) in Adobe Acrobat, e-mail it to a customer, burn it to a CD, or upload it to an online printer.

For Further Reading

Beginning PDF Programming with PHP and PDFlib
by Ron Goff
Foreword by Thomas Merz

ISBN 0973589841

Although written with PHP developers in mind, this book covers all the basics of the PDFlib API, which is essentially platform-independent. I highly recommend it for anyone who is starting off with PDFlib development or curious about what it can really do.

Meet the Whole Family

You truly have only scratched the surface of what PDFlib offers. The programming API is also part of a larger product family:

  • PDFlib contains all functions required to create PDF output containing text, vector graphics and images, plus hypertext elements.
  • PDFlib+PDI includes all PDFlib functions, plus the PDF Import Library (PDI) for including pages from existing PDF documents in the generated output.
  • PDFlib Personalization Server (PPS) includes PDFlib+PDI, plus additional functions for automatically filling PDFlib blocks. Blocks are placeholders on the page that can be filled with text, images, or PDF pages. They can be created interactively with the PDFlib Block Plugin for Adobe Acrobat (Mac or Windows) and will be filled automatically with PPS. The plugin is included in PPS.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries; just drop an e-mail to sysop@HAL9K.com.

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date