使用PdfPig时遇CS1501错误:GetBlocks方法无匹配重载
解决PdfPig中CS1501:GetBlocks方法无匹配重载的问题
你遇到的CS1501错误,核心原因是当前使用的PdfPig版本和官方Wiki示例的版本不匹配——Wiki里的GetBlocks双参数重载是新版本新增的,而你本地环境的旧版本只支持传入单词列表这一个参数。
办法一:升级PdfPig版本
通过NuGet包管理器将UglyToad.PdfPig更新到最新稳定版,或者和Wiki示例对应的版本(可查看Wiki页面更新记录匹配对应版本号)。升级后就能直接使用你参考的双参数写法。
办法二:适配旧版本的写法
如果暂时无法升级,可以通过设置DocstrumBoundingBoxes实例的属性来配置参数,替代传入Options对象的方式,修改后的代码如下:
using UglyToad.PdfPig; using UglyToad.PdfPig.Content; using UglyToad.PdfPig.DocumentLayoutAnalysis.PageSegmenter; using UglyToad.PdfPig.DocumentLayoutAnalysis.WordExtractor; using (PdfDocument document = PdfDocument.Open(@"C:\Temp\file.pdf")) { for (var i = 0; i < document.NumberOfPages; i++) { var page = document.GetPage(i + 1); var words = page.GetWords(); // 先获取实例并设置配置参数 var docstrumSegmenter = DocstrumBoundingBoxes.Instance; docstrumSegmenter.WithinLineBounds = new DocstrumBoundingBoxes.AngleBounds(-45, 45); docstrumSegmenter.BetweenLineBounds = new DocstrumBoundingBoxes.AngleBounds(35, 170); docstrumSegmenter.BetweenLineMultiplier = 1.5; // 调用单参数的GetBlocks方法 var blocks = docstrumSegmenter.GetBlocks(words); foreach (var block in blocks) { // Do something } } }
内容的提问来源于stack exchange,提问作者Asher
相关产品推荐
相关产品推荐

