How to Convert PDF and Word DOCX to SVG in C# .NET

This is the final blog in the series of articles related to programmatically working with SVG images using the GcDocuments library for C# .NET. In the previous articles, we saw the creation and manipulation of SVG images using C# .NET.  

This blog discusses how to programmatically convert popular document formats to scalable vector images with C# .NET. It will cover:


Your organization has been hired to take a significant amount of existing marketing documents and make them available as SVG images. The documents are mostly PDF or Word files with complex layouts and graphics. The client does not want to compromise on the images' content, layout, formatting, etc. Usually, converting the document to SVG image piece by piece would be time-consuming. But with GcDocs APIs, these documents are converted easily and readily as a single image.  

Let's learn how to convert PDF and Word files quickly and easily to an SVG format.

Convert PDF to SVG

To convert a pdf file to SVG, first load the pdf file (download the sample PDF document here!) using GcPdf libraries into the application. Next, extract the pdf page(s) you want to convert. We chose the first page that appears as below:

Commercial Tenant App

Then save it to SVG format using the SaveAsSvg method from GrapeCity.Documents.Pdf.Page class as depicted in the code below:

using GrapeCity.Documents.Pdf;
var pdf = new GcPdfDocument();
using (var fs = new FileStream(@"Form.pdf", FileMode.Open))
    var pdfPage = pdf.Pages[0]; 

The resultant SVG image looks as in the image below:

SVG Result

Convert Word to SVG

To convert a word file to SVG, first load the word file (download the sample Word document here!) using GcWord libraries into the application. Then get the word document page(s) using the Pages property of the GcWordLayout class from the GrapeCity.Document.Word.Layout assembly. We chose the first page that appears as below:

Word to SVG

Finally, convert and save the page to SVG format using the SaveAsSvg method from the Page class as depicted in the code below:

using GrapeCity.Documents.Word;
using GrapeCity.Documents.Word.Layout;
var word = new GcWordDocument();
using (var layout = new GcWordLayout(word))
    var wordPage = layout.Pages[0];

The resultant SVG image appears as in the image below:

SVG Result

Convert to SVGZ

SVGZ is the compressed format of SVG. Graphic design professionals widely use this format to transfer and share SVG images over the web. It reduces the size of SVG files by nearly 50-80%, saving space and time for on-the-fly compression while sharing the images online.

GcDocs introduces the ToSvgz method with the GcPDF and GcWord document APIs that can quickly generate a compressed SVG image. This method generates a byte array with compressed data from these documents that can be written to a .svgz file as depicted in the code below:

File.WriteAllBytes("pdfToSvgz.svgz", pdfPage.ToSvgz()); 
File.WriteAllBytes("wordToSvgz.svgz", wordPage.ToSvgz());

Configure Save Options

Both SaveAsSvg and ToSvgz methods come with multiple overloads that you can use to customize your image while converting from PDF or Word documents. It includes the following options:

SaveAsImageOptions for PDF

Use the save options from SaveAsImageOptions Class to configure the various image settings such as Zoom, Resolution, BackColor, etc. while converting the PDF file to an SVG image, as shown in the following example:

    new SaveAsImageOptions() { Zoom = 0.5f, BackColor = System.Drawing.Color.AliceBlue},

The image below shows the difference in the SVG images converted with and without the above save options:

With or Without Save Option

ViewState for PDF 

The ViewState class defines the environment for the scope of operations performed on a PDF document with layers, for example, print, view, or export specific pdf pages. You can use this option, for example, to convert only a specific layer in the pdf document to SVG as in the code below: 

var view = new GrapeCity.Documents.Pdf.Layers.ViewState(pdf);
view.SetLayersUIState(true, "lighting_plan.pdf");
view.SetLayersUIStateExcept(false, "lighting_plan.pdf");
pdfPage.SaveAsSvg("pdfToSvg_view.svg", view, null, null);

The original pdf and the converted SVG image with only a selected layer appear as follows:

ViewState for PDF

ImageOutputSettings for Word

Use the save options from GrapeCity.Documents.Word.Layout.ImageOutputSettings class to configure the various image settings such as Zoom, Resolution, BackColor, etc. while converting the Word file to an SVG image, as shown in the following example:

    new ImageOutputSettings() { Zoom = 0.5f, BackColor = System.Drawing.Color.AliceBlue },


You can use System.Xml.XmlWriterSettings class to manipulate the XML structure of the resultant SVG image. For example, generate an XML with indented tags (or elements) using the following code:

    new XmlWriterSettings() { Indent = true }

See the difference in the XML of the SVGs with and without indent in the image below:

XML With or Without Indent

Be sure to download the complete sample here! By running the sample, you will better understand how to utilize the GcDocuments APIs to help with your SVG image conversions.

You can also convert other GcDocuments such as GcExcel or Image files such as JPEG, PNG, etc., to SVG formats. However, the process is slightly different from pdf or word conversion. We will cover them in separate blogs.  

comments powered by Disqus