How to Get PDF Document Metadata in C# .NET Framework
Before we make changes to the metadata section of a PDF document, we might want to check what information we already have in place.
Using the below code, we can quickly retrieve PDF document metadata with a free, low-code API request. We’ll return our metadata information structured like the following JSON example:
{
"Successful": true,
"ErrorDetails": "string",
"Title": "string",
"Keywords": "string",
"Subject": "string",
"Author": "string",
"Creator": "string",
"DateModified": "2024-02-20T15:09:57.058Z",
"DateCreated": "2024-02-20T15:09:57.058Z",
"PageCount": 0,
"Encrypted": true
}
To make our API call, we can start by grabbing an API key to authorize our connection. A free API key will allow us to make up to 800 API calls per month with no commitments.
With our API key ready, we can begin structuring our API call with ready-to-run code examples.
First, let’s install the SDK. To install via NuGet, let’s run the following command in our Package Manager console:
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2
Now let’s copy the below code into our file and provide our API key in the appropriate snippet. We can then load our file into memory and call the function to retrieve our document metadata:
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 EditPdfGetMetadataExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new EditPdfApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
try
{
// Get PDF document metadata
PdfMetadata result = apiInstance.EditPdfGetMetadata(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditPdfApi.EditPdfGetMetadata: " + e.Message );
}
}
}
}
That’s all there is to it — now we can programmatically retrieve PDF metadata without any manual intervention.