Render HTML as an XPS Document

Aspose.HTML for Java renders HTML files, strings, and webpages to XPS with Converter.convertHTML() . Load the source into HTMLDocument, create XpsSaveOptions, and provide the output path. The resulting XPS preserves a fixed page appearance for viewing and printing.


HTML to XPS Java Code Example

The example loads a local HTML file, creates the XPS save options, and writes the fixed-layout result to output.xps.

Convert HTML to XPS in Java

import com.aspose.html.HTMLDocument;
import com.aspose.html.converters.Converter;
import com.aspose.html.saving.XpsSaveOptions;

// Load the HTML document
try (HTMLDocument document = new HTMLDocument("document.html")) {
    // Create the XPS save options
    XpsSaveOptions options = new XpsSaveOptions();

    // Convert HTML to XPS
    Converter.convertHTML(document, options, "output.xps");
}

Preview HTML to XPS Conversion Online

Upload an HTML file or specify a webpage URL to inspect the XPS result before adding the same workflow to Java code.

HTML to XPS API Steps

  1. Add Aspose.HTML for Java to the project.
  2. Load the source with HTMLDocument .
  3. Create an XpsSaveOptions instance.
  4. Call Converter.convertHTML() with the document, options, and output path.

Set XPS Page Layout and Resolution

Use XpsSaveOptions to configure output page size, margins, background color, horizontal and vertical resolution, and CSS processing. These settings determine how the reflowable HTML source is paginated into the fixed-layout XPS document.

See Convert HTML to XPS in Java and the XpsSaveOptions API Reference .


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 .


HTML to XPS Conversion FAQ

1. What is the difference between HTML and XPS layout?

HTML normally reflows for the browser viewport, while XPS stores content on fixed pages. Page size, margins, CSS, and fonts therefore affect the converted pagination.

2. Can HTML print styles be used when creating XPS?

The renderer processes CSS when laying out the document. Configure the required media and CSS behavior through the save options and verify the result with the page styles used by the source.

3. Can I convert an HTML string directly to XPS?

Yes. Use a supported Converter.convertHTML() overload with the HTML string, a base URI for relative resources, XpsSaveOptions, and the output path.

4. Can I convert HTML to XPS online for free?

Yes. Use the free online HTML to XPS converter for a manual conversion, or use the Java API for application integration.

Related Java Conversions

Choose another output format for the same HTML source.