Convert HTML to XPS Using Java

XPS is a document format created by Microsoft that offers several benefits, including advanced security features like digital signatures for added document protection. XPS files offer access rights management and produce high-quality, printable documents. Thus, HTML to XPS conversion can provide greater flexibility and functionality for managing and sharing documents, making it a valuable tool in certain situations.

In this article, you will find information on how to convert HTML to XPS using Aspose.HTML for Java library and how to apply XpsSaveOptions.

HTML to XPS by a single line of Java code

The static methods of the Converter class are primarily used as the easiest way to convert an HTML code into various formats. You can convert HTML to XPS in your Java application literally with a single line of code!

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

In the example, we use the convertHTML(content, baseUri, options, outputPath) method of the Converter class that takes four parameters: string with HTML code to be converted, the base folder for the input HTML file, an instance of the XpsSaveOptions class, and the output file path where the converted file will be saved.

Convert HTML to XPS in Java

Converting a file to another format using the convertHTML() method is a sequence of operations among which document loading and saving:

  1. Load an HTML file using the HTMLDocument class.
  2. Create a new XpsSaveOptions object. Use the empty XpsSaveOptions() constructor to convert with the default save options.
  3. Use the convertHTML() method of the Converter class to save HTML as an XPS file. You need to pass the HTMLDocument, XpsSaveOptions, and output file path to the convertHTML() method to convert HTML to XPS.
 1// Prepare an HTML code and save it to the file.
 2String code = "<span>Hello World!!</span>";
 3try (java.io.FileWriter fileWriter = new java.io.FileWriter($o("document.html"))) {
 4    fileWriter.write(code);
 5}
 6
 7// Initialize an HTML document from the file
 8HTMLDocument document = new HTMLDocument($i("canvas.html"));
 9
10// Initialize XpsSaveOptions
11XpsSaveOptions options = new XpsSaveOptions();
12
13// Convert HTML to XPS
14Converter.convertHTML(document, options, $o("canvas-output.xps"));

You can download the complete examples and data files from GitHub.

Save Options – XpsSaveOptions Class

With XpsSaveOptions you have the ability to personalize the rendering process for your XPS files. This includes customizing elements such as the page size, margins, background color, CSS media type, and more.

MetodDescription
getCss()Gets a CssOptions object which is used for configuration of CSS properties processing.
setBackgroundColor(value)Sets the color that will fill the background of every page. By default, this property is Transparent.
setPageSetup(value)This method sets a page setup object and uses it for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for internal images, in pixels per inch. By default this property is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.

To learn more about XpsSaveOptions please read Fine-Tuning Converters article.

Convert HTML to XPS using XpsSaveOptions

The XpsSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting HTML to XPS format. The following example shows how to use XpsSaveOptions and create an XPS file with custom page-size and background color:

  1. Load an HTML file using one of the HTMLDocument() constructors of the HTMLDocument class.
  2. Create a new XpsSaveOptions object.
  3. Use the convertHTML() method of the Converter class to save HTML as an XPS file. You must pass parameters to it – document, options and output file path savePath to perform the conversion.

The following example shows how to use XpsSaveOptions and create an XPS file with custom page-size and background color:

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

You can download the complete examples and data files from GitHub.

Check the quality of HTML to XPS conversion with our online HTML to XPS Converter. Upload, convert your files and get the result in a few seconds. Try our forceful HTML to XPS Converter for free now!

Text “HTML to XPS Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.