Convert SVG to XPS Using Java

Why convert SVG files to XPS format? You may want to create high-quality, vector-preserving, print-ready documents or ensure file compatibility with Microsoft applications that use the XPS format as their standard document format. In addition, by converting SVG to XPS, you can protect files with a password and digital signature, providing additional security features for sensitive documents.

In this article, you will find information on how to convert an SVG to XPS and how to use XpsSaveOptions and MemoryStreamProvider parameters.

SVG to XPS by a single line of code

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

1    // Invoke the convertSVG method() to convert SVG to XPS
2    com.aspose.html.converters.Converter.convertSVG(Path.combine(getDataDir(), "aspose.svg"), new XpsSaveOptions(), Path.combine(getOutputDir(), "aspose.xps"));   

Convert SVG to XPS in Java

If your scenario is required rendering an SVG document, for instance, to XPS file format, the following example demonstrates how that is simple:

  1. Load an SVG file using SVGDocument class. In the following example we create SVG content from scratch.
  2. Create a new XpsSaveOptions object. Use the empty XpsSaveOptions() constructor to convert with the default save options.
  3. Use one of the сonvertSVG() methods of the Converter class to save SVG as an XPS file. In the example we use the convertHTML(content, baseUri, options, savePath) method that takes four parameters: string with SVG code to be converted, the base folder for the input SVG file, an instance of the XpsSaveOptions class, and the output file path where the converted file will be saved.

Please review the following Java code snippet, which shows the SVG to XPS conversion process with step-by-step instructions:

 1    // Prepare SVG code 
 2    String code = StringExtensions.concat("<svg xmlns='http://www.w3.org/2000/svg'>", 
 3                "<circle cx ='100' cy ='100' r ='60' fill='none' stroke='red' stroke-width='10' />",
 4                "</svg>");
 5
 6    // Prepare a path for converted file saving 
 7    String savePath = Path.combine(getOutputDir(), "circle.xps");
 8
 9    // Initialize an instance of the XpsSaveOptions class
10    XpsSaveOptions options = new XpsSaveOptions();
11
12    // Convert SVG to XPS
13    com.aspose.html.converters.Converter.convertSVG(code, ".", options, savePath);

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.

Convert SVG to XPS using XpsSaveOptions

To convert SVG to XPS with XpsSaveOptions specifying, you should follow a few steps:

  1. Load an SVG file using one of the SVGDocument() constructors of the SVGDocument class.
  2. Create a new XpsSaveOptions object and specify the required properties. The XpsSaveOptions() constructor initializes an instance of the XpsSaveOptions class that is passed to converSVG() method:
    • Use the setJpegQuality() method to specify JPEG compression quality.
    • Use the setBackgroundColor() method to set the color that fills the background.
    • Use the setHorizontalResolution() and setVerticalResolution() methods to set horizontal and vertical resolutions for output XPS files.
    • Use the setPageSetup() method to specify the page size and margins for the output document.
  3. Call the сonvertSVG(document, options, savePath) method of the Converter class. The method takes the document, options, output file path savePath and performs the conversion operation.

The following example shows how to use XpsSaveOptions and create an XPS file with custom page size, margins, and resolutions:

 1    // Prepare a path to a source SVG file
 2    String documentPath = Path.combine(getDataDir(), "aspose.svg");
 3
 4    // Prepare a path for converted file saving 
 5    String savePath = Path.combine(getOutputDir(), "aspose-options.xps");
 6
 7    // Initialize an SVG document from the file
 8    SVGDocument document = new SVGDocument(documentPath);
 9    try {        }
10    finally { if (document != null) document.dispose(); }
11
12    // Initialize XpsSaveOptions. Set up the page size 500x500 pixels, margins, and resolutions
13    XpsSaveOptions options = new XpsSaveOptions();
14    options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
15    options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
16    options.getPageSetup().setAnyPage(new Page(new com.aspose.html.drawing.Size(500, 500), new Margin(30, 10, 10, 10)));
17
18    // Convert SVG to XPS
19    com.aspose.html.converters.Converter.convertSVG(document, options, savePath);

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

Aspose.HTML offers a free online SVG to XPS Converter that converts SVG to XPS with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.