How to Set PDF Metadata in C# .NET Framework

Cloudmersive
2 min readFeb 20, 2024

--

The metadata section of a PDF improves the document’s credibility and searchability, among other things. Thankfully, if our PDFs are lacking relevant metadata, we can fix that without ever opening a PDF editor.

Using ready-to-run code provided below, we can call a free API that allows us to set our PDF metadata with simple request parameters.

We can structure our input request like the following JSON example:

{
"InputFileBytes": "string",
"MetadataToSet": {
"Successful": true,
"ErrorDetails": "string",
"Title": "string",
"Keywords": "string",
"Subject": "string",
"Author": "string",
"Creator": "string",
"DateModified": "2024-02-20T14:50:58.660Z",
"DateCreated": "2024-02-20T14:50:58.660Z",
"PageCount": 0,
"Encrypted": true
}
}

Through this straightforward request, we can interact directly with the PDF’s document information dictionary and customize metadata for our file. Filling out information for “keywords”, for example, will help improve the searchability of the document in a database or through internet search engines.

Before we copy code, let’s turn our attention to API call authorization. We’ll need a free Cloudmersive API key to make up to 800 API calls per month with no commitments.

Once we have our API key ready, let’s go ahead and install the SDK. We can install via NuGet by running the below command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2

Next, let’s call the function. After we copy our API key string into the appropriate line, let’s load our PDF file bytes and customize the input request parameters provided above:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;

namespace Example
{
public class EditPdfSetMetadataExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new EditPdfApi();
var request = new SetPdfMetadataRequest(); // SetPdfMetadataRequest |

try
{
// Sets PDF document metadata
byte[] result = apiInstance.EditPdfSetMetadata(request);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditPdfApi.EditPdfSetMetadata: " + e.Message );
}
}
}
}

Just like that, we can programmatically set PDF document metadata without using any official PDF editor in the process.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.