PDF 搜索 (Text Search)
福昕 PDF SDK 提供 APIs 来搜索 PDF 文档、XFA 文档、文本页面或者 PDF 注释中的文本。它提供了文本搜索和获取搜索结果的函数:
- 指定搜索模式和选项,使用
TextSearch.SetPattern
、TextSearch.SetStartPage
(仅对 PDF 文档中的文本搜索有用)、TextSearch.SetEndPage
(仅对 PDF 文档中的文本搜索有用)、和TextSearch.SetSearchFlags
接口。 - 进行搜索,使用
TextSearch.FindNext
和TextSearch.FindPrev
接口。 - 获取搜索结果,使用
TextSearch.GetMatchXXX()
接口。
如何在PDF文档中搜索指定的文本模型
js
import { FoxitRDKNative } from 'foxit_rdk';
class TextSearchUnit {
private searchText() {
try {
const pdfpath = "XXX/Sample.pdf";
const doc = new FoxitRDKNative.pdf.PDFDoc(pdfpath);
doc.Load('');
class SearchCancelCallbackImpl extends FoxitRDKNative.pdf.SearchCancelCallback {
NeedToCancelNow(): boolean {
return false;
}
}
// 创建一个 text search handler
const textSearch =
new FoxitRDKNative.pdf.TextSearch(doc, new SearchCancelCallbackImpl(), FoxitRDKNative.pdf.TextPage.e_ParseTextNormal);
// 设置搜索开始的起始页索引。默认情况下,结束页将是最后一页
textSearch.SetStartPage(0);
// 设置要搜索的文本
textSearch.SetPattern("foxit");
// 设置搜索标志以匹配大小写和整个单词。
textSearch.SetSearchFlags(FoxitRDKNative.pdf.TextSearch.e_SearchMatchCase |
FoxitRDKNative.pdf.TextSearch.e_SearchMatchWholeWord);
while (textSearch.FindNext()) {
// 如果为true,则找到了一个匹配项
// 获取找到的匹配项的页面索引
const pageIndx = textSearch.GetMatchPageIndex();
// 获取找到的匹配项文本的起始字符索引
const startCharIndex = textSearch.GetMatchStartCharIndex();
// 获取找到的匹配项文本的结束字符索引
const endCharIndex = textSearch.GetMatchEndCharIndex();
// 获取找到的匹配项文本的矩形区域
const matchRects: FoxitRDKNative.common.fxcrt.RectFArray = textSearch.GetMatchRects();
}
} catch (e) {
console.error(e);
}
}
}