0

I have a PDF document that has a number of fill-in fields (text and checkboxes). How do you go about referencing these objects so I can manipulate its value, then push that updated PDF to the user where they can save it to their desired location. I'm not finding any good documentation on how to do this.

Any information you could give me would be greatly appreciated.

Right now I am using the following code, but when I go to open it my PDF reader tells me that it is damaged or corrupt.

            String srcPath = Server.MapPath("~/App_Data/w9.pdf");
        String dstPath = Server.MapPath("~/App_Data/w9_" + Session.SessionID + "_updated.pdf");

        if (File.Exists(dstPath)) {
            File.Delete(dstPath);
        }

        File.Copy(srcPath, dstPath);

        PdfReader reader = new PdfReader(dstPath);

        try
        {
            PdfStamper stamper = new PdfStamper(reader, Response.OutputStream);
            stamper.FormFlattening = true;
            stamper.AcroFields.SetField("topmostSubform[0].Page1[0].f1_01_0_[0]", "Homer J Simpson");

            Response.ContentType = "application/pdf";
            Response.BufferOutput = true;
            Response.AppendHeader("Content-Disposition", "attachment; filename=W9_" +Session.SessionID + "_Complete.pdf");

            Response.TransmitFile(dstPath);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
Jim
  • 149
  • 2
  • 11
  • Jim there are a lot of examples of how to do this on the Web do a Google Search [how to enumerate all fields in PDF document](http://stackoverflow.com/questions/3041883/how-do-i-enumerate-all-the-fields-in-a-pdf-file-in-itextsharp) – MethodMan Jan 22 '13 at 23:39

1 Answers1

0

I'm the author of the iText documentation. If you have a static form, then you can use the methods described in this chapter to fill out your form: http://www.manning.com/lowagie2/samplechapter6.pdf

Read section 6.3.5 to find out how to fill out a form created with Open Office. However, it looks as if you are using a form created with Adobe LiveCycle (fieldnames like topmostSubform[0].Page1[0].f1_01_0_[0] are typical for XFA forms).

Section 6.3.5 concludes with the following remark:

"There’s much more to say about forms, but we can’t go into further detail until we’ve talked about annotations. Also, I haven’t said anything about the different types of PDF forms yet: there are forms based on AcroForm technology (like the form you created using Open Office), and there are XFA forms (created with Adobe Designer). This will have to wait until chapter 8, because we have one more group of PDF manipulation classes left to cover."

If your form is a dynamic XFA form without any AcroForm technology inside, you need XFA Worker to flatten it. You can download a trial version of XFA Worker here: http://itextsupport.com/download/xfaworker.html

The first question you have to answer before you continue is: do I have an AcroForm or an XFA form? If you have an XFA form, you'll need either Adobe LiveCycle ES or XFA Worker to fill and flatten such a form. Both are closed source products. I don't know of any free / open source library that supports XFA flattening.

PS: if by "damaged" you mean that you get a message saying This document enabled extended features... then you are breaking the Reader enabling. You should read chapter 8 of "iText in Action - Second Edition" to find out how to avoid this message.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165