Perform POCO calls to the Document Service - Console C#

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 (2 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
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>\LEADTOOLS22\Examples\Document\DotNet\Document.Service

Build the project as a release build to build the Leadtools.Document.Service.dll.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

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

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\bin\Release\

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.

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); 
} 
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"); 
} 
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); 
} 
C#
static void Main(string[] args){ 
    CallDocumentService().GetAwaiter().GetResult(); 
} 

Run the Project

Ensure the .NET Framework 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 Framework project.

See Also

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

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