关于 c#:阅读 PDF 并找到要添加到列表的特定列

Read a PDF and find a specific column to add to a list

那么谁能找到一种方法以编程方式仅读出 .PDF 文件列中的数字?换句话说,是否有可能放下一个 PDF 文件并制作一些可以吸收它的东西,读出一列的所有内容?

列格式如下:

401232111555713


以下代码将使用 iTextSharp 打开任何 PDF 并将其读入字符串:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static string ReadPdfFile(string fileName)
{
    StringBuilder text = new StringBuilder();

    if (File.Exists(fileName))
    {
        PdfReader pdfReader = new PdfReader(fileName);

        for (int page = 1; page <= pdfReader.NumberOfPages; page++)
        {
            ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();

            string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);

            currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
            text.Append(currentText);
        }
        pdfReader.Close();
    }
    return text.ToString();
}

从那里您可以简单地运行一些 REGEX 以使用您布置的模式获取列:

1
2
3
4
5
6
7
string text = ReadPdfFile(@"path\\to\\pdf\\file.pdf");
Regex regex = new Regex(@"(?<number>\\d{15})");
List<string> results = new List<string>();
foreach (Match m in regex.Matches(text))
{
    results.Add(m.Groups["number"].Value);
}


您需要使用一些 PDF 处理库。这是一个关于该主题的讨论的 SO 链接:

用C#阅读PDF