Generating PDFs in Java using iText. Support for Free flowing text -
We have the following nature's PDF templates that need to be generated by web applications:
Sample Paragraph:
Dear {customer.name},
Your attorney, {customer.lawyer.name}, has contacted us about your account, {customer.account.number } Requesting an immediate closure of the account.
...
The {...} fields given above are for adjusting the various Actor areas which will be placed as placeholders, so that they are populated with data .
But the problem is that the field {customer.lawyer.name} can be of different lengths, ranging from 10 to 50 characters,
using iText, how can we Are you generating a PDF for the above mentioned template so that the different lengths of the variable can be adjusted? Maybe wrap the text appropriately anywhere?
I used iText columns for layout letters. Here's an example from my work, note that I just typed it in the stack overflow, I have not compiled or tested it.
Imagine a file where two new lines in the row point to the paragraph, the text is wrapped by the editor. The following classes come up with a method that reads the file and a U.S. Letter size pdf emits up and down and 1 1/2 inch margins on each side with 1 inch margin.
** generates a letter * / public class letter generator {/ ** one inch is 72 digits * / personal last fixed int INCH = 72; / ** Generate a letter * / Public Zero generated () throws document Exception, IOException {BufferedReader = New BufferedReader (New FileReader ("letter.txt")); Document document = new document (pages aliiter); PdfWriter.getInstance (document, new FileOutputStream ("letter.pdf")); PdfContentByte CB = Writer. Gate Direct Content (); Column reader CT = new column text (CB); String paragraph; Int spacingBefore = 0; While ((para = in. Readline ()) = null {line = line.trim (); If (line.length ()! = 0) {// Shekhar, transfer your place holder to here. Paragraph p = new paragraph (line); P.setSpacingBefore (spacingBefore); Ct.addElement (P); Interval = 8; }} Ct.setSimpleColumn (INCH, INCH * 1.5f, 7.5f * INCH, 9.5f * INCH); Int Status = Column Text. START_COLUMN; While ((Status and column text .NO_MORE_TEXT) == 0) {status = ct.go (); Ct.setYLine (PageSize.LETTER.getHeight () - INCH); Ct.setSimpleColumn (INCH, INCH * 1.5f, 7.5f * INCH, 9.5f * INCH); Document.newPage (); } Document.close (); }}
For my life, I do not remember why I reset the Y-line.
iText is a great library, I have learned the most about it, which I know about it in the tutorials through the eText in the Action Book.
Unfortunately, I have never bought a book.
Comments
Post a Comment