Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » iText »

[iText-questions] Fill PDF-Document Formfields during Download by
 Servlet

Oliver Schoenwald

2007-06-29

Replies:

Hello,

I'm pretty new to this mailing list. So far I couldn't find a similar
question in the last postings, so if I happen to ask something that has
been discussed before, please route me to that thread.

I want to write a Java-Servlet using itext, which will open a certain
PDF-document with prepared form-fields (for adress, date, some other
textual content, empty by default) when called by the client. The
servlet should take some data from our database and fill the form-fields
of the PDF-document before giving it to the response of the request.

In my current approach I tried to use PDFReader and PDFStamper to open
the PDF-document and to fill the relevant fields with setField(). Then I
would send he Output of PdfStamper into the response-stream. However,
the Client only gets the empty document, no filled fields. What am I
doing wrong?

Here is the java method that I use to open the document, fill its fields
and deliver the filled-out document to the user:

--- snip ---
public void fillAndDeliver(HttpServletResponse response, String pdfdoc,
String address, String content) {

    PdfReader reader = new PdfReader(pdfdoc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Fill the Fields
    PdfStamper stamp = new PdfStamper(reader, baos);
    AcroFields form = stamp.getAcroFields();
     form.setField("address", address);
    form.setField("content", content);
    stamp.close();

    // setting some response headers
    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate,
post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");
    response.setContentType("application/pdf");
    response.setContentLength(baos.size());

    // write ByteArrayOutputStream to the ServletOutputStream
    ServletOutputStream out = response.getOutputStream();
    baos.writeTo(out);
    out.flush();

    return;
}    
--- snip! ---

Thank you in advance,

Oliver Sch?nwald

--
Oliver Sch?nwald, Diplom-Informatiker

FernUniversit?t Hagen
Zentrum f?r IT und Medien (ZMI) - Competence Center Anwendungsentwicklung
Universit?tsstr.21/AVZ - 58084 Hagen - Deutschland
fon: +49 2331 987 1721 - email: oliver.schoenwald@(protected)


Attachment: Oliver.Schoenwald.vcf
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/
©2008 junlu.com - Jax Systems, LLC, U.S.A.