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

Tags: formsfile selection fieldsubmit formiText 5

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 setOptions() method. If you want a file chooser to appear, you also have to add a JavaScript action:

TextField file = new TextField(
    writer, new Rectangle(36, 788, 559, 806), "myfile");
PdfFormField upload = file.getTextField();
upload.setAdditionalActions(PdfName.U, PdfAction.javaScript(
    "this.getField('myfile').browseForFileToSubmit();", writer));

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.