Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert SVG to PDF in C#, load the SVG document or open the SVG file as a stream, create PdfSaveOptions, and call Converter.ConvertSVG() with the source, options, and output path or stream provider.
SVG is a scalable vector format for graphics, diagrams, icons, and illustrations. PDF is useful when SVG content must become a fixed-layout document for sharing, printing, archiving, or automated document workflows.
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.
The static
Converter class provides a compact way to render SVG content to PDF. The example below converts SVG to PDF with default PdfSaveOptions.
1// Convert SVG to PDF in C#
2
3// Invoke the ConvertSVG() method for SVG to PDF conversion
4Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new PdfSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.pdf"));Use this workflow when the SVG source exists on disk and the PDF should be saved to a file path.
The workflow is:
SVGDocument.PdfSaveOptions instance.Converter.ConvertSVG(source, options, savePath). 1// Convert SVG to PDF using C#
2
3// Prepare SVG code
4string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
5 "<circle cx ='100' cy ='100' r ='50' fill='none' stroke='red' stroke-width='5' />" +
6 "</svg>";
7
8// Prepare a path for converted file saving
9string savePath = Path.Combine(OutputDir, "circle.pdf");
10
11// Initialize PdfSaveOptions
12PdfSaveOptions options = new PdfSaveOptions();
13
14// Convert SVG to PDF
15Converter.ConvertSVG(code, ".", options, savePath);Use PdfSaveOptions when the generated PDF needs custom rendering settings. To customize SVG to PDF conversion:
PdfSaveOptions.PageSetup, JpegQuality, BackgroundColor, resolution, or Encryption.Converter.ConvertSVG().| Option | Use it to control |
|---|---|
PageSetup | Page size, margins, and page layout settings. |
JpegQuality | JPEG image compression quality in the PDF output. The default value is 95. |
Css | CSS processing behavior through CssOptions. |
BackgroundColor | The color used to fill the page background. The default value is transparent. |
HorizontalResolution and VerticalResolution | Output resolution values used by the rendering process. The default value is 300 dpi for each property. |
Encryption | PDF encryption settings. If not configured, the PDF is created without encryption. |
1// Convert SVG to PDF 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.pdf");
8
9// Initialize an SVG document from the file
10using SVGDocument document = new SVGDocument(documentPath);
11
12// Initialize PdfSaveOptions. Set up the page-size, margins, resolutions, JpegQuality, and change the background color to AliceBlue
13PdfSaveOptions options = new PdfSaveOptions()
14{
15 HorizontalResolution = 200,
16 VerticalResolution = 200,
17 BackgroundColor = System.Drawing.Color.AliceBlue,
18 JpegQuality = 100
19};
20options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 10, 10, 10));
21
22// Convert SVG to PDF
23Converter.ConvertSVG(document, options, savePath);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 PDF with MemoryStreamProvider.
1// Convert SVG to PDF 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 PDF using the MemoryStreamProvider
12Converter.ConvertSVG(code, ".", new PdfSaveOptions(), 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.pdf")))
20{
21 memory.CopyTo(fs);
22}Use the online SVG to PDF Converter for quick manual conversion. Use Aspose.HTML for .NET when SVG to PDF conversion must run inside a C# application, service, or document pipeline.
You can evaluate Aspose.HTML for .NET before purchasing a license, but converted documents created in evaluation mode contain watermarks and are limited to four pages. For unrestricted testing, request a free 30-day Temporary License. For occasional manual conversions, use the free online SVG to PDF Converter.
See Licensing Aspose.HTML for .NET for evaluation limitations and license setup.
The SVG viewport and the PDF page are configured independently. Check the source width, height, and viewBox, then use PdfSaveOptions.PageSetup to set the PDF page size and margins. Matching these settings prevents unintended clipping or excessive whitespace.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.