1219820Sjeff// $Id: businesscard.cpp 14574 2005-10-29 16:27:43Z bonefish $ 2219820Sjeff// PDFlib client: hello example in C++ 3219820Sjeff// 4219820Sjeff// 5219820Sjeff 6219820Sjeff#include <iostream> 7219820Sjeff 8219820Sjeff#include "pdflib.hpp" 9219820Sjeff 10219820Sjeffint 11219820Sjeffmain(void) 12219820Sjeff{ 13219820Sjeff try { 14219820Sjeff PDFlib p; // the PDFlib object 15219820Sjeff int i, blockcontainer, page; 16219820Sjeff const string infile = "boilerplate.pdf"; 17219820Sjeff /* This is where font/image/PDF input files live. Adjust as necessary. 18219820Sjeff * 19219820Sjeff * Note that this directory must also contain the LuciduxSans font 20219820Sjeff * outline and metrics files. 21219820Sjeff */ 22219820Sjeff const string searchpath = "../data"; 23219820Sjeff struct blockdata { 24219820Sjeff blockdata(string n, string v): name(n), value(v){} 25219820Sjeff string name; 26219820Sjeff string value; 27219820Sjeff }; 28219820Sjeff 29219820Sjeff blockdata data[] = { 30219820Sjeff blockdata("name", "Victor Kraxi"), 31219820Sjeff blockdata("business.title", "Chief Paper Officer"), 32219820Sjeff blockdata("business.address.line1", "17, Aviation Road"), 33219820Sjeff blockdata("business.address.city", "Paperfield"), 34219820Sjeff blockdata("business.telephone.voice","phone +1 234 567-89"), 35219820Sjeff blockdata("business.telephone.fax", "fax +1 234 567-98"), 36219820Sjeff blockdata("business.email", "victor@kraxi.com"), 37219820Sjeff blockdata("business.homepage", "www.kraxi.com"), 38219820Sjeff }; 39219820Sjeff 40219820Sjeff#define BLOCKCOUNT (sizeof(data)/sizeof(data[0])) 41219820Sjeff 42219820Sjeff // open new PDF file 43219820Sjeff if (p.open_file("businesscard.pdf") == -1) { 44219820Sjeff cerr << "Error: " << p.get_errmsg() << endl; 45219820Sjeff return(2); 46219820Sjeff } 47219820Sjeff 48219820Sjeff p.set_parameter("SearchPath", searchpath); 49219820Sjeff 50219820Sjeff // This line is required to avoid problems on Japanese systems 51219820Sjeff p.set_parameter("hypertextencoding", "host"); 52219820Sjeff 53219820Sjeff p.set_info("Creator", "businesscard.cpp"); 54219820Sjeff p.set_info("Author", "Thomas Merz"); 55219820Sjeff p.set_info("Title","PDFlib block processing sample (C++)"); 56219820Sjeff 57219820Sjeff blockcontainer = p.open_pdi(infile, "", 0); 58219820Sjeff if (blockcontainer == -1) { 59219820Sjeff cerr << "Error: " << p.get_errmsg() << endl; 60219820Sjeff return(2); 61219820Sjeff } 62219820Sjeff 63219820Sjeff page = p.open_pdi_page(blockcontainer, 1, ""); 64219820Sjeff if (page == -1) { 65219820Sjeff cerr << "Error: " << p.get_errmsg() << endl; 66219820Sjeff return(2); 67219820Sjeff } 68219820Sjeff 69219820Sjeff p.begin_page(20, 20); // dummy page size 70219820Sjeff 71219820Sjeff // This will adjust the page size to the block container's size. 72219820Sjeff p.fit_pdi_page(page, 0, 0, "adjustpage"); 73219820Sjeff 74219820Sjeff // Fill all text blocks with dynamic data 75219820Sjeff for (i = 0; i < (int) BLOCKCOUNT; i++) { 76219820Sjeff if (p.fill_textblock(page, data[i].name, data[i].value, 77219820Sjeff "embedding encoding=host") == -1) { 78219820Sjeff cerr << "Error: " << p.get_errmsg() << endl; 79219820Sjeff } 80219820Sjeff } 81219820Sjeff 82219820Sjeff p.end_page(); // close page 83219820Sjeff p.close_pdi_page(page); 84219820Sjeff 85219820Sjeff p.close(); // close PDF document 86219820Sjeff p.close_pdi(blockcontainer); 87219820Sjeff } 88219820Sjeff catch (PDFlib::Exception &ex) { 89219820Sjeff cerr << "PDFlib exception occurred in businesscard sample: " << endl; 90219820Sjeff cerr << "[" << ex.get_errnum() << "] " << ex.get_apiname() 91219820Sjeff << ": " << ex.get_errmsg() << endl; 92219820Sjeff return 99; 93219820Sjeff } 94219820Sjeff 95219820Sjeff return 0; 96219820Sjeff} 97219820Sjeff