mzorola
mzorola

Reputation: 51

iTextSharp, code to submit all fields on post

I am using iTextSharp and I have run across a problem I cannot solve. I am loading an existing PDF form that contains a mixture of text fields and check boxes. I dynamically add a submit button to the form before sending it to the browser where it will be filled out then resubmitted to the server. My problem is that I cannot get the PDF to submit back ALL of the filled and empty fields. I have identified the code that should return all fields but I can't get empty check boxes to return. I am at the point where I think its not possible. Anyone doing this using a PDF Acroform created using Acrobat Pro - not LiveCycle. See below for code snippet that creates the button. I am doing this in C# asp.net 4 using iTextSharp.

    ButtonRect = MakeButtonRect(PDFFormOut, ButtonPos);
    button = new PushbuttonField(PDFFormOut.Writer, ButtonRect, "postSubmit");
    button.BackgroundColor = BaseColor.LIGHT_GRAY;
    button.BorderColor = GrayColor.BLACK;
    button.BorderWidth = 1f;
    button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
    button.TextColor = GrayColor.GREEN;
    button.FontSize = 8f;
    button.Text = "Submit";
    button.Visibility = PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT;
    field = button.Field;
    field.Put(PdfName.TU, new PdfString("Save changes and return to the folder."));
    **field.Action = PdfAction.CreateSubmitForm(@"http://" + Application["WebSiteRoot"].ToString() + @"/SaveForm.aspx?OP=SAV", null, PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_INCLUDE_NO_VALUE_FIELDS);**
    PDFFormOut.AddAnnotation(field, 1);

Upvotes: 5

Views: 2991

Answers (1)

Chris Haas
Chris Haas

Reputation: 55457

Per the PDF spec, Section 12.7.5.2 - Submit-Form Action, table 237, you're setting the 2nd flag bit which is IncludeNoValueFields which works for text fields but as you've seen not checkboxes. Unfortunately, if you check out 12.7.3.1 - Field Dictionaries General, table 221 you'll see that each field within the PDF can have their 3rd bit set in their individual field flags which is the NoExport bit which IncludeNoValueFields does not include. So passing name/values over HTTP like this unfortunately won't work for you.

However, you can set the submit action to include PdfAction.SUBMIT_XFDF which will give you an XML file with every field, including ones not marked for export.

int flags = PdfAction.SUBMIT_XFDF | PdfAction.SUBMIT_INCLUDE_NO_VALUE_FIELDS;
field.Action = PdfAction.CreateSubmitForm(@"http://localhost/", null, flags);

Below is the result of a form with two checkboxes, one radio button group and one text field.

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
 <fields>
  <field name="Check Box3"><value>Off</value></field>
  <field name="Check Box4"><value>Off</value></field>
  <field name="Text7"><value/></field>
  <field name="YesOrNo"><value>Yes</value></field>
  <field name="postSubmit"/>
 </fields>
 <ids original="B8B00E1D7C7ADB119BD056BC3BD5CA9F" modified="05CBB9BE1720DA2B6FC45E071B74F7E3"/>
 </xfdf>

Server-side you can just work with the Request.InputStream to get your XML.

The other option would be to actually just submit the PDF over the wire and using something server-side to parse it:

int flags = PdfAction.SUBMIT_PDF;

Upvotes: 4

Related Questions