oleole wrote:

Hello,

I have a pdf model with several fields to fill in. The
idea is to load the model, add it to my pdf and fill
in the fields with the appropriate data (returned by a
SQL).

I'm currently using this code:
// Init
PdfReader reader = new PdfReader(@"d:\model_uk.pdf");
FileStream Output = new FileStream(@"d:\output.pdf",
FileMode.Create);
PdfStamper stamp1 = new PdfStamper(reader, Output);
// First page
AcroFields form1 = stamp1.AcroFields;
form1.SetField("Date", "10/10/2005");
form1.SetField("Communication", "002/0014/35150");
stamp1.Close();
// Second page
stamp1 = new PdfStamper(reader, Output);
form1 = stamp1.AcroFields;
form1.SetField("Date", "11/10/2005");
form1.SetField("Communication", "001/0014/35150");
stamp1.Close();


It doesn't work and return this message right after my
"Second Page" comment:  "The original document was
reused. Read it again from file".

So my question: how would I add several times the same
page to my output pdf and fill the fields on the fly ?

Thanking you in advance.

You are making two errors:
1) PdfStamper 'tampers' the reader.
 You need to repeat
 reader = new PdfReader("d:\model.pdf");
2) When you close the first stamper instance,
 you close the PDF file.
 When you create the second stamper instance,
 you OVERWRITE the PDF file.
 You can't just add new pages to it the way you do.

Take a look at this code:
PdfReader reader;
PdfStamper stamper;
ByteArrayOutputStream baos
RandomAccessFileOrArray model = new RandomAccessFileOrArray("d:\model.pdf");

// first stamper
reader = new PdfReader(model, null);
baos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, baos);
form = stamper.getAcroFields();
form.setField("Date", "11/05/2005");
stamper.setFormFlattening(true);
stamper.close();
// copy content of first page
reader = new PdfReader(baos.toByteArray());
Document document = new Document(reader.getPageSizeWithRotation(1));
PdfCopy writer = new PdfCopy(document, new FileOutputStream("HelloWorldStampCopy.pdf"));
document.open();
writer.addPage(writer.getImportedPage(reader, 1));

// second stamper reader = new PdfReader(letter, null);
baos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, baos);
form = stamper.getAcroFields();
form.setField("Date", "16/05/2005");
stamper.setFormFlattening(true);
stamper.close();

// copy content of second page reader = new PdfReader(baos.toByteArray());
writer.addPage(writer.getImportedPage(reader, 1));
document.close();

You might want to adapt the code, so that the stamping
and copying is done in a loop.
br,
Bruno


-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to