【发布时间】:2019-12-27 22:52:59
【问题描述】:
我正在构建一个应用程序,用于从 PDF 的特定区域提取文本(即来自遵循固定布局的 PDF 中的客户端名称或客户端 ID)。为此,我让用户用鼠标选择该区域,然后应用程序将该 sn-p 带走并在其上应用 OCR。
虽然结果令人满意,但并非 100% 可靠。
考虑到这一点,有没有更好的方法来做到这一点?
我使用 iText 在网络上搜索了解决方案,但大多数使用已弃用 (iTextSharp) 或过时版本的库。
从 cmets 中,我得到了这段代码:
static string ReadPDF()
{
string path = "PATH TO FILE";
//Coordinates for the rectangle that contains the person's name written on the PDF document
int[] upper_left = {118, 116};
int[] lower_right = {582, 135};
int width = lower_right[0] - upper_left[0];
int height = lower_right[1] - upper_left[1];
PdfDocument doc = new PdfDocument(new PdfReader(path));
Rectangle rect = new Rectangle(upper_left[0], upper_left[1], width, height);
FilteredTextEventListener filterListener = new FilteredTextEventListener(new LocationTextExtractionStrategy(), new TextRegionEventFilter(rect));
return PdfTextExtractor.GetTextFromPage(doc.GetPage(1), filterListener);
}
坐标是我用来使用 OCR 从文件中提取客户姓名的坐标,这很有效。我不知道上面的代码可能有什么问题。
【问题讨论】:
-
“我使用 iText 在 Web 上搜索了解决方案,但大多数使用的是已弃用 (iTextSharp) 或过时版本的库。” - 你想要哪个版本的 iText采用?当前的 7.1.x?最新的 5.5.x?
-
对不起,我忘了指出这一点。我正在使用当前的 7.1.7
-
谢谢!我从该示例中提取了我认为重要的行并创建了一个方法,我在上面发布了该方法。遗憾的是它不起作用。
-
“坐标是我用 OCR 从文件中提取客户姓名的坐标,这很有效。” - 问题是坐标:PDF 使用坐标系原点(通常)在 bottom 左角,y 坐标向上增加,而对于位图图像,您通常有一个坐标系,原点在左上角,而y 向下增加。此外,位图图像坐标通常以像素为单位,而 PDF 以点为单位(1 pt = 1/72 in)。因此,您必须转换坐标。