How to Merge SVG Documents - 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:
using Aspose.Svg;
using System.IO;
using Aspose.Svg.Rendering;
using Aspose.Svg.Rendering.Image;
...
// Initialize an SVG document from a file
using (var document = new SVGDocument(Path.Combine(DataDir, "owl.svg")))
{
// Create an instance of SvgRenderer
using (var renderer = new SvgRenderer())
{
// Create an instance of ImageDevice
using (var device = new ImageDevice(Path.Combine(OutputDir, "owl.png");))
{
// Render SVG to PNG
renderer.Render(device, document);
}
}
}
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 ( owl.svg , conclusion.svg , lineto.svg ) can be done with a few lines of code:
using Aspose.Svg;
using System.IO;
using Aspose.Svg.Rendering;
using Aspose.Svg.Rendering.Pdf;
...
// Initialize SVG documents from files to merge later
using (var document1 = new SVGDocument(Path.Combine(DataDir, "owl.svg")))
using (var document2 = new SVGDocument(Path.Combine(DataDir, "conclusion.svg")))
using (var document3 = new SVGDocument(Path.Combine(DataDir, "lineto.svg")))
{
// Create an instance of SvgRenderer
using (var renderer = new SvgRenderer())
{
// Create an instance of PdfDevice
using (var device = new PdfDevice(Path.Combine(OutputDir, "result.pdf")))
{
// Merge all SVG documents to PDF
renderer.Render(device, document1, document2, document3);
}
}
}
You can merge SVG files to PDF, XPS, JPEG, JPG, BMP, PNG, TIFF and GIF formats.