How to Merge SVG Files – C# Examples

Aspose.SVG for .NET API provides the Renderer class for rendering and merging SVG documents. The article provides information about the Render() method used to convert SVG documents to another format and merge them into a single file. You will learn how to merge multiple SVG documents to PDF, XPS or Image formats and find C# examples of SVG merging.

Rendering SVG documents

Rendering of SVG files is the process of generating images from a 2D model by means of the API. Converting from SVG to other formats can be performed by using Render(IDevice, TDocument) method of the Renderer class.

In the How to Convert SVG Files section, we consider two conversion scenarios: using ConvertSVG() methods and RenderTo() method. This article considers the Render() method applying for SVG conversion to other formats – PDF, XPS, JPG, JPG, BMP, PNG, TIFF and GIF.

To convert SVG to another format, for example, PNG, use the following code snippet:

1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Rendering;
4using Aspose.Svg.Rendering.Image;
 1// Render SVG to PNG using C#
 2
 3// Initialize an SVG document from a file
 4using (SVGDocument document = new SVGDocument(Path.Combine(DataDir, "owl.svg")))
 5{
 6    // Create an instance of SvgRenderer
 7    using (SvgRenderer renderer = new SvgRenderer())
 8    {
 9        // Create an instance of ImageDevice
10        using (ImageDevice device = new ImageDevice(Path.Combine(OutputDir, "owl.png")))
11        {
12            // Render SVG to PNG
13            renderer.Render(device, document);
14        }
15    }
16}

Merging SVG documents

The Render() method gives you the ability to send multiple documents at once to the output rendering device and merge them. Documents merging can be done with a few lines of code:

1using Aspose.Svg;
2using System.IO;
3using Aspose.Svg.Rendering;
4using Aspose.Svg.Rendering.Pdf;
 1// Merge SVGs to PDF using C#
 2
 3// Initialize SVG documents from files to merge later
 4using (SVGDocument document1 = new SVGDocument(Path.Combine(DataDir, "circle.svg")))
 5using (SVGDocument document2 = new SVGDocument(Path.Combine(DataDir, "flower.svg")))
 6using (SVGDocument document3 = new SVGDocument(Path.Combine(DataDir, "lineto.svg")))
 7{
 8    // Create an instance of SvgRenderer
 9    using (SvgRenderer renderer = new SvgRenderer())
10    {
11        // Create an instance of PdfDevice
12        using (PdfDevice device = new PdfDevice(Path.Combine(OutputDir, "result.pdf")))
13        {
14            // Merge all SVG documents to PDF
15            renderer.Render(device, document1, document2, document3);
16        }
17    }
18}

You can merge SVG files to PDF, XPS, JPEG, JPG, BMP, PNG, TIFF and GIF formats.

You can download the complete examples and data files from GitHub. About downloading from GitHub and running examples, you find out from the How to Run the Examples section.

You can try to merge SVG files to a variety of other image formats with our free online Image Merger. The application allows you to freely add any images, rotate, scale, add backgrounds, filters and move each element. You can use a set of intuitive combining options that allow you to flexibly control the creation of an image collage and achieve the desired result. With the Image Merger app, you will make image collages easily within minutes. Try our forceful application for free now!

Text “Image Merger”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.