←Select platform

DocumentPageText Class


Contains the text characters and words found in a document page.

public class DocumentPageText 
public [SerializableAttribute,  
   ref class DocumentPageText 
public class DocumentPageText implements Serializable 
class DocumentPageText: 

The text of a document page can be read by using the DocumentPage.GetText method. The text characters found in the page will be set in the in Characters property of the returned DocumentPageText object.

The text words are created from the characters found in the document based on the IsEndOfWord returned by document parsing engine. Whenever an "end of word" is found, the last set of characters are grouped together and stored as an item in the Words list. This is not performed automatically, instead, you must call BuildWords to populate the Words list from the Characters.

The document page text can also be obtained as a simple string object through the Text property. This is not performed automatically and you must call BuildText to populate this property with the text value from Characters. Note that BuildText will also build the words by calls BuildWords first if this has not been done by the user first.

The FirstCharacterIndex and LastCharacterIndex of the DocumentWord object can be used to map the word back to the original characters in the Characters list. Similarly, you can use BuildTextWithMap to populate Text as well the TextMap list that can be used to map the text string all the way back to its part in the Characters list.

At any time, you can update the Characters list and call any of the methods above to re-generate Words, Text and TextMap. To clear the generated values, use ClearBuildData.

The text is parsed from the original document using either SVG or OCR technologies, for more information, refer to Parsing Text with the Document Library.

using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Document.Writer; 
using Leadtools.Document; 
using Leadtools.Caching; 
using Leadtools.Annotations.Engine; 
using Leadtools.Ocr; 
using Leadtools.Barcode; 
using Leadtools.Document.Converter; 
public void DocumentPageTextExample() 
   var options = new LoadDocumentOptions(); 
   using (var document = DocumentFactory.LoadFromFile(Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.doc"), options)) 
      // Get page text  
      var page = document.Pages[0]; 
      // Get all of the DocumentTextExtractionModes (DocumentTextExtractionMode reference) 
      DocumentTextExtractionMode[] textExtractionModes = (DocumentTextExtractionMode[])Enum.GetValues(typeof(DocumentTextExtractionMode)); 
      foreach (var modes in textExtractionModes) 
         Console.WriteLine($"Text extraction mode: {modes}"); 
      // Text extraction mode. Auto is default 
      document.Text.TextExtractionMode = DocumentTextExtractionMode.Auto; 
      // DocumentPageText reference 
      var pageText = page.GetText(); 
      if (pageText != null) 
         var characters = pageText.Characters; 
         var text = pageText.Text; 
         Console.WriteLine($"Total number of characters: {characters.Count}"); 
         Console.WriteLine($"Total number of words: {pageText.Words.Count}"); 
         // Get each word 
         foreach (DocumentWord word in pageText.Words) 
            Console.WriteLine($"Bounds: {word.Bounds} | First character index: {word.FirstCharacterIndex} " + 
               $"| Last character index: {word.LastCharacterIndex} | Value: {word.Value}"); 
static class LEAD_VARS 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.net.URL; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
import java.util.concurrent.Callable; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
import java.util.concurrent.Future; 
import java.util.regex.Pattern; 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
import leadtools.*; 
import leadtools.annotations.engine.*; 
import leadtools.barcode.*; 
import leadtools.caching.*; 
import leadtools.codecs.*; 
import leadtools.document.*; 
import leadtools.document.DocumentMimeTypes.UserGetDocumentStatusHandler; 
import leadtools.document.converter.*; 
import leadtools.document.writer.*; 
import leadtools.ocr.*; 
public void documentPageTextExample() { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   LoadDocumentOptions options = new LoadDocumentOptions(); 
   LEADDocument document = DocumentFactory.loadFromFile(combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf"), 
   // Get page text 
   DocumentPage page = document.getPages().get(0); 
   // Get all of the DocumentTextExtractionModes (DocumentTextExtractionMode 
   // reference) 
   DocumentTextExtractionMode[] textExtractionModes = DocumentTextExtractionMode.values(); 
   for (DocumentTextExtractionMode modes : textExtractionModes) { 
      System.out.println("Text extraction mode: " + modes); 
   // Text extraction mode. Auto is default 
   // DocumentPageText reference 
   String text = ""; 
   DocumentPageText pageText = page.getText(); 
   assertTrue(pageText != null); 
   if (pageText != null) { 
      List<DocumentCharacter> characters = pageText.getCharacters(); 
      text = pageText.getText(); 
      System.out.println("Total number of characters: " + characters.size()); 
      System.out.println("Total number of words: " + pageText.getWords().size()); 
      // Get each word 
      for (DocumentWord word : pageText.getWords()) { 
               "Bounds: " + word.getBounds() + " | First character index: " + word.getFirstCharacterIndex() + " " + 
                     "| Last character index: " + word.getLastCharacterIndex() + " | Value: " + word.getValue()); 

Target Platforms

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

Leadtools.Document Assembly

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