This tutorial shows how to extract the text from a searchable PDF document in a C# Windows Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to parse the text from a PDF using the GetText() method in a C# Windows Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (3 KB) |
Platform | C# Windows Console Application |
IDE | Visual Studio 2019, 2022 |
Development License | Download LEADTOOLS |
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Parse the Text of a Document - Console C# tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If that project is unavailable, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).
If using NuGet references, this tutorial requires the following NuGet package:
Leadtools.Document.Sdk
If using local DLL references, the following DLLs are needed.
The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Document.dll
Leadtools.Document.Pdf.dll
Leadtools.Document.Writer.dll
Leadtools.Ocr.dll
Leadtools.Ocr.LEADEngine.dll
Leadtools.Pdf.dll
For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.
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:
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
:
using System;
using System.IO;
using Leadtools;
using Leadtools.Document;
using Leadtools.Ocr;
Create a new method inside Program.cs
named ExtractDocumentText()
. Call the method in the Main()
method, under the set license call, as shown below.
static void Main(string[] args)
{
try
{
SetLicense();
ExtractDocumentText();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
}
Add the code below to the ExtractDocumentText()
method to extract the text from the given document and display the text to the console.
static void ExtractDocumentText()
{
string _documentFile = @"C:\LEADTOOLS22\Resources\Images\leadtools.pdf";
using (LEADDocument _document = DocumentFactory.LoadFromFile(_documentFile, new LoadDocumentOptions()))
{
IOcrEngine _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);
_ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime");
_document.Text.OcrEngine = _ocrEngine;
foreach (DocumentPage _page in _document.Pages)
{
DocumentPageText _pageText = _page.GetText();
_pageText.BuildText();
string _text = _pageText.Text;
Console.WriteLine($"Page Number: {_page.PageNumber}\n");
Console.WriteLine($"{_text}\n");
}
}
}
To handle the files using MemoryStream
, replace the DocumentFactory.LoadFromFile
call in the ExtractDocumentText()
method with DocumentFactory.LoadFromStream
as follows:
static void ExtractDocumentText()
{
string _documentFile = @"C:\LEADTOOLS22\Resources\Images\leadtools.pdf";
byte[] _documentData = File.ReadAllBytes(_documentFile);
using (MemoryStream _documentStream = new MemoryStream(_documentData))
using (LEADDocument _document = DocumentFactory.LoadFromStream(_documentStream, new LoadDocumentOptions()))
{
IOcrEngine _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD);
_ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime");
_document.Text.OcrEngine = _ocrEngine;
foreach (DocumentPage _page in _document.Pages)
{
DocumentPageText _pageText = _page.GetText();
_pageText.BuildText();
string _text = _pageText.Text;
Console.WriteLine($"Page Number: {_page.PageNumber}\n");
Console.WriteLine($"{_text}\n");
}
}
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application loads the specified document as a LEADDocument
, iterates through each DocumentPage
in the LEADDocument
, extracts the text, and then displays the text string to the console.
This tutorial showed how to extract the text from a document file and output the text to the console. Also it covered how to use the IOcrEngine
interface and the LEADDocument
, DocumentPage
, and DocumentPageText
classes.