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 perform 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;
5...
6
7 // Initialize an SVG document from a file
8 using (var document = new SVGDocument(Path.Combine(DataDir, "owl.svg")))
9 {
10 // Create an instance of SvgRenderer
11 using (var renderer = new SvgRenderer())
12 {
13 // Create an instance of ImageDevice
14 using (var device = new ImageDevice(Path.Combine(OutputDir, "owl.png");))
15 {
16 // Render SVG to PNG
17 renderer.Render(device, document);
18 }
19 }
20 }
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;
5...
6
7 // Initialize SVG documents from files to merge later
8 using (var document1 = new SVGDocument(Path.Combine(DataDir, "circle.svg")))
9 using (var document2 = new SVGDocument(Path.Combine(DataDir, "flower.svg")))
10 using (var document3 = new SVGDocument(Path.Combine(DataDir, "lineto.svg")))
11 {
12 // Create an instance of SvgRenderer
13 using (var renderer = new SvgRenderer())
14 {
15 // Create an instance of PdfDevice
16 using (var device = new PdfDevice(Path.Combine(OutputDir, "result.pdf")))
17 {
18 // Merge all SVG documents to PDF
19 renderer.Render(device, document1, document2, document3);
20 }
21 }
22 }
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!