Passport Loader

My first PassportPDF .NET application

This guide provides a simple step by step introduction to create a .NET console application using the PassportPDF Rest API. In just a few minutes, you will be able to programmatically and straightforwardly perform complex operations on your documents. You can find out about all you can do with the API right here.

Prerequisites

To follow this guide, you will need Visual Studio (no specific version is required).
You will also need to sign up on PassportPDF and get your passport number in the My Account section. This passport number acts as an API key, that will allow your authentication. A passport has a certain number of credits associated to it, which will be used to consume the API services.

Create and setup your application

Open Visual Studio.

Create a new .NET Console Application. Note that the PassportPDF SDK is compatible with .NET 4.6.1 or higher and.NET Core 2.0 or higher applications. In this guide we will create a .NET Core application.

Once you have created your application, add a reference to the PassportPDF NuGet package by right-clicking on your project in the Solution Explorer window on the right, and clicking on Manage Nuget Packages. Search for “PassportPDF”, and then install the package.

Usage of the API

In this guide, we will demonstrate how to convert any type of document to a searchable PDF (using OCR). You will find the full source code of the application at the bottom of the page along with two other samples: hyper-compress an image, and merge different documents into a new PDF.

PassportPDF API exposes several endpoints that let you perform highly configurable and complex operations on documents. Those endpoints are organized into categories, based on their routes. You will therefore find all the PDF-related, and Image-related available endpoints respectively in the PDF and Image categories on the reference page:

This organization is reflected in the SDK, and you will find a class for each of these API groups in the PassportPDF.Api namespace. Likewise, each available endpoint is accessible through a method of the same name. You may provide your passport number to the constructors of those classes when you are creating an instance.

PDFApi pdfApi = new PDFApi("XXX-PassportNumber-XXX");

Tip: You can set your passport number once for the whole life cycle of your application by using the GlobalConfiguration.ApiKey static property.

The first thing you want to do is uploading you document into the API. Depending on what you want to achieve, you may use a different route to achieve that. In this guide we want to perform OCR on a document imported as PDF, and will therefore use the pdf/LoadDocumentAsPDFMultipart endpoint with the PDFApi.LoadDocumentAsPDFMultipart method.

var loadDocumentResponse = pdfApi.LoadDocumentAsPDFMultipart(inputFile);

This method returns a PdfLoadDocumentResponse object, which is modeled after the schema of the same name, whose full definition can be found with all the other schemas in the Schema category at the bottom of the reference page:

Tip: Multipart upload/download lets you directly stream a file to/from the API and should be favored when dealing with large files.

The PdfLoadDocumentResponse schema contains a FileId string, which allows you to reference the uploaded document in your forthcoming operations. Now the document is uploaded, we will simply provide its file ID through the PdfOCRParameters schema to the pdf/OCR endpoint, along with the page range we want to process (“*” for the whole document).

var ocrResponse = pdfApi.OCR(new PdfOCRParameters(loadDocumentResponse.FileId, "*"));

Tip: Each schema representing a response contains an Error in its definition, which defines several levels of information about an error that has occurred in the API while processing your request. The value of the Error property of a response should therefore always be checked to make sure nothing went wrong before proceeding further

Once we have performed all the operations we want on a document, we need to download the new version of the file from the API. To obtain our searchable PDF, we will use the PDFApi.SaveDocumentToFile method and provide a stream to a file on our hard drive for the SDK to write the downloaded file data onto.

using (var outputFile = File.Create(@"d:\output.pdf"))
    {
        pdfApi.SaveDocumentToFile(new PdfSaveDocumentParameters(loadDocumentResponse.FileId), outputFile);
    }

Code samples

using System.IO;
using PassportPDF.Api;
using PassportPDF.Model;

namespace MyFirstPassportPDFApp
{
    class Program
    {
        static void Main(string[] args)
        {
            PDFApi pdfApi = new PDFApi("XXX-PassportNumber-XXX");
            using (var inputFile = File.OpenRead(@"d:\input.pdf"))
            {
                var loadDocumentResponse = pdfApi.LoadDocumentAsPDFMultipart(inputFile);
                if (loadDocumentResponse.Error == null)
                {
                    var ocrResponse = pdfApi.OCR(new PdfOCRParameters(loadDocumentResponse.FileId, "*"));
                    if (ocrResponse.Error == null)
                    {
                        using (var outputFile = File.Create(@"d:\output.pdf"))
                        {
                            pdfApi.SaveDocumentToFile(new PdfSaveDocumentParameters(loadDocumentResponse.FileId), outputFile);
                        }
                    }
                }
            }
        }
    }
}

using System.IO;
using PassportPDF.Api;
using PassportPDF.Model;

namespace MyFirstPassportPDFApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ImageApi imageApi = new ImageApi("XXX-PassportNumber-XXX");
            using (var inputFile = File.OpenRead(@"d:\input.jpg"))
            {
                var loadDocumentResponse = imageApi.ImageLoadMultipart(inputFile);
                if (loadDocumentResponse.Error == null)
                {
                    using (var outputFile = File.Create(@"d:\output.pdf"))
                    {
                        imageApi.ImageSaveAsPDFMRCFile(new ImageSaveAsPDFMRCParameters(loadDocumentResponse.FileId), outputFile);
                    }
                }
            }
        }
    }
} 

using System.Collections.Generic;
using System.IO;
using PassportPDF.Api;
using PassportPDF.Model;

namespace MyFirstPassportPDFApp
{
    class Program
    {
        static void Main(string[] args)
        {
            PDFApi pdfApi = new PDFApi("XXX-PassportNumber-XXX");
            using (var inputFile1 = File.OpenRead(@"d:\input.docx"))
            {
                using (var inputFile2 = File.OpenRead(@"d:\input.jpg"))
                {
                    var fileIdToMerge = new List<string>();
                    var loadDocumentResponse = pdfApi.LoadDocumentAsPDFMultipart(inputFile1);
                    if (loadDocumentResponse.Error == null)
                    {
                        fileIdToMerge.Add(loadDocumentResponse.FileId);
                        loadDocumentResponse = pdfApi.LoadDocumentAsPDFMultipart(inputFile2);
                        if (loadDocumentResponse.Error == null)
                        {
                            fileIdToMerge.Add(loadDocumentResponse.FileId);
                            var mergeResponse = pdfApi.Merge(new PdfMergeParameters(fileIdToMerge));
                            if (mergeResponse.Error == null)
                            {
                                using (var outputFile = File.Create(@"d:\output.pdf"))
                                {
                                    pdfApi.SaveDocumentToFile(new PdfSaveDocumentParameters(mergeResponse.FileId), outputFile);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}