Convert SVG to XPS – C# Examples
Converting SVG documents to other formats is one of the main features of Aspose.SVG for .NET API. You can convert SVG to XPS and other popular formats in any way – online or programmatically. In this article, you find information on how to convert SVG to XPS by using ConvertSVG() methods of the Converter class, RenderTo() method of the SVGDocument class and applying XpsSaveOptions or XpsRenderingOptions.
Online SVG Converter
You can check the Aspose.SVG API functionality and convert SVG in real-time. Please load SVG from the local file system, select the output format and run the example. In the example, the save options are set by default. You will immediately receive the result as a separate file.
Convert SVG to XPS Using ConvertSVG() Method
The static methods of the Converter class can convert SVG to XPS with a single line of code. It is the easiest way for conversion. Converting a file to another format using ConvertSVG() methods is a sequence of operations among which document loading and saving:
- Load an SVG document using one of the SVGDocument() constructors ( owl.svg).
- Create an instance of the XpsSaveOptions class.
- Use the ConvertSVG() method to save SVG as an XPS file.
The following code snippet can be used to convert an SVG file to XPS format:
1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Converters;
4using System.Drawing;
5using Aspose.Svg.Saving;
6...
7
8 // Initialize an SVG document from a file
9 using (var document = new SVGDocument(Path.Combine(DataDir, "owl.svg")))
10 {
11 // Initialize an instance of the XpsSaveOptions class
12 var saveOptions = new XpsSaveOptions();
13 saveOptions.BackgroundColor = Color.Gray;
14
15 // Convert SVG to XPS
16 Converter.ConvertSVG(document, saveOptions, Path.Combine(OutputDir, "owl_out.xps"));
17 }
The
XpsSaveOptions() constructor initializes an instance of the XpsSaveOptions class that is passed to ConvertSVG() method. The
ConvertSVG() method takes the document
, saveOptions
, output file path and performs the conversion operation.
In the example above, we add the BackgroundColor
property that sets Color
, which will fill the background of every page.
You can download the complete examples and data files from GitHub. You find out about downloading from GitHub and running examples from the How to Run the Examples section.
Save Options
Aspose.SVG allows converting SVG to XPS with default or custom save options. XpsSaveOptions usage enables you to customize the rendering process; you can specify the page size, margins, background color, file permissions, Css, etc.
Property | Description |
---|---|
BackgroundColor | This property sets the color that will fill the background of every page. By default, this property is Transparent. |
Css | Gets a CssOptions object which is used for configuration of CSS properties processing. |
HorizontalResolution | Sets the horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
VerticalResolution | Sets the vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
PageSetup | This property gets a page setup object and uses it for configuration output page-set. |
Note: The options that are implementing with the XpsSaveOptions class are inheriting from the XpsRenderingOptions class.
Convert SVG to XPS Using RenderTo() Method
Consider SVG to XPS conversion scenario using RenderTo() method:
- Initialize a document using SVGDocument() constructor ( light.svg).
- Initialize an instance of the XpsRenderingOptions class and specify the document’s properties.
- Create a new instance of the XpsDevice class.
- Convert the document by RenderTo() method.
The following code snippet can be used to convert SVG to XPS:
1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Drawing;
4using Aspose.Svg.Rendering;
5using Aspose.Svg.Rendering.Xps;
6...
7
8 // Initialize an SVG document from a file
9 using (var document = new SVGDocument(Path.Combine(DataDir, "light.svg")))
10 {
11 // Initialize an instance of the XpsRenderingOptions class and set a custom PageSetup properties
12 var xpsRenderingOptions = new XpsRenderingOptions();
13 xpsRenderingOptions.PageSetup.AnyPage = new Page(new Drawing.Size(500, 500), new Margin(10, 10, 10, 10));
14
15 // Initialize an instance of XpsDevice class
16 using (IDevice device = new XpsDevice(xpsRenderingOptions, Path.Combine(OutputDir, "light_out.xps")))
17 {
18 // Render SVG to XPS, send the document to the rendering device
19 document.RenderTo(device);
20 }
21 }
The
XpsRenderingOptions() constructor creates a new object that is passed as an argument to the
XpsDevice(options, file
) constructor. The last initializes a new instance of
XpsDevice class by rendering options and output file name. The
RenderTo(device
) method converts and sends the current document to the output rendering device.
You can try our free online SVG to XPS Converter that converts SVG to XPS file with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!