Skip to main content
Skip table of contents

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

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?

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 How to send a file to the server through a PDF? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.