How to send a file to the server through a PDF?

Tags: formsfile selection fieldsubmit formiText 7

I'm dynamically creating some PDF files through my application, and I need to use several components (TextField, CheckBox, RadioButtons etc), and then submit the values to server. One of the requirements says that the user needs to be able to select and send files along with the other values. I did not find a specific component to this, and so I'm asking for some help with this situation.

Is there a way to create some kind of Input File, File Chooser, etc, and attach it on the generated PDF file? And then send this selected file to server?

Posted on StackOverflow on Jul 18, 2014 by user2230377

I've prepared an example that explains how to create such a field when creating a document from scratch: FileSelectionExample

A file selection field is created just like any other text field, except that you have to set a flag using the setFileSelect(true). If you want a file chooser to appear, you also have to add a JavaScript action:

PdfTextFormField field = PdfFormField.createText(pdfDoc, new Rectangle(36, 788, 523, 18), "myfile", "");
field.setFileSelect(true);
field.setAdditionalAction(PdfName.U, PdfAction.createJavaScript(
     "this.getField('myfile').browseForFileToSubmit();"));
form.addField(field);

In the full example, I also added a second field and after selecting the file with the browseForFileToSubmit() method, I set the focus to that other field. I'm doing this because the file name only becomes visible in the file selection field after that field loses focus.

Click this link if you want to see how to answer this question in iText 5.