Create, Edit, and Convert HTML in Java
Aspose.HTML for Java is a class library for working with HTML and related web formats in Java applications. The HTMLDocument class loads HTML from a file, URL, string, or stream and exposes a standards-based DOM for querying and editing elements. The Converter class converts HTML, SVG, MHTML, EPUB, and Markdown to PDF, XPS, DOCX, and raster image formats through source-specific methods.
Use the API when HTML processing must run in a server, desktop application or another Java workflow. Documents can be created or loaded, changed through DOM methods, inspected with CSS selectors or XPath, saved with linked resources, rendered, merged, or converted without automating a desktop browser.
Choose a Java HTML Task
Choose the workflow that matches the application. Each guide focuses on the relevant Java classes, provides a concise example, and links to detailed documentation or API Reference pages.
newCreate HTML
Create a document from markup or DOM nodes, or load HTML from a file, URL, string, or stream.
Convert Web Documents
Convert HTML, XHTML, SVG, MHTML, EPUB, or Markdown to PDF, XPS, DOCX, and image formats.
newMerge HTML
Render several HTML documents in a defined order to one PDF, XPS, DOCX, or TIFF file.
Extract Data
Find links and resources in the DOM, resolve their URLs, and save the required content.
Download Files
Request a remote resource through the document network service and save the response locally.
newExtract Images
Find image elements, resolve relative source URLs, and download referenced image files.
Extract SVG
Save inline SVG markup and download external SVG resources referenced by an HTML page.
Edit HTML Before Converting It in Java
Load an HTML file with
HTMLDocument
, find the first heading with
querySelector()
, and update its text.
PdfSaveOptions
selects PDF output, while
Converter.convertHTML()
writes the modified document to output.pdf.
Edit HTML and Convert It to PDF in Java
// Load and edit the HTML document
try (HTMLDocument document = new HTMLDocument("input.html")) {
Element heading = document.querySelector("h1");
if (heading != null) {
heading.setTextContent("Updated report");
}
// Convert the updated document to PDF
PdfSaveOptions options = new PdfSaveOptions();
Converter.convertHTML(document, options, "output.pdf");
}
Supported Web Formats and Java APIs
The input format determines which document class or conversion method starts the workflow. The save-options class passed to the converter determines the output format and its settings.
| Source format | Java API entry point | Typical operations |
|---|---|---|
| HTML and XHTML | HTMLDocument | Create, load, query, edit, save, render, merge, and convert |
| SVG | SVGDocument and convertSVG() | Load, inspect, edit, render, and convert vector content |
| MHTML and MHT | Converter.convertMHTML() | Convert a web archive from a file or stream to supported output |
| EPUB | Converter.convertEPUB() | Convert an EPUB to fixed-layout or image output |
| Markdown | Converter.convertMarkdown() | Convert Markdown to HTML and render the resulting document |
Depending on the source workflow, output can be written as PDF, XPS, DOCX, TIFF, PNG, JPEG, BMP, GIF, HTML, MHTML, or Markdown. Use the matching save-options class to configure page size, resolution, resource handling, and other format-specific settings.
HTML Processing Guides for Java
Start with Create and Load HTML Documents to open HTML from files, URLs, strings, and streams or build a document from markup. Continue with Edit an HTML Document for DOM changes and HTML Navigation for CSS selector and XPath examples.
For rendered output, use the HTML Converter guide to choose the conversion method for each source format. Fine-Tuning Converters explains page setup, resolution, resource handling, and other output settings.
Frequently Asked Questions
1. Does Aspose.HTML for Java require a web browser?
No. Aspose.HTML for Java parses, edits, renders, and converts documents inside the Java application without automating a browser executable. It can retrieve external stylesheets, images, fonts, and other resources when the document references them and network access is available.
2. How can I load HTML from a file, URL, string, or stream in Java?
Use an
HTMLDocument
constructor that matches the source. Pass a file path or URL directly. When loading markup from a string or stream, also provide a base URL so relative CSS, image, font, and link URLs can be resolved correctly.
3. What is the difference between saving and converting an HTML document?
HTMLDocument.save()
writes the current DOM as HTML, MHTML, or Markdown and can process linked resources.
Converter
methods render a document to PDF, XPS, DOCX, or an image using the save-options class for the selected output format.
4. Is Aspose.HTML for Java free?
Aspose.HTML for Java is a commercial library. You can download an evaluation version to test the API or request a temporary license to evaluate it without the limitations of evaluation mode.
Install Aspose.HTML for Java
Add Aspose.HTML for Java from the official Aspose repository. Replace ASPOSE_HTML_VERSION with an exact version from the
Aspose.HTML for Java releases
page, then reload or rebuild the project so the dependency is resolved.
Add the repository and dependency to the Maven project:
Maven – pom.xml
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html</artifactId>
<version>ASPOSE_HTML_VERSION</version>
</dependency>
</dependencies>Add the repository and dependency to the Gradle project:
Gradle – build.gradle
repositories {
maven {
url = uri('https://releases.aspose.com/java/repo/')
}
}
dependencies {
implementation 'com.aspose:aspose-html:ASPOSE_HTML_VERSION'
}Use a JDK compatible with the selected release. For manual JAR installation, Kotlin DSL, updates, and dependency troubleshooting, see Install Aspose.HTML for Java .
Explore Aspose.HTML for Java Guides
Continue with focused Java guides for creating, converting, merging, and extracting web content.
Support and Learning Resources
- Developer Resources
- Documentation
- GitHub Examples
- API Reference
- Product Support
- Free Support
- Paid Support
- Blog
- Release Notes
- Why Aspose.HTML for Java
- Aspose Customers
- Success Stories