FindText method
<< Click to Display Table of Contents >> Navigation: Component Description > Methods > FindText method |
Declaration
function FindText(Text: string; HighLight, FindNext: Boolean; CaseInsensitive: Boolean = false; DontGoToPage: Boolean = false): Boolean;
Description
This function searches text in the loaded PDF file. Please set the parameter HighLight to TRUE to also highlight the found text. Use FindNext=TRUE to continue a search on the following pages. You need to pass the same search string. To switch of the highlighting pass an empty search text. Please note: The search function does not check spaces.
This functions is implemented like this:
function TWPViewPDF.FindText(
Text: string;
HighLight, FindNext: Boolean;
CaseInsensitive: Boolean = false ;
DontGoToPage: Boolean = false ): Integer;
begin
CommandEx(COMPDF_FindGotoPage, Integer(DontGoToPage));
CommandEx(COMPDF_FindCaseInsitive, Integer(CaseInsensitive));
// If we search case insensitive we simply search 2 versions of the same string
// This allows the support of charsets
if CaseInsensitive then
begin
CommandStr(COMPDF_FindAltText, AnsiUpperCase(Text));
Text := AnsiLowerCase(Text);
end;
if FindNext then
Result := CommandStr(COMPDF_FindNext, Text) // Next
else
Result := CommandStr(COMPDF_FindText, Text); // First
if HighLight then
CommandStr(COMPDF_HighlightText, Text);
end;
It is also possible to create an highlight annotation which covers the found text. (It is not possible to delete the text)
This requires WPViewPDF PLUS.
procedure TForm1.FindtextAndAddHighlightAnnotClick(Sender: TObject);
var s : string;
b : Boolean;
page, x,y,w,h : Integer;
begin
s := '';
b := false;
if (pdf<>nil) and InputQuery('Red Text', '', s) then
begin
try
pdf.command(COMPDF_BEGINUPDATE);
while pdf.FindText(s, false, b, true)>=0 do
begin
b := true;
page := pdf.command(COMPDF_FindGetXYWH, 10);
if page>=0 then
begin
x := pdf.command(COMPDF_FindGetXYWH, 11);
y := pdf.command(COMPDF_FindGetXYWH, 12);
w := pdf.command(COMPDF_FindGetXYWH, 13);
h := pdf.command(COMPDF_FindGetXYWH, 14);
pdf.AddHighlightRect(page, x,y,w,h, clRed, [wpAsAnnot,wpAnnotAtFoundText]);
end
else break;
end;
finally
pdf.command(COMPDF_ENDUPDATE, 2);
end;
end;
end;