Developer Tools
|
Office Productivity Applications
|
Platform-Agnostic APIs
|
Home | Online Demos | Downloads | Buy Now | Support | About Us | News | Working Together | Contact Us
PDFOne Java's text writing function has the ability to accept placeholders within text strings and replace them with actual values determined or got from an event handler at run-time. PDFOne Java provides several built-in placeholders, such as PageNo, PageCount, Author, Filename, Path, FilePath. Apart from these built-in placeholders, developers can define their own custom placeholders. The values for these custom placeholders are queried by PDFOne Java, as and when they are encountered, by invoking the event onCustomPlaceHolder. To provide values for custom placeholders, developers should implement the event onCustomPlaceHolder(String variable, PdfDocument d, int pagenumber), as defined in com.gnostice.pdfone.PdfCustomPlaceholderHandler.
This feature helps developers generate PDF documents with dynamic content. Also, it eliminates the need for creating and maintaining variables to store the dynamic content and the need to convert them to string type and format them before writing to PDF at run-time.
When specifying placeholders in a string, the placeholders need to be bracketed between '<%' and '%>' tags.
Note: Sometimes, it so happens that the syntax for specifying placeholders conflicts with the text that you need to write to PDF. In these cases, just precede the placeholder tags with a '~' symbol. The '~' symbol will cause the text to be written without any processing for placeholders.
Following is the code for a simple mail-merge application.
import java.awt.Color; import java.io.IOException; import com.gnostice.pdfone.PDFOne; import com.gnostice.pdfone.PdfCustomPlaceholderHandler; import com.gnostice.pdfone.PdfDocument; import com.gnostice.pdfone.PdfException; import com.gnostice.pdfone.PdfLinkAnnot; import com.gnostice.pdfone.PdfMeasurement; import com.gnostice.pdfone.PdfPage; import com.gnostice.pdfone.PdfRect; import com.gnostice.pdfone.PdfWriter; import com.gnostice.pdfone.encodings.PdfEncodings; import com.gnostice.pdfone.fonts.PdfFont; public class PlaceHolders implements PdfCustomPlaceholderHandler { static { PDFOne.activate("T95VZE:W8HBPVA:74VQ8QV:LO4V8", "9B1HRZAP:X5853ERNE:5EREMEGRQ:TX1R10"); } static String customerNames[] = {"Akili Mbovu", "Sheila Isbell", "Kristen McGregor"}; static String productNames[] = {"PDFOne Java", "PDFOne .Net", "PDFtoolkit"}; static String productVersions[] = {"1.41", "1.41", "2.43"}; static String downloadLinks[] = {"PDFOneJavadownload.asp", "PDFOneNETdownload.asp", "pdftoolkitdownload.asp"}; static int count = -1; public String onCustomPlaceHolder(String variable, PdfDocument d, int pagenumber) { if (variable.equals("customerName")) { return customerNames[++count]; } else if (variable.equals("productName")) { return productNames[count]; } else if (variable.equals("productVersion")) { return productVersions[count]; } else if (variable.equals("downloadLink")) { return downloadLinks[count]; } return null; } public static void main(String[] args) throws IOException, PdfException { try { /* Create a PdfDocument instance */ PdfDocument d = new PdfDocument(); PdfPage p; /* Create a font */ PdfFont font = PdfFont.create("Arial", PdfFont.BOLD, 20, PdfEncodings.CP1252); font.setColor(Color.black); /* Create a font */ PdfFont contentFont = PdfFont.create("Courier", 20, PdfEncodings.CP1252); contentFont.setColor(Color.black); /* Create a font object to display the hyperlink */ PdfFont linkFont = PdfFont.create("Courier", PdfFont.UNDERLINE, 20, PdfEncodings.CP1252); linkFont.setColor(Color.blue); /* Create a Font for Header and Footer Text */ PdfFont headerFont = PdfFont.create("Arial", PdfFont.BOLD | PdfFont.ITALIC | PdfFont.STROKE_AND_FILL, 20, PdfEncodings.CP1252); headerFont.setColor(Color.black); for (int i = 0; i < customerNames.length ; i++) { /* Creates a new page */ p = new PdfPage(800, 900, 100, 100, 50, 50, 50, 50, PdfMeasurement.MU_POINTS); p.writeText("Bangalore,", font, 540, 10); /* Write a string containing predefined placeHolders */ p.writeText( "<%date 'dd/MM/yyyy'%>", font, 540, 40); /* Write a string containing custom placeHolder */ p.writeText("Dear <%customerName%>,", font, 0, 80); /* Write a string containing custom placeHolders */ p.writeText( "We are pleased to introduce the new version of " + "<%productName%>, Gnostice " + "<%productName%> v<%productVersion%>.", contentFont, 0, 120); p.writeText( "Downloads are available at", contentFont, 0, 200); p.writeText( "http://www.gnostice.com" + "/" + downloadLinks[i], linkFont, 0, 220); PdfLinkAnnot lnkAnnot = new PdfLinkAnnot( new PdfRect(0, 225, 560, 20), Color.white); lnkAnnot.addActionURI("http://www.gnostice.com" + "/" + downloadLinks[i]); p.addAnnotation(lnkAnnot); p.writeText("Best Regards,", contentFont, 0, 270); p.writeText("<%productName%> Team,", font, 0, 300); p.writeText("Gnostice Information Technologies Pvt. Ltd.", font, 0, 320); p.setCph(new PlaceHolders()); /* Add page to the document */ d.add(p); } d.addHeaderText("Announcement for Gnostice <%productName%>", headerFont, PdfPage.HP_MIDDLE | PdfPage.VP_CENTRE, true, "-"); d.addFooterText("Gnostice Information Technologies " + "Pvt. Ltd., Bangalore, India", headerFont, PdfPage.HP_LEFT | PdfPage.VP_CENTRE, true, "-"); d.setOpenAfterSave(true); d.save(args[0]); d.close(); } catch (ArrayIndexOutOfBoundsException n) { System.out.println( "Usage : java PlaceHolders [Output File Path]"); } } }
---o0O0o---
Our .NET Developer Tools | |
---|---|
Gnostice Document Studio .NETMulti-format document-processing component suite for .NET developers. |
PDFOne .NETA .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications. |
Our Delphi/C++Builder developer tools | |
---|---|
Gnostice Document Studio DelphiMulti-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms. |
eDocEngine VCLA Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools. |
PDFtoolkit VCLA Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents. |
Our Java developer tools | |
---|---|
Gnostice Document Studio JavaMulti-format document-processing component suite for Java developers. |
PDFOne (for Java)A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications. |
Our Platform-Agnostic Cloud and On-Premises APIs | |
---|---|
StarDocsCloud-hosted and On-Premises REST-based document-processing and document-viewing APIs |
Privacy | Legal | Feedback | Newsletter | Blog | Resellers | © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved. |