logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
AboveTheSky  
#1 Posted : Wednesday, July 20, 2016 9:14:41 AM(UTC)
AboveTheSky

Rank: Newbie

Groups: Registered
Joined: 7/20/2016(UTC)
Posts: 2
Italy

Hi
Is there a way to achieve something like?

page.Text.GetTextInfo(FS_RECTF);

I would like to get TextInfo of a bounding rectangle.

Thanks in advance
Paul Rayman  
#2 Posted : Wednesday, July 20, 2016 4:39:48 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
But why?
PdfTextInfo contains the following
Text - Gets a text string associated with this instance of PdfTextInfo class.
Rect - one or more rectangular areas bounding specified text.

So, to get the text bounded by rectangle, yo can use PdfText.GetBoundedText Method
https://pdfium.patagames...5d-e99b-3437c92809f9.htm
And the bounding rectangle is known from the conditions of the task.

Edited by user Wednesday, July 20, 2016 5:35:41 PM(UTC)  | Reason: Not specified

AboveTheSky  
#3 Posted : Thursday, July 21, 2016 2:44:31 AM(UTC)
AboveTheSky

Rank: Newbie

Groups: Registered
Joined: 7/20/2016(UTC)
Posts: 2
Italy

My goal is.
I have a big column, and inside the column there are some words in different lines. The space between the rows is not the same.
I would like to get the position of the rows.


Some hint?
Thanks in advance

Edited by user Thursday, July 21, 2016 2:54:59 AM(UTC)  | Reason: Not specified

Paul Rayman  
#4 Posted : Thursday, July 21, 2016 4:37:07 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
You can check the bounding box of each character on a page for intersects with target area and if so then analyze it position relatively the previous character.
Please find code below. It's illustrates the main idea

Code:

static void Main(string[] args)
{
	PdfCommon.Initialize();

	var doc = PdfDocument.Load(@"d:\00\test_big.pdf");
	var page = doc.Pages[1];

	//target area
	var targetArea= new FS_RECTF() { left = 88, top = 586, right = 150, bottom = 546 };

	//Getting rows in target area
	//The bounded text would be placed into 'text'
	string text; 
	List<FS_RECTF> rects = GetBoundedTextInfo(page, targetArea, out text);

	//Test. Get the text bounded by found rectangles. Compare visually with text received earlier.
	//Looks like everything works fine.
	for (int i=0; i< rects.Count; i++)
	{
		string textForTest = page.Text.GetBoundedText(rects[i]);
	}
}

/// <summary>
/// Emulates GetTextInfo method for target area
/// </summary>
static List<FS_RECTF> GetBoundedTextInfo(PdfPage page, FS_RECTF rect, out string text)
{
	var cnt = page.Text.CountChars;
	List<FS_RECTF> ret = new List<FS_RECTF>();
	text = "";
	for (int i=0; i< cnt; i++)
	{
		var bbox = page.Text.GetCharBox(i);
		if (!IsIntersects(bbox, rect))
			continue; //The character does not intersects with target area.

		if (AnalizeCharBbox(ret, bbox))
			text += "\r\n"; //new row into ret collection  was added
		text += page.Text.GetCharacter(i);
	}
	return ret;
}

/// <summary>
/// Returns true if reactengles are intersected, False otherwise.
/// </summary>
static bool IsIntersects(FS_RECTF rect1, FS_RECTF rect2)
{
	if (rect1.right > rect2.left && rect1.right < rect2.right)
		if (rect1.bottom < rect2.top && rect1.bottom > rect2.bottom)
			return true; //rect 1 intersects with rect2
	return false; //the rectangles are not intersectedd
}

/// <summary>
/// Analyzes the current bounding box relatively the previous bbox.
/// If a new row was detected, adds new rectangle into ret collections and returns true.
/// Otherwise extend the last row for current bbox and returns false;
/// </summary>
private static bool AnalizeCharBbox(List<FS_RECTF> ret, FS_RECTF bbox)
{
	if (ret.Count == 0)
	{
		ret.Add(bbox);
		return false; //it's a first row
	}

	var prevBox = ret[ret.Count - 1];
	if (bbox.top < prevBox.bottom)
	{
		ret.Add(bbox);
		return true; //it's a new row;
	}
	else
	{
		prevBox.right = bbox.right;
		prevBox.top = Math.Max(prevBox.top, bbox.top);
		prevBox.bottom = Math.Min(prevBox.bottom, bbox.bottom);
		ret[ret.Count - 1] = prevBox;
		return false; //it is a not new row. Just extend a previouse row.
	}

}

Edited by user Thursday, July 21, 2016 8:38:35 PM(UTC)  | Reason: Not specified

Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.