Skip to content
DemoAPI 参考文档FAQ

PDF 生成模板编辑器

开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。

查看 GitHub

基本搜索

ComPDFKit 使用 CPDFTextSearcher 类进行文字搜索。搜索过程分为三个步骤:开始搜索、获取下一个结果、结束搜索。

搜索选项

选项说明
C_Search_Options.Search_Case_Sensitive大小写敏感
C_Search_Options.Search_Match_Whole_Word全词匹配

执行搜索

以下是在文档中搜索文本的示例代码:

C#
CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFTextSearcher searcher = new CPDFTextSearcher();

// 遍历每一页进行搜索
for (int pageIndex = 0; pageIndex < document.PageCount; pageIndex++)
{
    CPDFPage page = document.PageAtIndex(pageIndex);
    CPDFTextPage textPage = page.GetTextPage();

    // 开始搜索
    searcher.FindStart(textPage, "关键词", C_Search_Options.Search_Case_Sensitive, 0);

    // 获取搜索结果
    CRect resultRect = new CRect();
    string resultText = "";
    int resultPageIndex = 0;
    List<CPoint> textPointList = new List<CPoint>();

    while (searcher.FindNext(page, textPage, ref resultRect, ref resultText, ref resultPageIndex, ref textPointList))
    {
        Console.WriteLine($"在第 {pageIndex + 1} 页找到: {resultText}");
        Console.WriteLine($"位置: {resultRect}");
    }

    // 结束搜索
    searcher.FindClose();
}