C# 使用iTextSharp解析PDF,然后将特定文本提取到屏幕上

C# 使用iTextSharp解析PDF,然后将特定文本提取到屏幕上,c#,parsing,pdf,itext,C#,Parsing,Pdf,Itext,因此,我试图从PDF文件中提取某些内容。所以这是一张发票,我希望能够在PDF文件中搜索“发票号码”,然后搜索“名字”,并在 Console.WriteLine() 所以现在这就是我所得到的,我需要弄清楚如何走得更远 using iTextSharp.text.pdf; using System.IO; using iTextSharp.text.pdf.parser; using System; namespace PdfProperties { class Program {

因此,我试图从PDF文件中提取某些内容。所以这是一张发票,我希望能够在PDF文件中搜索“发票号码”,然后搜索“名字”,并在

Console.WriteLine()

所以现在这就是我所得到的,我需要弄清楚如何走得更远

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

namespace PdfProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfReader reader = new PdfReader("C:/PDF/invoiceDetail.pdf");
            PdfReaderContentParser parser = new PdfReaderContentParser(reader);
            FileStream fs = new FileStream("C:/PDF/result0.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            SimpleTextExtractionStrategy strategy;

            string text = "";

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                strategy = parser.ProcessContent(i, new SimpleTextExtractionStrategy());
                sw.WriteLine(strategy.GetResultantText());

                text = strategy.GetResultantText();

                String[] splitText = text.Split(new char[] {'.' });

                Console.WriteLine("Test");

                Console.WriteLine(text);
            }
            sw.Flush();
            sw.Close();

        }
    }
}
使用iTextSharp.text.pdf;
使用System.IO;
使用iTextSharp.text.pdf.parser;
使用制度;
命名空间PDFProperty
{
班级计划
{
静态void Main(字符串[]参数)
{
PdfReader reader=新的PdfReader(“C:/PDF/invoiceDetail.PDF”);
PdfReaderContentParser=新的PdfReaderContentParser(读取器);
FileStream fs=newfilestream(“C:/PDF/result0.txt”,FileMode.Create);
StreamWriter sw=新StreamWriter(fs);
简单外延战略;
字符串文本=”;
对于(inti=1;iHy
你可以试试这个:

String[] splitText = text.Split(".");
for(int i =0; i<splitText.Lenght;i++)
{
if(splitText[i].toString() =="Invoice Number:")
(
  // we have Invoice Number

 // now we search for First Name
   if(splitText[i].toString() == "First Name")
   (
     // now we have also First Name
   ) 
) 
}
String[]splitText=text.Split(“.”);

对于(int i=0;i有两种方法:

  • 您可以尝试自己处理发票。这意味着处理结构和边缘案例。如果内容不总是以相同的方式对齐会怎样?如果发票模板发生变化会怎样?如果发票中的某些文本是可变的,而您不能真正依赖提取的精确文本会怎样

    简言之,这不是一个需要解决的小问题

  • 使用pdf2Data。它专门设计用于处理结构丰富的文档。例如发票。它使用一个称为“选择器”的概念,允许您定义某些内容的位置。可以通过位置(坐标定义的矩形中的某个位置)或结构块(此表中的行..)等

    即使加载项是封闭源代码的,您也可以使用试用许可证进行试用。在评估pdf2Data后,您至少可以对解决此问题的方法做出更明智的决定

    查看更多信息


  • 您的控制台输出是否包含所需的发票号和名字?您需要pdf2Data加载项来完成此操作。该加载项是封闭源代码(其中之一是因为太多人认为他们可以在商业环境中免费使用iText)。有关pdf2Data的更多信息,请参阅。