You are viewing limited content. For full access, please sign in.

Question

Question

TextLinker code and usage

asked on May 29, 2014

I am looking for an example of the TextLinker class in the  Laserfiche.RepositoryAccess namespace of the Laserfiche 8.2 .NET SDK, both in code and in practice.  Thank you 

0 0

Answer

SELECTED ANSWER
replied on May 29, 2014 Show version history

The following snippet is untested, but it should give you an idea. The xPos and yPos variables refer to a pixel on the page image, with (0, 0) being the top-left corner and (width - 1, height - 1) being the lower-right corner.

 

const int DOCUMENT_ID = 12345;
const int PAGE_NUMBER = 1;
int xPos = ..., yPos = ...;

// Log in and get a Session.
Session session = ...;

// Read the text portion of the page.
Stream textStream = Document.ReadPagePart(DOCUMENT_ID, PAGE_NUMBER,
  PagePart.Text, session);
byte[] textBytes = new byte[textStream.Length];
textStream.Read(textBytes, 0, textBytes.Length);
MemoryStream textData = new MemoryStream(textBytes, false);

// Read the OCR locations data for the page.
Stream locationsStream = Document.ReadPagePart(DOCUMENT_ID, PAGE_NUMBER,
  PagePart.Locations, session);
byte[] locationsBytes = new byte[locationsStream.Length];
locationsStream.Read(locationsBytes, 0, locationsBytes.Length);
MemoryStream locationsData = new MemoryStream(locationsBytes, false);

// Instantiate TextLinker with the text and locations data.
TextLinker textLinker = new TextLinker(
  new WordsReader(textData),
  new WordLocationsReader(locationsData));

// Get the text inside the rectangles which intersect
// the point and print it out.
IList<TextRange> hitText = textLinker.GetTextRanges(
  new LfRectangle(xPos, yPos, 1, 1));
string pageText = Encoding.Unicode.GetString(textBytes);
foreach (TextRange range in hitText)
{
    Console.Write(pageText.Substring(range.StartPosition, range.Length));
}

 

2 0

Replies

replied on May 29, 2014 Show version history

I don't have the 8.2 SDK installed any more, but a quick search in the documentation for the latest version shows several RA examples. What specifically are you looking for?

0 0
replied on May 29, 2014 Show version history

Really?  I've looked there.What I am hoping to accomplish is, say a user clicks on a word, or group of words in an already OCR'd image, I want to return the OCR text of that word or words

0 0
replied on May 29, 2014

That is a fairly complicated task. I'll assume that you are able to map the user's click location to the appropriate page coordinates, and that you already have a PageInfo for the page you are currently displaying.

 

First, you'll want to use the PageInfo's ReadLocationsPagePart to read the locations of all of the words on the page and cache them.

 

Next, you'll need to construct a TextLinker object using the return values from the PageInfo's ReadLocationsPagePart and ReadTextPagePartAsWords.

 

When a user clicks on your image, you will want to check against all of your cached rectangles to figure out which (if any) rectangle the user has clicked. Then pass this rectangle to the GetTextRanges method of your TextLinker to figure out which word(s) the user has clicked on.

 

For click-and-drag, your user click would turn into a rectangle, and you would want to find the rectangles that intersect or are contained in that user rectangle.

 

 

0 0
SELECTED ANSWER
replied on May 29, 2014 Show version history

The following snippet is untested, but it should give you an idea. The xPos and yPos variables refer to a pixel on the page image, with (0, 0) being the top-left corner and (width - 1, height - 1) being the lower-right corner.

 

const int DOCUMENT_ID = 12345;
const int PAGE_NUMBER = 1;
int xPos = ..., yPos = ...;

// Log in and get a Session.
Session session = ...;

// Read the text portion of the page.
Stream textStream = Document.ReadPagePart(DOCUMENT_ID, PAGE_NUMBER,
  PagePart.Text, session);
byte[] textBytes = new byte[textStream.Length];
textStream.Read(textBytes, 0, textBytes.Length);
MemoryStream textData = new MemoryStream(textBytes, false);

// Read the OCR locations data for the page.
Stream locationsStream = Document.ReadPagePart(DOCUMENT_ID, PAGE_NUMBER,
  PagePart.Locations, session);
byte[] locationsBytes = new byte[locationsStream.Length];
locationsStream.Read(locationsBytes, 0, locationsBytes.Length);
MemoryStream locationsData = new MemoryStream(locationsBytes, false);

// Instantiate TextLinker with the text and locations data.
TextLinker textLinker = new TextLinker(
  new WordsReader(textData),
  new WordLocationsReader(locationsData));

// Get the text inside the rectangles which intersect
// the point and print it out.
IList<TextRange> hitText = textLinker.GetTextRanges(
  new LfRectangle(xPos, yPos, 1, 1));
string pageText = Encoding.Unicode.GetString(textBytes);
foreach (TextRange range in hitText)
{
    Console.Write(pageText.Substring(range.StartPosition, range.Length));
}

 

2 0
replied on June 4, 2014

Thanks a lot.  That was enough to get me started.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.