How to Convert PDF and Word DOCX to SVG in C# .NET
Tags: webdev, dotnet, csharp, tutorial
Published: 2022-06-03 20:54:51

---
canonical_url: https://www.grapecity.com/blogs/how-to-convert-pdf-and-word-docx-to-svg-in-c-sharp-net
description: Learn how to convert PDF and Word DOCX to SVG in C# .NET. See more from GrapeCity Documents today.
---

This is the final blog in the series of articles related to programmatically working with SVG images using the [GcDocuments library for C# .NET](https://www.grapecity.com/documents-api). In the previous articles, we saw the [creation](https://www.grapecity.com/blogs/how-to-create-svg-files-programmatically-in-c-sharp-net) and [manipulation](https://www.grapecity.com/blogs/how-to-edit-svg-images-programmatically-in-c-sharp-net) 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:

*   [conversion of PDF to SVG format](#pdf)
*   [conversion of Word to SVG format](#word)
*   [configure save options](#save) 
*   [conversion to SVGZ format](#svgz)

### Use-Case

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.

### <a id="pdf"></a>Convert PDF to SVG

To convert a pdf file to SVG, first [load the pdf file](https://www.grapecity.com/documents-api-pdf/docs/online/quickstart.html#stp1) (download the sample PDF document [here](https://files.grapecity.com/gc-website-media/ekzn4hnx/form.pdf)!) using [GcPdf libraries](https://www.grapecity.com/documents-api-pdf/docs/online/overview.html) into the application. Next, extract the [pdf page(s)](https://www.grapecity.com/documents-api-pdf/docs/online/Pages.html) you want to convert. We chose the first page that appears as below:

![Commercial Tenant App](https://files.grapecity.com/gc-website-media/pcnhzj4d/01-pdf-to-svg.png?width=439.81481481481484&height=500)

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))
    {
        pdf.Load(fs);
        var pdfPage = pdf.Pages[0]; 
        pdfPage.SaveAsSvg("pdfToSvg.svg");
    }
```

The resultant SVG image looks as in the image below:

![SVG Result](https://files.grapecity.com/gc-website-media/vdhlbhe1/02-svg-result.png?width=384.0725806451613&height=500)

### <a id="word"></a>Convert Word to SVG

To convert a word file to SVG, first [load the word file](https://www.grapecity.com/documents-api-word/docs/online/quickstart.html#stp1) (download the sample Word document [here](https://files.grapecity.com/gc-website-media/2fwcwtl1/testresult.docx)!) using [GcWord libraries](https://www.grapecity.com/documents-api-word/docs/online/overview.html) into the application. Then get the word document page(s) using the [Pages](https://www.grapecity.com/documents-api-word/docs/online/GrapeCity.Documents.Word.Layout~GrapeCity.Documents.Word.Layout.GcWordLayout~Pages.html) property of [the GcWordLayout](https://www.grapecity.com/documents-api-word/docs/online/GrapeCity.Documents.Word.Layout~GrapeCity.Documents.Word.Layout.GcWordLayout.html) class from the [GrapeCity.Document.Word.Layout](https://www.grapecity.com/documents-api-word/docs/online/GrapeCity.Documents.Word.Layout.html) assembly. We chose the first page that appears as below:

![Word to SVG](https://files.grapecity.com/gc-website-media/egynuqn2/03-word-to-svg.png?width=483.5106382978724&height=500)

Finally, convert and save the page to SVG format using the [SaveAsSvg](https://www.grapecity.com/documents-api-word/docs/online/GrapeCity.Documents.Word.Layout~GrapeCity.Documents.Word.Layout.Page~SaveAsSvg.html) method from the [Page](https://www.grapecity.com/documents-api-word/docs/online/GrapeCity.Documents.Word.Layout~GrapeCity.Documents.Word.Layout.Page.html) class as depicted in the code below:

```
    using GrapeCity.Documents.Word;
    using GrapeCity.Documents.Word.Layout;

    ....

    var word = new GcWordDocument();
    word.Load(@"TestResult.docx");
    using (var layout = new GcWordLayout(word))
    {
        var wordPage = layout.Pages[0];
        wordPage.SaveAsSvg("wordToSvg.svg");
    }
```

The resultant SVG image appears as in the image below:

![SVG Result](https://files.grapecity.com/gc-website-media/egqmcvmq/04-svg-result.png?width=457.7822990844354&height=500)

### <a id="svgz"></a>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());
```

### <a id="save"></a>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 <u>[SaveAsImageOptions](https://www.grapecity.com/documents-api-pdf/docs/online/GrapeCity.Documents.Pdf~GrapeCity.Documents.Pdf.SaveAsImageOptions.html)</u> 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:

```
    pdfPage.SaveAsSvg(
        "pdfToSvg_saveOptions.svg", 
        null, 
        new SaveAsImageOptions() { Zoom = 0.5f, BackColor = System.Drawing.Color.AliceBlue},
        null
    );
```

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

![With or Without Save Option](https://files.grapecity.com/gc-website-media/cqzfh2nm/05-with-or-without-save-option.png?width=500&height=439.60036330608534)

#### ViewState for PDF 

The [ViewState](https://www.grapecity.com/documents-api-pdf/docs/online/GrapeCity.Documents.Pdf~GrapeCity.Documents.Pdf.Layers.ViewState.html) 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](https://files.grapecity.com/gc-website-media/gpsgqxq3/06-viewstate-pdf.png?width=500&height=411.1111111111111)

#### ImageOutputSettings for Word

Use the save options from [GrapeCity.Documents.Word.Layout.ImageOutputSettings](https://www.grapecity.com/documents-api-word/docs/online/GrapeCity.Documents.Word.Layout~GrapeCity.Documents.Word.Layout.ImageOutputSettings.html) 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:

```
    wordPage.SaveAsSvg(
        "wordToSvgz_saveOptions.svg",                    
        new ImageOutputSettings() { Zoom = 0.5f, BackColor = System.Drawing.Color.AliceBlue },
        null
    );
```

#### XmlWriterSettings

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:

```
    wordPage.SaveAsSvg(
        "wordToSvg_XML.svg",
        null,
        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](https://files.grapecity.com/gc-website-media/o4vln3is/07-xml-with-or-without-indent.png?width=500&height=330.2034428794992)

Be sure to download the complete sample [here](https://files.grapecity.com/gc-website-media/5uamr4wn/convert_to_svg.zip)! By running the sample, you will better understand how to utilize the GcDocuments APIs to help with your SVG image conversions.

_Note:_  
_You can also convert other GcDocuments such as [GcExcel](https://www.grapecity.com/documents-api-excel/docs/online/overview.html) 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. _