首页 > 解决方案 > 使用 bytescout PDFExtractor C# 查找文本坐标

问题描述

我有一个 PDF,需要查找和替换一些文本。我知道如何创建叠加层和添加文本,但我无法确定如何定位当前文本坐标。这是我在 bytescout 网站上找到的示例 -

        // Create Bytescout.PDFExtractor.TextExtractor instance
        TextExtractor extractor = new TextExtractor();
        extractor.RegistrationName = "";
        extractor.RegistrationKey = "";

        /////find text
        // Load sample PDF document
        extractor.LoadDocumentFromFile(@"myPdf.pdf");

        int pageCount = extractor.GetPageCount();
        RectangleF location;

        for (int i = 0; i < pageCount; i++)
        {
            // Search each page for string
            if (extractor.Find(i, "OPTION 2", false, out location))
            {
                do
                {
                    Console.WriteLine("Found on page " + i + " at location " + location.ToString());

                }
                while (extractor.FindNext(out location));
            }
        }
        Console.WriteLine();
        Console.WriteLine("Press any key to continue...");
        Console.ReadLine();
    }
} 

但它不起作用,因为没有采用 4 个参数的重载 Find 方法。我不喜欢使用 Bytescout 从 pdf 中查找文本坐标,但我的公司有许可证。如果 Bytescout 无法完成我想要做的事情,是否有一种免费的方法可以在 pdf 上查找文本坐标?

标签: c#pdfcoordinatespdf-extraction

解决方案


尝试使用:

extractor.Find(i, "OPTION 2", false).FoundText.Bounds

(来源:https ://cdn.bytescout.com/help/BytescoutPDFExtractorSDK/html/M_Bytescout_PDFExtractor_TextExtractor_Find.htm )

FoundText 属性实现 ISearchResult: https ://cdn.bytescout.com/help/BytescoutPDFExtractorSDK/html/T_Bytescout_PDFExtractor_ISearchResult.htm

它具有以下属性:

公共属性 Bounds:所有搜索结果元素的边界矩形。使用 Elements 或 GetElement(Int32) 获取单个元素的边界。

公共属性 ElementCount:返回单个搜索结果元素的计数。

公共属性 Elements:搜索结果元素(包含在搜索结果中的单个文本对象) 对于 COM/ActiveX,请改用 GetElement(Int32)。

公共属性Height:搜索结果边框的高度。使用 Elements 或 GetElement(Int32) 获取单个元素的边界。

公共属性 Left:搜索结果边界矩形的左坐标。使用 Elements 或 GetElement(Int32) 获取单个元素的边界。

公共属性 PageIndex:包含搜索结果的页面的索引。

公共属性 Text:搜索结果的文本表示形式。使用 Elements 或 GetElement(Int32) 获取单个元素。

公共属性 Top:搜索结果边界矩形的顶部坐标。使用 Elements 或 GetElement(Int32) 获取单个元素的边界。

公共属性 Width:搜索结果边框的宽度。使用 Elements 或 GetElement(Int32) 获取单个元素的边界。


推荐阅读