HTML Form Editor - Java

HTML Form Element

The HTMLFormElement represents a section containing input controls for submitting user information. Using our library, you can easily fulfill the HTML forms and send data directly from your Java application. To make this process is much more comfortable, we designed the com.aspose.html.forms Package. The main element of this Package is FormEditor. It allows you to find an existing form in the document, create a new from scratch, iterate through the elements, fill them up with data and surely send the information to the remote server. In this article, you will learn how to work with FormEditor object.

In this article, you will learn how to work with a FormEditor object to manipulate HTML form in the document, input controls, and submit information. For the testing purpose in this article, we use the form designed by service.

How to Use FormEditor in Java

The following code snippet demonstrates how to use the Aspose.HTML for Java library to fill and submit an HTML form programmatically:

  1. Create a new instance of the HTMLDocument class using HTMLDocument(url) constructor. Specify the URL of the HTML form to be filled out and submitted.
  2. Use the create(document, index) method to create an instance of the FormEditor class, passing in it the HTMLDocument instance and the index of the form being edited (value 0 refers to the first form in the HTML document).
  3. Fill the data up using classes and methods of the com.aspose.html.forms and save the filled HTML form to a file.
  4. Create an instance of the FormSubmitter class.
  5. Call the submit() method of the FormSubmitter class to submit the form data to the remote server.
  6. Check the status of the form submission result using the isSuccess() method.
 1// Initialize an instance of HTML document from '' url
 2com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("");
 4// Create an instance of the FormEditor
 5com.aspose.html.forms.FormEditor editor = com.aspose.html.forms.FormEditor.create(document, 0);
 7// You can fill the data up using direct access to the input elements, like this:
 8com.aspose.html.forms.InputElement custname = editor.addInput("custname");
 9custname.setValue("John Doe");
13// or this:
14com.aspose.html.forms.TextAreaElement comments = editor.getElement(com.aspose.html.forms.TextAreaElement.class, "comments");
15comments.setValue("MORE CHEESE PLEASE!");
17// or even by performing a bulk operation, like this one:
18java.util.Map<String, String> dictionary = new java.util.HashMap<>();
19dictionary.put("custemail", "");
20dictionary.put("custtel", "+1202-555-0290");
22// Create an instance of FormSubmitter
23com.aspose.html.forms.FormSubmitter submitter = new com.aspose.html.forms.FormSubmitter(editor);
25// Submit the form data to the remote server
26// If you need you can specify user-credentials and timeout for the request, etc.
27com.aspose.html.forms.SubmissionResult result = submitter.submit();
29// Check the status of the result object
30if (result.isSuccess()) {
31    // Check whether the result is in json format
32    if (result.getResponseMessage().getHeaders().getContentType().getMediaType().equals("application/json")) {
33        // Dump result data to the console
34        System.out.println(result.getContent().readAsString());
35    }
36    else {
37        // Load the result data as an HTML document
38        com.aspose.html.dom.Document doc = result.loadDocument();
39        // Inspect HTML document here
40    }

Aspose.HTML offers free online Converters for converting HTML, XHTML, MHTML, EPUB, XML and Markdown files to a variety of popular formats. You can easily convert HTML to PDF, HTML to JPG, SVG to PDF, MHTML to PDF, or MD to HTML. Just select a file, choose the format to convert, and you’re done. It’s fast and completely free!

Text “Banner Free Online Converters”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.