Rank: Administration
Groups: Administrators
Joined: 1/5/2016(UTC) Posts: 1,075
Thanks: 6 times Was thanked: 124 time(s) in 121 post(s)
|
Here is an example of such functionality. Code:
public class MyPdfViewer : PdfViewer
{
PointF _startPt = new PointF(0, 0);
PointF _curPt = new PointF(0, 0);
int _shapshotPage = -1;
bool _isSelectionStarted = false;
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
_shapshotPage = PointInPage(e.Location);
if (_shapshotPage >= 0)
{
HighlightedTextInfo.Clear();
_startPt = ClientToPage(_shapshotPage, e.Location);
_curPt = _startPt;
_isSelectionStarted = true;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
_isSelectionStarted = false;
if (_shapshotPage >= 0)
{
FS_POINTF leftTop, rightBottom;
NormalizeSnapshotPoints(out leftTop, out rightBottom);
var page = Document.Pages[_shapshotPage];
var len = page.Text.CountChars;
int lineLen = 0;
for(int i=0; i< len; i++)
{
var chBox = page.Text.GetCharBox(i);
if(IntersectsWith(chBox, new FS_RECTF(leftTop.X, leftTop.Y, rightBottom.X, rightBottom.Y)))
lineLen++;
else if(lineLen>0)
{
if (!HighlightedTextInfo.ContainsKey(_shapshotPage))
HighlightedTextInfo.Add(_shapshotPage, new List<HighlightInfo>());
var hInf = HighlightedTextInfo[_shapshotPage];
hInf.Add(new HighlightInfo() { CharIndex = i - lineLen, CharsCount = lineLen, Color = Color.Red });
lineLen = 0;
Invalidate();
}
}
_shapshotPage = -1;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_isSelectionStarted)
{
DeselectText();
int pageIdx = PointInPage(e.Location);
if (pageIdx != _shapshotPage)
return;
_curPt = ClientToPage(_shapshotPage, e.Location);
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (_shapshotPage >= 0)
{
var pt1 = PageToClient(_shapshotPage, _startPt);
var pt2 = PageToClient(_shapshotPage, _curPt);
int left = Math.Min(pt1.X, pt2.X);
int top = Math.Min(pt1.Y, pt2.Y);
int right = Math.Max(pt1.X, pt2.X);
int bottom = Math.Max(pt1.Y, pt2.Y);
e.Graphics.DrawRectangle(Pens.Red, left, top, right - left, bottom - top);
}
}
private void NormalizeSnapshotPoints(out FS_POINTF leftTop, out FS_POINTF rightBotton)
{
leftTop = new FS_POINTF(
Math.Min(_startPt.X, _curPt.X),
Math.Max(_startPt.Y, _curPt.Y));
rightBotton = new FS_POINTF(
Math.Max(_startPt.X, _curPt.X),
Math.Min(_startPt.Y, _curPt.Y));
}
public bool IntersectsWith(FS_RECTF rect1, FS_RECTF rect2)
{
return rect1.left < rect2.right && rect1.right > rect2.left && rect1.top > rect2.bottom && rect1.bottom < rect2.top;
}
}
|