Convert SVG to XPS in C#

To convert SVG to XPS in C#, load the SVG document or prepare SVG content, create XpsSaveOptions, and call Converter.ConvertSVG() with the source, options, and output path or stream provider.

XPS is a fixed-layout document format used in Windows-oriented viewing, printing, and document pipelines. SVG to XPS conversion is useful when vector graphics must be rendered into an XPS document with controlled page setup, background color, and resolution.

Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.

Convert SVG to XPS with One Line of C#

The static Converter class provides a compact way to render SVG content to XPS. The example below converts a local SVG file to XPS with default XpsSaveOptions.

1// Convert SVG to XPS using C#
2
3// Invoke the ConvertSVG() method to convert SVG to XPS
4Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new XpsSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.xps"));

Convert SVG to XPS in C#

Use this workflow when SVG content is created in code and then saved as an XPS file.

The workflow is:

  1. Prepare the SVG content and base URI.
  2. Create an XpsSaveOptions instance.
  3. Call Converter.ConvertSVG(content, baseUri, options, outputPath).
  4. Save the generated XPS file to the target output path.
 1// Convert SVG to XPS in C#
 2
 3// Prepare SVG code
 4string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
 5              "<circle cx ='100' cy ='100' r ='60' fill='none' stroke='red' stroke-width='10' />" +
 6              "</svg>";
 7
 8// Prepare a path for converted file saving
 9string savePath = Path.Combine(OutputDir, "circle.xps");
10
11// Initialize XpsSaveOptions
12XpsSaveOptions options = new XpsSaveOptions();
13
14// Convert SVG to XPS
15Converter.ConvertSVG(code, ".", options, savePath);

Customize SVG to XPS with XpsSaveOptions

Use XpsSaveOptions when the generated XPS file needs custom rendering settings.

To customize SVG to XPS conversion:

  1. Load the SVG file as an SVGDocument.
  2. Create XpsSaveOptions.
  3. Configure only the settings required by the output, such as PageSetup, BackgroundColor, CSS processing, or resolution.
  4. Pass the document, options, and output path to Converter.ConvertSVG().
OptionUse it to control
PageSetupPage size, margins, and page layout settings.
CssCSS processing behavior through CssOptions.
BackgroundColorThe color used to fill the page background. The default value is transparent.
HorizontalResolution and VerticalResolutionOutput resolution values used by the rendering process. The default value is 300 dpi for each property.
 1// Convert SVG to XPS in C# with custom page settings
 2
 3// Prepare a path to a source SVG file
 4string documentPath = Path.Combine(DataDir, "aspose.svg");
 5
 6// Prepare a path for converted file saving
 7string savePath = Path.Combine(OutputDir, "aspose-options.xps");
 8
 9// Initialize an SVG document from the file
10using SVGDocument document = new SVGDocument(documentPath);
11
12// Initialize XpsSaveOptions. Set up the page-size 500x500 pixels, margins, resolutions and change the background color to AliceBlue
13XpsSaveOptions options = new XpsSaveOptions()
14{
15    HorizontalResolution = 200,
16    VerticalResolution = 200,
17    BackgroundColor = System.Drawing.Color.AliceBlue
18};
19options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 10, 10, 10));
20
21// Convert SVG to XPS
22Converter.ConvertSVG(document, options, savePath);

The image below illustrates the XPS rendering result with custom page size and background settings.

Aspose logo rendered with XPS options

When to Use SVG to XPS Conversion

Use XPS output when a C# application needs a fixed-layout document for Windows-oriented viewing, printing, or internal document pipelines. If the result must be widely shared outside an XPS-aware environment, SVG to PDF is usually the more practical choice.

Related XPS Workflows

Save SVG to XPS with an Output Stream Provider

Use ICreateStreamProvider when the conversion output should be written through streams instead of only to a file path. This is useful for memory-based workflows, remote storage, databases, and services that decide where the rendered output is stored.

 1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
 2
 3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
 4{
 5    // List of MemoryStream objects created during the document rendering
 6    public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
 7
 8    public Stream GetStream(string name, string extension)
 9    {
10        // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11        MemoryStream result = new MemoryStream();
12        Streams.Add(result);
13        return result;
14    }
15
16    public Stream GetStream(string name, string extension, int page)
17    {
18        // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19        MemoryStream result = new MemoryStream();
20        Streams.Add(result);
21        return result;
22    }
23
24    public void ReleaseStream(Stream stream)
25    {
26        // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27    }
28
29    public void Dispose()
30    {
31        // Releasing resources
32        foreach (MemoryStream stream in Streams)
33            stream.Dispose();
34    }
35}

The following example converts SVG to XPS with MemoryStreamProvider.

 1// Convert SVG to XPS in C# using memory stream
 2
 3// Create an instance of MemoryStreamProvider
 4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
 5
 6// Prepare SVG code
 7string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
 8              "<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />" +
 9              "</svg>";
10
11// Convert SVG to XPS using the MemoryStreamProvider
12Converter.ConvertSVG(code, ".", new XpsSaveOptions(), streamProvider);
13
14// Get access to the memory stream that contains the result data
15MemoryStream memory = streamProvider.Streams.First();
16memory.Seek(0, SeekOrigin.Begin);
17
18// Flush the result data to the output file
19using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.xps")))
20{
21    memory.CopyTo(fs);
22}

Related SVG Conversion Guides

Other Platforms

Try Online SVG to XPS Conversion

                
            

Use the online SVG to XPS Converter for quick manual conversion. Use Aspose.HTML for .NET when SVG to XPS conversion must run inside a C# application, service, or document pipeline.

SVG to XPS Converter