Skip to content

文本搜索

福昕 PDF SDK iOS 版通过 Core SDK 提供了文档内文本搜索能力,支持在 PDF 文档中查找指定内容,并获取匹配结果的页码、字符范围和矩形区域。

UI Extensions 内置搜索

如果您使用的是完整阅读器(UI Extensions),文本搜索功能已内置。用户可通过顶部工具栏的搜索按钮打开搜索面板,支持关键字输入、结果高亮和前后导航,无需额外编码。

核心类

说明
FSTextSearch文本搜索引擎,支持从 FSPDFDoc 构造

搜索标志常量

常量说明
FSTextSearchSearchNormal默认模式,不区分大小写
FSTextSearchSearchMatchCase区分大小写
FSTextSearchSearchMatchWholeWord全词匹配
FSTextSearchSearchConsecutive连续搜索

示例:在文档中搜索文本

objc
#import <FoxitRDK/FSPDFObjC.h>

FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:@"path/to/Sample.pdf"];
[doc load:nil];

FSTextSearch *textSearch = [[FSTextSearch alloc] initWithDocument:doc
                                                          cancel:nil
                                                           flags:FSTextPageParseTextNormal];
[textSearch setStartPage:0];
[textSearch setEndPage:[doc getPageCount] - 1];
[textSearch setPattern:@"foxit"];
[textSearch setSearchFlags:FSTextSearchSearchMatchCase];

while ([textSearch findNext]) {
    int pageIndex = [textSearch getMatchPageIndex];
    int startChar = [textSearch getMatchStartCharIndex];
    int endChar = [textSearch getMatchEndCharIndex];
    FSRectFArray *rects = [textSearch getMatchRects];
    NSString *sentence = [textSearch getMatchSentence];

    NSLog(@"在第 %d 页找到匹配,字符范围: %d-%d", pageIndex, startChar, endChar);
}

在 UI Extensions 中触发搜索

如果您使用了 UIExtensionsManager,可以通过以下方式编程式打开搜索面板:

objc
[extensionsManager showSearchBar:YES];

API 参考

FSTextSearch 的完整接口说明请参阅 API 手册