C# PDF API — Getting Started

With Nutrient’s C# PDF API, you can quickly process your documents by making HTTP requests to one of our 50+ API tools. You can make a single request to one tool or combine API actions to generate, edit, OCR, and convert your document (1 document can have multiple API actions).

In this guide, we’ll go through how you can use C# to make HTTP requests with our API by:

  1. 1
    Creating a new project
  2. 2
    Preparing the request
  3. 3
    Executing the request

You’ll use RestSharp to make HTTP requests.

Creating a New Project

You’ll need a .NET project. The easiest way is to use the .NET CLI tool that ships with the .NET SDK and create a new project based on the Console Application template:

Shell
dotnet new console --name PspdfkitApiDemo

Then, switch to the newly created project directory:

Shell
cd PspdfkitApiDemo

Now you can add a RestSharp dependency:

Shell
dotnet add package RestSharp

Finally, you’ll need to add document.pdf and logo.png files to the root of your project. You can use the sample files provided by us — document.pdf and logo.png — or use your own.

Your project is now ready to make requests to Nutrient DWS API. Next, you’ll add your main entry point to the app. Open the Program.cs file in the new project and make sure it has the Program class with the Main method. You can also add all the imports you’re going to use:

C#
using System;
using System.IO;
using System.Net;
using RestSharp;

namespace PspdfkitApiDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      // Implement your call to Nutrient DWS API here.
    }
  }
}

Next, you’ll prepare the request.

Preparing the Request

For this example, you’ll add an image watermark to your document. To do so, you need to create an instructions JSON object:

C#
var instructions = new JsonObject
{
  ["parts"] = new JsonArray
  {
    new JsonObject
    {
      ["file"] = "document"
    }
  },
  ["actions"] = new JsonArray
  {
    new JsonObject
    {
      ["type"] = "watermark",
      ["image"] = "logo",
      ["width"] = "25%"
    }
  }
};

Here, you’re just building a JSON object via the JSON API built in to .NET. For more details on the available options specifically related to watermarking, refer to our watermarking guide.

Next, you can create a RestRequest with the required authorization header, instructions, and all required files:

C#
var request = new RestRequest(Method.POST)
  .AddHeader("Authorization", "Bearer your_api_key_here")
  .AddFile("document", "document.pdf")
  .AddFile("logo", "logo.png")
  .AddParameter("instructions", instructions.ToString());

Here, you assemble your multipart/form-data body containing the JSON instructions, your document.pdf that will be watermarked, and the logo.png that you’ll use as a watermark.

You want to save the successful response to a file. You could execute the request and save the resulting binary data to a file, but this requires reading the whole response into memory. A better solution is to stream the response to a file. For this, you’ll set AdvancedResponseWriter on your request:

C#
request.AdvancedResponseWriter = (responseStream, response) =>
{
  // Check if this is an OK response (i.e. with HTTP status 200).
  if (response.StatusCode == HttpStatusCode.OK)
  {
    // Stream the response with the processed document to a file, "result.pdf".
    using (responseStream)
    {
      using var outputFileWriter = File.OpenWrite("result.pdf");
      responseStream.CopyTo(outputFileWriter);
    }
  }
  else
  {
    // For the sake of this example, you'll write any error responses to the console.
    var responseStreamReader = new StreamReader(responseStream);
    Console.Write(responseStreamReader.ReadToEnd());
  }
};

Executing the Request

Finally, all that’s left is to actually make the request. You need a new RestClient configured to make requests to the Nutrient DWS API /build endpoint:

C#
var client = new RestClient("https://api.nutrient.io/build");

Now, use it to execute the request:

C#
client.Execute(request);

This will actually send your request to Nutrient DWS API and save the resulting PDF in the root folder as result.pdf.

At this point, you can run our example directly in the terminal:

Shell
dotnet run

After it finishes, you should see result.pdf appear in your project’s folder. And that’s it — you now have everything set up to use Nutrient DWS API from C#.

While this example made use of our watermarking API, this same approach can be used for all our available API tools.

Full Code

For your convenience, here’s the whole code. Just copy it and run it:

C#
using System;
using System.IO;
using System.Net;
using RestSharp;

namespace PspdfkitApiDemo
{
  class Program
  {
    static void Main(string[] args)
    {
      var client = new RestClient("https://api.nutrient.io/build");

      var instructions = new JsonObject
      {
        ["parts"] = new JsonArray
        {
          new JsonObject
          {
            ["file"] = "document"
          }
        },
        ["actions"] = new JsonArray
        {
          new JsonObject
          {
            ["type"] = "watermark",
            ["image"] = "logo",
            ["width"] = "25%"
          }
        }
      };

      var request = new RestRequest(Method.POST)
        .AddHeader("Authorization", "Bearer your_api_key_here")
        .AddFile("document", "document.pdf")
        .AddFile("logo", "logo.png")
        .AddParameter("instructions", instructions.ToString());

      request.AdvancedResponseWriter = (responseStream, response) =>
      {
        if (response.StatusCode == HttpStatusCode.OK)
        {
          using (responseStream)
          {
            using var outputFileWriter = File.OpenWrite("result.pdf");
            responseStream.CopyTo(outputFileWriter);
          }
        }
        else
        {
          var responseStreamReader = new StreamReader(responseStream);
          Console.Write(responseStreamReader.ReadToEnd());
        }
      };

      client.Execute(request);
    }
  }
}