Perform POCO calls to the Document Service - Console C# .NET 6

This tutorial shows how to perform POCO calls to the LEADTOOLS SDK Document Service in a C# Windows Console application. The calls covered in this tutorial will perform the following:

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 (1 KB)
Platform C# Windows Console Application
IDE Visual Studio 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 Set up and Configure the Demo with the Document Service section inside the Get Started with the Document Viewer Demo tutorial, before working on the Perform POCO calls to the Document Service - Console C# tutorial.

Build the Leadtools.Document.Service.dll

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

Build the project to build the Leadtools.Document.Service.dll.

Create the Project and Add LEADTOOLS References

Start with a brand new .NET 6 Console project. The code included in the Add References and Set a License tutorial will not be needed for the purposes of this tutorial.

The references needed depend upon the purpose of the project. References can be added via local DLL references.

The only DLLs needed for this tutorial is the Leadtools.Document.Service.dll and Newtonsoft.Json. Leadtools.Document.Service.dll was created in the previous step. That DLL should be located here: <INSTALL_DIR>\LEADTOOLS23\Examples\Document\DotNet\Document.Service\obj_\Debug

For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note: Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Interact with the Document Service Application

With the project created, the references added, and the license set, 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; 

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

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 6 Document Service runs on http://localhost:30000.

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

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:30000"); 
    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"); 
} 
C#
static async Task CallDocumentService(){ 
    var service = new DocumentService(); 
    service.ServiceAddress = new Uri("http://localhost:30000"); 
    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); 
} 
C#
static void Main(string[] args){ 
    CallDocumentService().GetAwaiter().GetResult(); 
} 

Run the Project

Ensure the .NET 6 Document Service is running properly. This is covered in the Set up and Configure the Demo with the Document Service section inside the Get Started with the Document Viewer Demo tutorial.

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 6 project.

See Also

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


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