In this tutorial, we will learn how to generate Word or PDF documents using the Documentero API service in .NET & C# language. Documentero is an API service that allows you to generate documents based on templates and data provided.

Prerequisites

Before we begin, make sure you have the following:

  • Visual Studio or any other preferred .NET development environment installed on your machine
  • Documentero API access and your API secret key

Step 1: Setting up the Project

Let’s start by setting up a new .NET project. Open Visual Studio and follow these steps:

  1. Create a new .NET project with the desired project type (e.g., Console Application, ASP.NET MVC, etc.) and name it as “DocumenteroDemo”.

  2. Open the project in Visual Studio.

Step 2: Making a POST Request to Documentero API

Next, we’ll write the code to make a POST request to the Documentero API service and generate a document.

In the project, open the main file (e.g., Program.cs for a Console Application) and add the necessary namespaces:

1
2
3
4
5
6
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Now, let’s define a method to generate the document:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
static async Task GenerateDocument()
{
string documenteroEndpoint = "https://app.documentero.com/api";

using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

JObject requestBody = new JObject
{
{ "document", "Pt04DVNbifqY4inserI2" }, // Replace with your template identifier
{ "apiKey", "YOUR_API_KEY" }, // Replace with your API secret key
{ "format", "docx" } // Set the desired format: 'docx' for Word or 'pdf' for PDF
};

JObject data = new JObject
{
{ "field", "fieldvalue" } // Replace with the data you want to pass to the document generation
};
requestBody.Add("data", data);

HttpContent content = new StringContent(requestBody.ToString(), System.Text.Encoding.UTF8, "application/json");

using (HttpResponseMessage response = await client.PostAsync(documenteroEndpoint, content))
{
string responseContent = await response.Content.ReadAsStringAsync();

if (response.IsSuccessStatusCode)
{
JObject responseData = JObject.Parse(responseContent);
int status = (int)responseData["status"];
string message = (string)responseData["message"];
string downloadLink = (string)responseData["data"];

if (status == 200)
{
Console.WriteLine("Document Generated Successfully");
Console.WriteLine("Download Link: " + downloadLink);
}
else
{
Console.WriteLine("Document Generation Failed: " + message);
}
}
else
{
Console.WriteLine("Unexpected response from Documentero API");
}
}
}
}

Make sure to replace "Pt04DVNbifqY4inserI2" with the actual template identifier provided by Documentero, and "YOUR_API_KEY" with your API secret key obtained from Documentero.

Step 3: Running the Code

Save the changes to the main file (e.g., Program.cs). Now, let’s call the GenerateDocument method from the Main method to execute the code and generate the document:

1
2
3
4
static async Task Main(string[] args)
{
await GenerateDocument();
}