Q: How can I submit FDF, XFDF or HTML data to a server and receive a response back?
A: Here is a sample service that receive a PDF file and then responds with an FDF file.
package com.qoppa.jaxrs.service.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @Path("test/formsubmit") public class FormSubmitService { /** * Post some form data and return fdf. * @param documentAction * @return */ @POST @Produces("application/vnd.fdf") public Response post(InputStream formData) { try { copyFile(formData, "C:/test/formdata.xfdf"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return getFileResponse(new File("C:/test/response.fdf"), "response.fdf"); } private void copyFile(InputStream file, String to) throws IOException { // save the file directly so we don't have to worry about passwords FileOutputStream os = new FileOutputStream(to); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = file.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } file.close(); os.flush(); os.close(); } private Response getFileResponse(File file, String fileName) { ResponseBuilder response = Response.ok((Object) file); response.header("Content-Disposition", "attachment; filename=" + fileName); return response.build(); } } |
Our PDF components jPDFNotes and jPDFEditor are able to interpret this response and import back the fields data from the FDF file (you’ll see the field values change).
Here is a pdf_submit_sample.pdf to test this form submission that contains a few buttons with a Javascript action calling this.submitForm with the different cSubmitAs parameters.