Hello, Dear members.
I have a question regarding how to generate the pdf document, insert a
couple of text fields, and then fill in those text fields from a database at
the same time.
Here is a good example (
http://itextpdf.com/examples/index.php?page=example&id=157) on how to create
and fill in a form.
But I don't want the intermediate file, I just want to create the pdf file,
and fill in the fields in memory, and then send it to the client over Http ,
what's the best way to do it? Basically, I want to get the AcroFields from
the writer, not from the reader.
Thank you for your help.
public class TextFields implements PdfPCellEvent
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPCellEvent.html> {
public static final String RESULT1 =
"results/part2/chapter08/text_fields.pdf";
public static final String RESULT2 =
"results/part2/chapter08/text_filled.pdf";
protected int tf;
public static void main(String[] args) throws DocumentException
<http://api.itextpdf.com/com/itextpdf/text/DocumentException.html>,
IOException {
TextFields example = new TextFields(0);
example.createPdf(RESULT1);
example.manipulatePdf(RESULT1, RESULT2);
}
public TextFields(int tf) {
this.tf = tf;
}
public void manipulatePdf(String src, String dest) throws
IOException, DocumentException
<http://api.itextpdf.com/com/itextpdf/text/DocumentException.html> {
PdfReader
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfReader.html> reader
= new PdfReader
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfReader.html>(src);
PdfStamper
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfStamper.html>
stamper = new PdfStamper
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfStamper.html>(reader,
new FileOutputStream(dest));
AcroFields
<http://api.itextpdf.com/com/itextpdf/text/pdf/AcroFields.html> form =
stamper.getAcroFields();
form.setField("text_1", "Bruno Lowagie");
form.setFieldProperty("text_2", "fflags", 0, null);
form.setFieldProperty("text_2", "bordercolor", BaseColor
<http://api.itextpdf.com/com/itextpdf/text/BaseColor.html>.RED, null);
form.setField("text_2", "bruno");
form.setFieldProperty("text_3", "clrfflags", TextField
<http://api.itextpdf.com/com/itextpdf/text/pdf/TextField.html>.PASSWORD,
null);
form.setFieldProperty("text_3", "setflags", PdfAnnotation
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfAnnotation.html>.FLAGS_PRINT,
null);
form.setField("text_3", "12345678", "xxxxxxxx");
form.setFieldProperty("text_4", "textsize", new Float(12), null);
form.regenerateField("text_4");
stamper.close();
}
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename) throws DocumentException
<http://api.itextpdf.com/com/itextpdf/text/DocumentException.html>,
IOException {
Document
<http://api.itextpdf.com/com/itextpdf/text/Document.html> document =
new Document <http://api.itextpdf.com/com/itextpdf/text/Document.html>();
PdfWriter
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfWriter.html>.getInstance(document,
new FileOutputStream(filename));
document.open();
PdfPCell
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPCell.html> cell;
PdfPTable
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPTable.html> table =
new PdfPTable <http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPTable.html>(2);
table.setWidths(new int[]{ 1, 2 });
table.addCell("Name:");
cell = new PdfPCell
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPCell.html>();
cell.setCellEvent(new TextFields(1));
table.addCell(cell);
table.addCell("Loginname:");
cell = new PdfPCell
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPCell.html>();
cell.setCellEvent(new TextFields(2));
table.addCell(cell);
table.addCell("Password:");
cell = new PdfPCell
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPCell.html>();
cell.setCellEvent(new TextFields(3));
table.addCell(cell);
table.addCell("Reason:");
cell = new PdfPCell
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPCell.html>();
cell.setCellEvent(new TextFields(4));
cell.setFixedHeight(60);
table.addCell(cell);
document.add(table);
document.close();
}
public void cellLayout(PdfPCell
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPCell.html> cell,
Rectangle <http://api.itextpdf.com/com/itextpdf/text/Rectangle.html>
rectangle, PdfContentByte
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfContentByte.html>[]
canvases) {
PdfWriter
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfWriter.html> writer
= canvases[0].getPdfWriter();
TextField
<http://api.itextpdf.com/com/itextpdf/text/pdf/TextField.html> text =
new TextField
<http://api.itextpdf.com/com/itextpdf/text/pdf/TextField.html>(writer,
rectangle,
String.format("text_%s", tf));
text.setBackgroundColor(new GrayColor
<http://api.itextpdf.com/com/itextpdf/text/pdf/GrayColor.html>(0.75f));
switch(tf) {
case 1:
text.setBorderStyle(PdfBorderDictionary
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfBorderDictionary.html>.STYLE_BEVELED);
text.setAlignment(Element
<http://api.itextpdf.com/com/itextpdf/text/Element.html>.ALIGN_RIGHT);
text.setText("Enter your name here...");
text.setFontSize(0);
text.setAlignment(Element
<http://api.itextpdf.com/com/itextpdf/text/Element.html>.ALIGN_CENTER);
text.setOptions(TextField
<http://api.itextpdf.com/com/itextpdf/text/pdf/TextField.html>.REQUIRED);
break;
case 2:
text.setMaxCharacterLength(8);
text.setOptions(TextField
<http://api.itextpdf.com/com/itextpdf/text/pdf/TextField.html>.COMB);
text.setBorderStyle(PdfBorderDictionary
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfBorderDictionary.html>.STYLE_SOLID);
text.setBorderColor(BaseColor
<http://api.itextpdf.com/com/itextpdf/text/BaseColor.html>.BLUE);
text.setBorderWidth(2);
break;
case 3:
text.setBorderStyle(PdfBorderDictionary
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfBorderDictionary.html>.STYLE_INSET);
text.setOptions(TextField
<http://api.itextpdf.com/com/itextpdf/text/pdf/TextField.html>.PASSWORD);
text.setVisibility(TextField
<http://api.itextpdf.com/com/itextpdf/text/pdf/TextField.html>.VISIBLE_BUT_DOES_NOT_PRINT);
break;
case 4:
text.setBorderStyle(PdfBorderDictionary
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfBorderDictionary.html>.STYLE_DASHED);
text.setBorderColor(BaseColor
<http://api.itextpdf.com/com/itextpdf/text/BaseColor.html>.RED);
text.setBorderWidth(2);
text.setFontSize(8);
text.setText("Enter the reason why you want to win a free
accreditation for the Foobar Film Festival");
text.setOptions(TextField
<http://api.itextpdf.com/com/itextpdf/text/pdf/TextField.html>.MULTILINE
| TextField
<http://api.itextpdf.com/com/itextpdf/text/pdf/TextField.html>.REQUIRED);
break;
}
try {
PdfFormField
<http://api.itextpdf.com/com/itextpdf/text/pdf/PdfFormField.html>
field = text.getTextField();
if (tf == 3) {
field.setUserName("Choose a password");
}
writer.addAnnotation(field);
}
catch(IOException ioe) {
throw new ExceptionConverter
<http://api.itextpdf.com/com/itextpdf/text/ExceptionConverter.html>(ioe);
}
catch(DocumentException
<http://api.itextpdf.com/com/itextpdf/text/DocumentException.html> de)
{
throw new ExceptionConverter
<http://api.itextpdf.com/com/itextpdf/text/ExceptionConverter.html>(de);
}
}}
------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/