DEV Community

IDRSolutions
IDRSolutions

Posted on

How to convert PDF to SVG in C# (Tutorial)

Introduction

This tutorial explains how to convert PDF files to SVG in C# using the hosted BuildVu Cloud API, including examples such as:

  • The IDRsolutions trial version or cloud subscription
  • A self-hosted BuildVu microservice instance

Although the above services can be accessed via standard HTTP requests, this tutorial uses our open-source C# IDRCloudClient, which offers an easy-to-use C# wrapper for the REST API.

Prerequisites

Install the idrsolutions-csharp-client package via NuGet using the following command:

nuget install idrsolutions-csharp-client
Enter fullscreen mode Exit fullscreen mode

Code Example

Below is a simple code example demonstrating how to convert PDF files to SVG. Additional configuration options and advanced features are described further down.

using System;
using System.Collections.Generic;
using idrsolutions_csharp_client;

class ExampleUsage
{
    static void Main(string[] args)
    {

        var client = new IDRCloudClient("https://cloud.idrsolutions.com/cloud/" + IDRCloudClient.BUILDVU);

        try
        {
            Dictionary parameters = new Dictionary
            { 
                //["token"] = "Token", //Required only when connecting to the IDRsolutions trial and cloud subscription service
                ["input"] = IDRCloudClient.UPLOAD,
                ["file"] = "path/to/input.pdf"
            };

            Dictionary results = client.Convert(parameters);

            String outputUrl = results.GetValueOrDefault("downloadUrl", "No download URL provided");

            client.DownloadResult(results, "path/to/output/dir");

            Console.WriteLine("Converted: " + outputUrl);
        }
        catch (Exception e)
        {
            Console.WriteLine("Conversion failed: " + e.Message);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Return result to a callback URL

The BuildVu Microservice can accept a callback URL to report the status of a conversion once it is complete. Using a callback URL eliminates the need to poll the service to check when the conversion has finished. You can provide the callback URL to the convert method as shown below.

Dictionary parameters = new Dictionary
{ 
    //["token"] = "Token", //Required only when connecting to the IDRsolutions trial and cloud subscription service
    ["callbackUrl"] = "http://listener.url",
    ["input"] = IDRCloudClient.UPLOAD,
    ["file"] = "path/to/input.pdf"
};

Enter fullscreen mode Exit fullscreen mode

Configuration Options

The BuildVu API accepts a stringified JSON object with key-value pair configuration options to customize your conversion. These settings should be included in the parameters array. A complete list of configuration options for converting PDF files to SVG is available here.

["settings"] = "{\"key\":\"value\",\"key\":\"value\"}"
Enter fullscreen mode Exit fullscreen mode

Upload by URL

In addition to uploading a local file, you can provide a URL for the BuildVu Microservice to download and convert. To do this, replace the input and file values in the parameters variable with the following.

["input"] = IDRCloudClient.DOWNLOAD
["url"] = "http://exampleURL/exampleFile.pdf"
Enter fullscreen mode Exit fullscreen mode

Using Authentication

If your self-hosted BuildVu Microservice requires a username and password to convert PDF files to SVG, you must include them with each conversion request. These credentials are passed as username and password variables to the convert method, as shown below.

var client = new IDRCloudClient("http://exampleURL.com/" + IDRCloudClient.BUILDVU, "username", "password");
Enter fullscreen mode Exit fullscreen mode

Top comments (0)