Receive a submitted PDF form in a Jakarta Servlet and read its scalar, choice, date, and table fields.

Handling the POST request. The Jakarta Servlet reads the submitted document from the request stream and passes its bytes to the form submission parser.

Reading values in Java. The servlet method in Parsing Forms Submission Results/src/main/java/com/stimulsoft/forms/SubmissionServlet.java demonstrates scalar fields, selections, dates, table cells, and totals:
private void parseSubmission(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {

            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            int nRead;
            byte[] buff = new byte[1024];

            while ((nRead = req.getInputStream().read(buff, 0, buff.length)) != -1) {
                buffer.write(buff, 0, nRead);
            }

            buffer.flush();
            byte[] data = buffer.toByteArray();

            StiPdfFormSubmission submission = new StiPdfFormSubmission();
            submission.parseXFDF(data);

            System.out.println("All data");
            TreeSet<String> sortedList = new TreeSet<String>(submission.getData().keySet());
            for (String key : sortedList) {
                System.out.println(key + ": " + submission.getData().get(key));
            }

            System.out.println("Customer Name: " + submission.getStringValue("CustomerName"));
            System.out.println("Email: " + submission.getStringValue("CustomerEmail"));
            System.out.println("Adress street: " + submission.getStringValue("CustomerAddress.StreetAddress"));
            System.out.println("Adress city: " + submission.getStringValue("CustomerAddress.City"));
            System.out.println("Order date: " + submission.getDateTimeBoxValue("OrderDate", "MM/dd/yyyy"));
            System.out.println("Order number: " + submission.getDoubleValue("OrderNumber"));
            System.out.println("Delivery method: " + submission.getSingleSelectionValue("DeliveryMethod"));
            System.out.println("Payment type: " + submission.getSingleSelectionValue("PaymentType"));

            String tableName = "OrderItems";
            int rowsCount = submission.getTableRowsCount(tableName);
            int columnsCount = submission.getTableColumnsCount(tableName);
            for (int y = 0; y < rowsCount; y++) {
                for (int x = 0; x < columnsCount; x++) {
                    System.out.print(submission.getTableFieldValue(tableName, x, y) + "|");
                }
                System.out.println("");
            }

            for (int i = 0; i < 3; i++) {
                System.out.println("Total " + i + ": " + submission.getTableTotalFieldValue(tableName, i));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setStatus(HttpServletResponse.SC_OK);

        // If from Adobe Acrobat - return PDF response
        List<String> headers = Collections.list(req.getHeaderNames());
        if (String.join("", headers).contains("acrobat")) {
        // Serialized form data omitted for clarity.
            byte[] pdfBytes = Base64.getDecoder().decode(pdfFile);
            resp.getOutputStream().write(pdfBytes);
        }

    }

}

Working with the parsed data. The field map can be validated, persisted, or passed to application services just like data from a regular HTML form.

By using this website, you agree to the use of cookies for analytics and personalized content. Cookies store useful information on your computer to help us improve efficiency and usability. For more information, please read the privacy policy and cookie policy.