【问题标题】:Extract value from PDF file to variable从 PDF 文件中提取值到变量
【发布时间】:2020-02-08 12:15:01
【问题描述】:

我正在尝试从 PDF 文件中获取“发票编号”,在本例中为 INV-3337,并希望将其存储为变量以供将来在代码中使用。

目前我正在研究示例并将此 PDF 用于测试目的: https://slicedinvoices.com/pdf/wordpress-pdf-invoice-plugin-sample.pdf

使用我当前的代码,我能够将整个内容解析为 .txt 格式。有人可以指导我如何获得只需要的值并将其存储到变量中吗?可以直接用itextsharp完成吗?还是我需要先全部解析为 .txt 文件,然后解析 .txt 文件,将值存储为变量,删除 .txt 文件并继续前进?

注意!在实际设置中会有很多 PDF 文件需要解析。

这是我当前的代码:

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.IO;
using System.Text;


namespace PDF_parser
{
    class Program
    {
        static void Main(string[] args)
        {

            string filePath = @"C:\temp\parser\Invoice_Template.pdf";
            string outPath = @"C:\temp\parser\Invoice_Template.txt";
            int pagesToScan = 2;

            string strText = string.Empty;
            try
            {
                PdfReader reader = new PdfReader(filePath);

                for (int page = 1; page <= pagesToScan; page++) //(int page = 1; page <= reader.NumberOfPages; page++) <- for scanning all the pages in A PDF
                {
                    ITextExtractionStrategy its = new LocationTextExtractionStrategy();
                    strText = PdfTextExtractor.GetTextFromPage(reader, page, its);

                    strText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strText)));
                    //creating the string array and storing the PDF line by line
                    string[] lines = strText.Split('\n');
                    foreach (string line in lines)
                    {
                        //Creating and appending to a text file
                        using (StreamWriter file = new StreamWriter(outPath, true))
                        {
                            file.WriteLine(line);
                        }
                    }
                }

                reader.Close();
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
        }
    }
}

编辑:

我没听错吧?

using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using System;
using System.IO;
using System.Text;


namespace PDF_parser
{
    class Program
    {
        static void Main(string[] args)
        {

            string filePath = @"C:\temp\parser\Invoice_Template.pdf";
            string outPath = @"C:\temp\parser\Invoice_Template.txt";
            int pagesToScan = 2;

            string strText = string.Empty;
            try
            {
                PdfReader reader = new PdfReader(filePath);

                for (int page = 1; page <= pagesToScan; page++) //(int page = 1; page <= reader.NumberOfPages; page++) <- for scanning all the pages in A PDF
                {
                    ITextExtractionStrategy its = new LocationTextExtractionStrategy();
                    strText = PdfTextExtractor.GetTextFromPage(reader, page, its);

                    strText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strText)));
                    //creating the string array and storing the PDF line by line
                    string[] lines = strText.Split('\n');
                    foreach (string line in lines)
                    {
                        //Creating and appending to a text file
                        using (StreamWriter file = new StreamWriter(outPath, true))
                        {
                            // file.WriteLine(line);

                           int indexOccurrance = line.LastIndexOf("Invoice Number");
                           if(indexOccurrance > 0)
                           {
                           var invoiceNumber = line.Substring(indexOccurrance, (line.Length - indexOccurrance) );
                           }
                        }
                    }
                }

                reader.Close();
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
        }
    }
}

【问题讨论】:

  • 您可以在line 中找到Invoice Number 的出现为int indexOccurrance = line.LastIndexOf("Invoice Number")。然后使用substring获取实际值。

标签: c# parsing pdf itext pdf-parsing


【解决方案1】:

一种选择是使用LastIndexOf 在每行文本中搜索"Invoice Number"。 如果找到,则使用Substring 获取该行的其余部分(将是Invoice Number

类似:

int indexOccurrance = line.LastIndexOf("Invoice Number");
if(indexOccurrance > 0)
{
  var invoiceNumber = line.Substring(indexOccurrance, (line.Length - indexOccurrance) );
}

【讨论】:

  • 谢谢你!是在当前过程中完成还是解析成.txt格式后完成?
  • @hatman 取决于您的需要。如果您只在寻找Invoice Number,那么您可以在解析时搜索并在找到后突破。如果您正在寻找更多信息,您可以继续。可能影响您的逻辑的另一件事是该单词的出现次数。如果您希望它出现多次,那么您需要查找前面/后面的锚文本。
  • 好的,我不会写入 txt 填充,而是将其拆分为数组并尝试从那里搜索。由于某种原因,我在编辑部分添加的代码似乎不起作用。所以也许数组搜索方法可以解决问题
  • @hatman 写入输出文件的一个建议。您正在打开文件并为每一行写入文件。您可以使用 (StreamWriter file = new StreamWriter(outPath, true))` about foreach 块,这样文件就不会被多次打开。
猜你喜欢
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 2020-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
相关资源
最近更新 更多