Perform POCO calls to the Document Service - Console C#

This tutorial shows how to perform POCO calls to the LEADTOOLS Document Service in a C# Windows Console application.

Overview  
Summary This tutorial covers how to handle POCO calls to and from the LEADTOOLS Document Service in a C# Windows Console application.
Completion Time 20 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform C# Windows Console Application
IDE Visual Studio 2019, 2022
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of setting up and running the Document Service project by reviewing the Configure and Run the Document Service tutorial, before working on the Perform POCO calls to the Document Service - Console C# tutorial.

The Document Service has different API calls which can be interacted with and without using any HTML5/JS UI.

This tutorial shows how to use .NET controllers to make the API calls.

The calls covered in this tutorial will perform the following:

Build the Leadtools.Document.Service.dll

Open the Class Library project located in the below file path. <INSTALL_DIR>\LEADTOOLS22\Examples\Document\DotNet\Document.Service\fx

Build the project as a release configuration to generate the Leadtools.Document.Service.dll assembly.

Add LEADTOOLS References

Create a new C# Windows console application project in Visual Studio.

The only DLL needed for this tutorial is the Leadtools.Document.Service.dll that was created in the previous step. That DLL should be located here: <INSTALL_DIR>\LEADTOOLS22\Examples\Document\DotNet\Document.Service\fx\bin\Release\

Interact with the Document Service Application

With the project created and the DLL reference added, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs:

C#
using Leadtools.Document.Service; 
using Leadtools.Document.Service.Document; 
using Leadtools.Document.Service.Models; 
using Leadtools.Document.Service.Models.Factory; 
using Leadtools.Document.Service.Models.Page; 

Verify that the Service Connection is Established

In the Program.cs file, create a new static async Task named CallDocumentService(). Create a DocumentService object and set the ServiceAddress to the URI that is being used by the Document Service. Next, create a Request object to ping if the service is ready. For the purposes of the tutorial the .NET Framework Document Service runs on http://localhost:40000.

C#
static async Task CallDocumentService(){ 
    var service = new DocumentService(); 
    service.ServiceAddress = new Uri("http://localhost:40000"); 
    var pingRequest = new Request(); 
    await service.Test.PingGet(pingRequest); 
} 

Load a File from a URI into the Document Service Cache

Update the CallDocumentService() method to add the code below to create a LoadFromUriRequest object and return a LEADDocument object.

Once the file is loaded into the Document Service's Cache, the DocumentId is needed to gather the text from a page of the LEADDocument.

C#
static async Task CallDocumentService(){ 
    var service = new DocumentService(); 
    service.ServiceAddress = new Uri("http://localhost:40000"); 
    var pingRequest = new Request(); 
    await service.Test.PingGet(pingRequest); 
 
    var loadFromUriRequest = new LoadFromUriRequest 
    { 
        Uri = new Uri("https://demo.leadtools.com/images/pdf/leadtools.pdf"), 
        Options = new LoadDocumentOptions { }, 
    }; 
 
    var loadFromUriResult = await service.Factory.LoadFromUri(loadFromUriRequest); 
    var document = LEADDocument.FromJSON(loadFromUriResult.Response.Document); 
 
    Console.WriteLine($"Document ID: {document.DocumentId}\n"); 
} 

Retrieve the Text of the File from the Cache

Update the CallDocumentService() method to receive and output the text to the console.

C#
static async Task CallDocumentService(){ 
    var service = new DocumentService(); 
    service.ServiceAddress = new Uri("http://localhost:40000"); 
    var pingRequest = new Request(); 
    await service.Test.PingGet(pingRequest); 
 
    var loadFromUriRequest = new LoadFromUriRequest 
    { 
        Uri = new Uri("https://demo.leadtools.com/images/pdf/leadtools.pdf"), 
        Options = new LoadDocumentOptions { }, 
    }; 
 
    var loadFromUriResult = await service.Factory.LoadFromUri(loadFromUriRequest); 
    var document = LEADDocument.FromJSON(loadFromUriResult.Response.Document); 
     
    Console.WriteLine($"Document ID: {document.DocumentId}\n"); 
 
    var getTextRequest = new GetTextRequest 
    { 
        BuildText = true, 
        BuildWords = true, 
        DocumentId = document.DocumentId, 
        ImagesRecognitionMode = DocumentTextImagesRecognitionMode.Auto, 
        PageNumber = 1, 
        TextExtractionMode = DocumentTextExtractionMode.OcrOnly, 
    }; 
 
    var getTextResult = await service.Page.GetText(getTextRequest); 
    var documentPageText = getTextResult.Response.PageText; 
 
    Console.WriteLine(documentPageText.Text); 
} 

Add the line of code below to run the CallDocumentService() function.

C#
static void Main(string[] args){ 
    CallDocumentService().GetAwaiter().GetResult(); 
} 

Run the Project

Ensure the .NET Framework Document Service is running properly.

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application runs and the Document ID and text extracted will display to the console.

Screenshot showing the console output.

Wrap-up

This tutorial showed how to interact with the LEADTOOLS SDK Document Service. It also showed how to build the Leadtools.Document.Service.dll and reference it in a C# .NET Framework project.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.