PDFNet[Android]: How to search a PDF file and highlight in PDFViewCtrl

Skip to first unread message

Frank Liu

Jun 18, 2012, 2:10:41 PM6/18/12
to PDFTron PDFNet SDK on behalf of Support
I want to provide the search functionality to my application.

Once the search is complete, the searched text should be shown as highlighted in the PDFViewCtrl.



 you can use PDFViewCtrl.findText() for searching, which will also highlight the found text for you. Note that it searches in an incremental manner. If you want to find all the instances in the background and then provide user with a list of found instances, you are better off doing it yourself using TextSearch class.

Roughly speaking, you can use TextSearch to find all the instances within a PDF file. For each found instances, you can record a Highlights instance (http://www.pdftron.com/pdfnet/samplecode/TextSearchTest.java). If you want PDFViewCtrl to select the text corresponding to a Highlight instance, you can use PDFViewCtrl.selectByHighlights(). This class will simply set certain information within PDFViewCtrl and it will not automatically highlight anything on screen. If you want to highlight the text on screen, you can do something as follows:

private void populateSelectionResult() {
Path sel_path = new Path();
float sx = PDFViewCtrl.getScrollX();
float sy = PDFViewCtrl.getScrollY();
int sel_pg_begin = PDFViewCtrl.getSelectionBeginPage();
int sel_pg_end = PDFViewCtrl.getSelectionEndPage();
//loop through the pages that have text selection, and construct 'mSelPath' for highlighting.
for ( int pg = sel_pg_begin; pg <= sel_pg_end; ++pg )  {
PDFViewCtrl.Selection sel = PDFViewCtrl.getSelection(pg); //each Selection may have multiple quads 
double[] quads = sel.getQuads();
double[] pts;
int sz = quads.length / 8; //each quad has eight numbers (x0, y0), ... (x3, y3)
if ( sz == 0 ) {
int k = 0;
float x, y;
for (int i = 0; i < sz; ++i, k+=8) {
pts = PDFViewCtrl.convPagePtToClientPt(quads[k], quads[k + 1], pg);
x = (float) pts[0] + sx;
y = (float) pts[1] + sy;
sel_path.moveTo(x, y);
pts = PDFViewCtrl.convPagePtToClientPt(quads[k + 2], quads[k + 3], pg);
x = (float) pts[0] + sx;
y = (float) pts[1] + sy;
sel_path.lineTo(x, y);
pts = PDFViewCtrl.convPagePtToClientPt(quads[k + 4], quads[k + 5], pg);
x = (float) pts[0] + sx;
y = (float) pts[1] + sy;
sel_path.lineTo(x, y);
pts = PDFViewCtrl.convPagePtToClientPt(quads[k + 6], quads[k + 7], pg);
x = (float) pts[0] + sx;
y = (float) pts[1] + sy;
sel_path.lineTo(x, y);


Then you can draw 'sel_path' in order to highlight.  We used similar code in Tools.jar.

Anderson Konzen

Apr 22, 2014, 3:42:01 PM4/22/14
to pdfne...@googlegroups.com

Starting with version 6.1.0 of the Android PDFNet SDK, the Tools library is now shipped as an Android Library, and its source code can be found in the samples folder. The package does not include the Tools.jar anymore, and you now have the flexibility to include the source directly into your project or create a separate library for your projects.
Reply all
Reply to author
0 new messages