In some cases, you may need to get the rectangle of the selected text, without adding an annotation (export motives for example).
To do so, you can use the below method, which will return the rectangle of the selected text in PDF coordinates.
You can call the below method in PDFLayoutView.onTouchSelect as follows:
case MotionEvent.ACTION_CANCEL:
if(m_sel != null)
{
m_sel.SetSel( m_annot_pos.x, m_annot_pos.y,
m_annot_page.ToPDFX(event.getX(), m_layout.vGetX()),
m_annot_page.ToPDFY(event.getY(), m_layout.vGetY()) );
invalidate();
float[] rect = getSelectionRect(event.getX(), event.getY());
if(m_listener != null) m_listener.OnPDFSelectEnd(m_sel.GetSelString());
}
break;
getSelectionRect:
private float[] getSelectionRect(float lastX, float lastY) {
float pt[] = new float[2];
pt[0] = m_annot_pos.x;
pt[1] = m_annot_pos.y;
Page page = m_doc.GetPage(m_annot_pos.pageno);
page.ObjsStart();
int index1 = page.ObjsGetCharIndex(pt);
pt[0] = m_annot_page.ToPDFX(lastX, m_layout.vGetX());
pt[1] = m_annot_page.ToPDFY(lastY, m_layout.vGetY());
int index2 = page.ObjsGetCharIndex(pt);
if( index1 > index2 ) {
int tmp = index1;
index1 = index2;
index2 = tmp;
}
index1 = page.ObjsAlignWord(index1, -1);
index2 = page.ObjsAlignWord(index2, 1);
float[] rect = new float[4];
float[] selectRect = new float[4];
page.ObjsGetCharRect(index1, rect);
selectRect[0] = rect[0];
selectRect[1] = rect[1];
selectRect[2] = rect[2];
selectRect[3] = rect[3];
int tmp = index1 + 1;
while( tmp <= index2 ) {
page.ObjsGetCharRect(tmp, rect);
float gap = (rect[3] - rect[1])/2;
if( selectRect[1] == rect[1] && selectRect[3] == rect[3] &&
selectRect[2] + gap > rect[0] && selectRect[0] - gap < rect[2] ) {
if( selectRect[0] > rect[0] ) selectRect[0] = rect[0];
if( selectRect[2] < rect[2] ) selectRect[2] = rect[2];
}
else {
if( selectRect[0] > rect[0] ) selectRect[0] = rect[0];
selectRect[1] = rect[1];
if( selectRect[2] < rect[2] ) selectRect[2] = rect[2];
if( selectRect[3] < rect[3] ) selectRect[3] = rect[3];
}
tmp++;
}
return selectRect;
}
RadaeePDF SDK for Android
Created : 2017-07-11 12:40:32, Last Modified : 2017-07-11 12:48:54