Convert HTML to XPS in Java

To convert HTML to XPS in Java, load the source with HTMLDocument, create XpsSaveOptions, and call Converter.convertHTML(). You can also pass an HTML string and base URI directly to a supported overload.

XPS is a fixed-layout document format designed to preserve page appearance for viewing and printing. Aspose.HTML for Java provides convertHTML() overloads for HTML conversion and XpsSaveOptions for configuring XPS output.

Convert HTML to XPS in One Line of Java Code

The following example passes an HTML string, base URI, XpsSaveOptions, and output path directly to Converter.convertHTML():

1// Convert HTML to XPS using Java
2
3// Invoke the convertHTML() method to convert HTML to XPS
4Converter.convertHTML("<h1>Convert HTML to XPS!</h1>", ".", new XpsSaveOptions(), "convert-with-single-line.xps");

The base URI ("." in the example) is used to resolve relative resources in the HTML string. The XPS file is saved as convert-with-single-line.xps.

Convert an HTML File to XPS

Follow these steps to convert an existing HTML file:

  1. Create or obtain an HTML file and load it with HTMLDocument.
  2. Create XpsSaveOptions for XPS output.
  3. Call Converter.convertHTML(document, options, outputPath) to convert HTML to XPS.

In this example, the input is document.html, and the result is saved as document-output.xps.

 1// Convert HTML to XPS using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<span>Hello, World!!</span>";
 5try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
 6    fileWriter.write(code);
 7}
 8
 9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("document.html");
11
12// Initialize XpsSaveOptions
13XpsSaveOptions options = new XpsSaveOptions();
14
15// Convert HTML to XPS
16Converter.convertHTML(document, options, "document-output.xps");

Download complete examples and data files from GitHub.

Customize XPS Output with XpsSaveOptions

Use XpsSaveOptions to configure page layout, resolution, background color, and CSS processing.

MethodUse it to
setBackgroundColor(value)Set the color that fills each output page. The default is transparent.
getPageSetup()Access the page setup used to configure output page size and margins.
setHorizontalResolution(value)Set horizontal resolution for internal images. The default is 300 dpi.
setVerticalResolution(value)Set vertical resolution for internal images. The default is 300 dpi.
getCss()Access CSS processing options.

To customize the conversion:

  1. Create and load the source HTML file.
  2. Create XpsSaveOptions.
  3. Set the background color, page size, and margins.
  4. Pass the document, options, and output path to Converter.convertHTML().

The following example converts save-options.html to save-options-output.xps with an Antique White background, page dimensions specified in inches, and custom margins:

 1// Convert HTML to XPS with custom settings using Java
 2
 3// Prepare HTML code and save it to a file
 4String code = "<h1>  XpsSaveOptions Class</h1>\r\n" +
 5        "<p>Using XpsSaveOptions Class, you can programmatically " +
 6        "apply a wide range of conversion parameters " +
 7        "such as BackgroundColor, PageSetup, etc.</p>";
 8
 9FileHelper.writeAllText("save-options.html", code);
10
11// Initialize an HTML document from the html file
12HTMLDocument document = new HTMLDocument("save-options.html");
13
14// Set up the page-size, margins and change the background color to AntiqueWhite
15XpsSaveOptions options = new XpsSaveOptions();
16options.setBackgroundColor(Color.getAntiqueWhite());
17options.getPageSetup().setAnyPage(new Page(new Size(Length.fromInches(4.9f), Length.fromInches(3.5f)), new Margin(30, 20, 10, 10)));
18
19// Convert HTML to XPS
20Converter.convertHTML(document, options, "save-options-output.xps");

For additional rendering controls, see Fine-Tuning Converters.

FAQ

Can I convert HTML to XPS for free?

Yes. Use the free online HTML to XPS Converter for a quick manual conversion. Aspose.HTML for Java is a commercial API for programmatic conversion and can be evaluated before licensing; see Licensing.

How do I control the XPS page size and margins?

Create XpsSaveOptions, access its page setup through getPageSetup(), and assign a Page with the required Size and Margin. Pass the configured options to Converter.convertHTML().

Try Online HTML to XPS Conversion

Use the free online HTML to XPS Converter for quick manual conversion without writing Java code.

Free Online HTML to XPS Converter