Apr 17, 2020

2 min read

How to validate a PDF Document in C# .NET Framework

Having PDF validation built into your project can definitely sift out a lot of potential bumps in the road down the line. I’m going to show you how to set it up without going through the arduous task of writing it all from scratch. We can make judicious implementation of an API to accomplish the same results in a fraction of the time.

Installation of our client package can be taken care of with this command here.

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

Following that, it’s time to make a simple function call for ValidateDocumentPdfValidation, as shown here:

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 ValidateDocumentPdfValidationExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new ValidateDocumentApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.try{// Validate a PDF document fileDocumentValidationResult result = apiInstance.ValidateDocumentPdfValidation(inputFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling ValidateDocumentApi.ValidateDocumentPdfValidation: " + e.Message );}}}}

And that is all she wrote. That’s right, now we just have to input some PDF files to test it out. No problem.