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 and MemoryStreamProvider parameters.

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 code to XPS
2    com.aspose.html.converters.Converter.convertHTML("<h1>Convert HTML to XPS!</h1>", ".", new XpsSaveOptions(), Path.combine(getOutputDir(), "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.

Please take a look over the following Java code snippet which shows the process of converting HTML to XPS using Aspose.HTML for Java.

 1    // Prepare a path to a source HTML file
 2    String documentPath = Path.combine(getDataDir(), "canvas.html");
 3
 4    // Prepare a path for converted file saving 
 5    String savePath = Path.combine(getOutputDir(), "canvas-output.xps");
 6
 7    // Initialize an HTML document from the file
 8    HTMLDocument document = new HTMLDocument(documentPath);
 9    try {        }
10    finally { if (document != null) document.dispose(); }
11
12    // Initialize XpsSaveOptions 
13    XpsSaveOptions options = new XpsSaveOptions();
14
15    // Convert HTML to XPS
16    com.aspose.html.converters.Converter.convertHTML(document, options, savePath);

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

Save Options

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 in Java 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    String documentPath = Path.combine(getOutputDir(), "save-options.html");
 2    String savePath = Path.combine(getOutputDir(), "save-options-output.xps");
 3
 4    // Prepare HTML code and save it to a file
 5    String code = StringExtensions.concat("<h1>  XpsSaveOptions Class</h1>\r\n", 
 6                   "<p>Using XpsSaveOptions Class, you can programmatically apply a wide range of conversion parameters such as BackgroundColor, PageSetup, etc.</p>\r\n");
 7
 8    com.aspose.html.internal.ms.System.IO.File.writeAllText(documentPath, code);
 9
10    // Initialize an HTML document from the file
11    HTMLDocument document = new HTMLDocument(documentPath);
12    try {        }
13    finally { if (document != null) document.dispose(); }
14        
15    // Set up the page-size, margins and change the background color to AntiqueWhite
16     XpsSaveOptions options = new XpsSaveOptions();
17    com.aspose.html.drawing.Color.getAntiqueWhite().CloneTo(options.getBackgroundColor());
18    options.getPageSetup().setAnyPage(new Page(new com.aspose.html.drawing.Size(com.aspose.html.drawing.Length.fromInches(4.9f), com.aspose.html.drawing.Length.fromInches(3.5f))));
19
20    // Convert HTML to XPS
21    com.aspose.html.converters.Converter.convertHTML(document, options, savePath); 

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 “Banner HTML to XPS Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.