Custom text highlighting in PDFViewCtrl

Q: I would like to implement a custom text selection in PDFViewCtrl for .NET.
Essentially I would like to highlight multiple instances of word (or different workds) throughout PDF.

I use the following approach (see code below) but the problem is that
PDFView.ConvPagePtToScreenPt() does not give me the coordinates I need.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using pdftron.SDF;
using pdftron.PDF;
namespace PDFViewer
{
public partial class Form1 : Form
{
pdftron.PDF.PDFDoc doc;
public Form1()
{
InitializeComponent();
}
protected override void OnShown(EventArgs e)
{
doc = new pdftron.PDF.PDFDoc(“my.pdf”);
doc.InitSecurityHandler();
pdfViewCtrl1.SetDoc(doc);
Application.DoEvents();
base.OnShown(e);
}
void SelectAllWords(string find)
{
Int32 page_num = 0;
String result_str = “”, ambient_string = “”;
Highlights h = new Highlights();
TextSearch txt_search = new TextSearch();
Int32 mode = (Int32)(TextSearch.SearchMode.e_highlight);
String pattern = “a”;
//call Begin() method to initialize the text search.
txt_search.Begin(doc, pattern, mode, -1, -1);
int step = 0;
//call Run() method iteratively to find all matching instances.
while (true)
{
Highlights h2 = new Highlights();
TextSearch.ResultCode code = txt_search.Run(ref page_num, ref result_str, ref ambient_string, h2);
if (code == TextSearch.ResultCode.e_found)
{
h.Add(h2);
}
else
{
break;
}
}
pdfViewCtrl1.Select(h);
}
}

A:

Looking over your test project it seems that you are explicitly drawing rectangles on top of the form which is not right way to implement this functionality.

The simplest way would be to use built-in text selection facility PDFView.Select() to highlight text. For example, the following snippet uses TextSearch class to highlights all instances of a given work on the page:

void SelectAllWords(string find)

{

Int32 page_num = 0;

String result_str = “”, ambient_string = “”;

Highlights h = new Highlights();

TextSearch txt_search = new TextSearch();

Int32 mode = (Int32)(TextSearch.SearchMode.e_highlight);

String pattern = “and”;

//call Begin() method to initialize the text search.

txt_search.Begin(doc, pattern, mode, -1, -1);

int step = 0;

//call Run() method iteratively to find all matching instances.

while (true)

{

Highlights h2 = new Highlights();

TextSearch.ResultCode code = txt_search.Run(ref page_num, ref result_str, ref ambient_string, h2);

if (code == TextSearch.ResultCode.e_found)

{

h.Add(h2);

}

else

{

break;

}

}

pdfViewCtrl1.Select(h);

}

If you need more control, you would need to derive a new class from PDFVIew and implement OnPaint method (as shown in PDFView sample project).