1/* $Id: businesscard.java 14574 2005-10-29 16:27:43Z bonefish $ 2 * 3 * PDFlib client: hello example in Java 4 */ 5 6import java.io.*; 7import com.pdflib.pdflib; 8import com.pdflib.PDFlibException; 9 10public class businesscard 11{ 12 public static void main (String argv[]) 13 { 14 int font; 15 pdflib p = null ; 16 int i, blockcontainer, page; 17 String infile = "boilerplate.pdf"; 18 /* This is where font/image/PDF input files live. Adjust as necessary. 19 * 20 * Note that this directory must also contain the LuciduxSans font 21 * outline and metrics files. 22 */ 23 String searchpath = "../data"; 24 String[][] data = { 25 { "name", "Victor Kraxi" }, 26 { "business.title", "Chief Paper Officer" }, 27 { "business.address.line1", "17, Aviation Road" }, 28 { "business.address.city", "Paperfield" }, 29 { "business.telephone.voice","phone +1 234 567-89" }, 30 { "business.telephone.fax", "fax +1 234 567-98" }, 31 { "business.email", "victor@kraxi.com" }, 32 { "business.homepage", "www.kraxi.com" }, 33 }; 34 35 try{ 36 p = new pdflib(); 37 38 // open new PDF file 39 if (p.open_file("businesscard.pdf") == -1) { 40 throw new Exception("Error: " + p.get_errmsg()); 41 } 42 43 p.set_parameter("SearchPath", searchpath); 44 45 p.set_info("Creator", "businesscard.java"); 46 p.set_info("Author", "Thomas Merz"); 47 p.set_info("Title","PDFlib block processing sample (Java)"); 48 49 blockcontainer = p.open_pdi(infile, "", 0); 50 if (blockcontainer == -1) { 51 throw new Exception("Error: " + p.get_errmsg()); 52 } 53 54 page = p.open_pdi_page(blockcontainer, 1, ""); 55 if (page == -1) { 56 throw new Exception("Error: " + p.get_errmsg()); 57 } 58 59 p.begin_page(20, 20); // dummy page size 60 61 // This will adjust the page size to the block container's size. 62 p.fit_pdi_page(page, 0, 0, "adjustpage"); 63 64 // Fill all text blocks with dynamic data 65 for (i = 0; i < (int) data.length; i++) { 66 if (p.fill_textblock(page, data[i][0], data[i][1], 67 "embedding encoding=winansi") == -1) { 68 System.err.println("Warning: " + p.get_errmsg()); 69 } 70 } 71 72 p.end_page(); // close page 73 p.close_pdi_page(page); 74 75 p.close(); // close PDF document 76 p.close_pdi(blockcontainer); 77 78 } catch (PDFlibException e) { 79 System.err.print("PDFlib exception occurred in businesscard sample:\n"); 80 System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() + 81 ": " + e.getMessage() + "\n"); 82 } catch (Exception e) { 83 System.err.println(e.getMessage()); 84 } finally { 85 if (p != null) { 86 p.delete(); /* delete the PDFlib object */ 87 } 88 } 89 } 90} 91