Convert SVG to PDF in C#
This article explains how to convert SVG image files to PDF.
You’ll learn how to convert the image to a PDF document without optical character recognition (OCR). This means that the text in the image isn’t recognized, and you can’t search in the output PDF file. For more information, refer to the guide on converting images to searchable PDFs.
For more information on using mixed raster content (MRC) compression during the conversion, which can reduce file sizes by up to 99 percent in some cases, refer to the guide on hypercompression.
Creating a PDF from an SVG file
To create a PDF from an SVG file, follow the steps below:
- Create a
GdPictureDocumentConverterobject. - Load the source document by passing its path to the
LoadFromFilemethod. Recommended: Specify the source document format with a member of theDocumentFormatenumeration. - Save the output in a new PDF document with the
SaveAsPDFmethod.
The example below creates a PDF document from an SVG file:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();// Load the source document.GdPictureStatus status = gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.svg", GdPicture14.DocumentFormat.DocumentFormatSVG);if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
// Save the output in a new PDF document.status = gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}Using gdpictureDocumentConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter() ' Load the source document. Dim status As GdPictureStatus = gdpictureDocumentConverter.LoadFromFile("C:\temp\source.svg", GdPicture14.DocumentFormat.DocumentFormatSVG) If status <> GdPictureStatus.OK Then Throw New Exception(status.ToString()) End If
' Save the output in a new PDF document. status = gdpictureDocumentConverter.SaveAsPDF("C:\temp\output.pdf") If status <> GdPictureStatus.OK Then Throw New Exception(status.ToString()) End IfEnd UsingOptional PDF configuration properties
Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter object:
PdfBitonalImageCompressionis a member of thePdfCompressionenumeration that specifies the compression scheme used for bitonal images in the output PDF file.PdfColorImageCompressionis a member of thePdfCompressionenumeration that specifies the compression scheme used for color images in the output PDF file.PdfEnableColorDetectionis a Boolean value that specifies whether to use automatic color detection during the conversion that preserves image quality and reduces the output file size.PdfEnableLinearizationis a Boolean value that specifies whether to linearize the output PDF to enable Fast Web View mode.PdfImageQualityis an integer from 0 to 100 that specifies the image quality in the output PDF file.RasterizationDPIis an integer that specifies the resolution, expressed in dots per inch (DPI), with which to convert the source vector image to a raster image in the output PDF file.
The example below creates a PDF document from an SVG file with a custom configuration:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();// Load the source document.GdPictureStatus status = gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.svg", GdPicture14.DocumentFormat.DocumentFormatSVG);if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
// Configure the conversion.gdpictureDocumentConverter.PdfColorImageCompression = PdfCompression.PdfCompressionJPEG;gdpictureDocumentConverter.PdfImageQuality = 50;gdpictureDocumentConverter.RasterizationDPI = 300;// Save the output in a new PDF document.status = gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}Using gdpictureDocumentConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter() ' Load the source document. Dim status As GdPictureStatus = gdpictureDocumentConverter.LoadFromFile("C:\temp\source.svg", GdPicture14.DocumentFormat.DocumentFormatSVG) If status <> GdPictureStatus.OK Then Throw New Exception(status.ToString()) End If
' Configure the conversion. gdpictureDocumentConverter.PdfColorImageCompression = PdfCompression.PdfCompressionJPEG gdpictureDocumentConverter.PdfImageQuality = 50 gdpictureDocumentConverter.RasterizationDPI = 300 ' Save the output in a new PDF document. status = gdpictureDocumentConverter.SaveAsPDF("C:\temp\output.pdf") If status <> GdPictureStatus.OK Then Throw New Exception(status.ToString()) End IfEnd Using