[C#] C# 用 iTextSharp 将 PDF 转成文本 →→→→→进入此内容的聊天室

来自 , 2020-12-23, 写在 C#, 查看 159 次.
URL http://www.code666.cn/view/a75a52f7
  1. using System;
  2. using System.IO;
  3. using iTextSharp.text;
  4. using iTextSharp.text.pdf;
  5. using iTextSharp.text.pdf.parser;
  6.  
  7. public class ParsingPDF {
  8.  
  9.     static string PDF;
  10.     static string TEXT2;
  11.  
  12.     /**
  13.      * Parses the PDF using PRTokeniser
  14.      * @param src  the path to the original PDF file
  15.      * @param dest the path to the resulting text file
  16.      */
  17.     public void parsePdf(String src, String dest)
  18.     {
  19.         PdfReader reader = new PdfReader(src);
  20.         StreamWriter output = new StreamWriter(new FileStream(dest, FileMode.Create));
  21.         int pageCount = reader.NumberOfPages;
  22.         for (int pg = 1; pg <= pageCount; pg++)
  23.         {
  24.             // we can inspect the syntax of the imported page
  25.             byte[] streamBytes = reader.GetPageContent(pg);
  26.             PRTokeniser tokenizer = new PRTokeniser(streamBytes);
  27.             while (tokenizer.NextToken())
  28.             {
  29.                 if (tokenizer.TokenType == PRTokeniser.TokType.STRING)
  30.                 {
  31.                     output.WriteLine(tokenizer.StringValue);
  32.                 }
  33.             }
  34.         }
  35.         output.Flush();
  36.         output.Close();
  37.     }
  38.  
  39.     /**
  40.      * Main method.
  41.      */
  42.     static void Main(string[] args)
  43.     {
  44.         if (args.Length < 1 || args.Length > 2)
  45.         {
  46.             Console.WriteLine("USAGE: ParsePDF infile.pdf <outfile.txt>");
  47.             return;
  48.         }
  49.         else if (args.Length == 1)
  50.         {
  51.             PDF = args[0];
  52.             TEXT2 = Path.GetFileNameWithoutExtension(PDF) + ".txt";
  53.         }
  54.         else
  55.         {
  56.             PDF = args[0];
  57.             TEXT2 = args[1];
  58.         }
  59.  
  60.         try
  61.         {
  62.             DateTime t1 = DateTime.Now;
  63.  
  64.             ParsingPDF example = new ParsingPDF();
  65.             example.parsePdf(PDF, TEXT2);
  66.  
  67.             DateTime t2 = DateTime.Now;
  68.             TimeSpan ts = t2 - t1;
  69.             Console.WriteLine("Parsing completed in {0:0.00} seconds.", ts.TotalSeconds);
  70.         }
  71.         catch (Exception ex)
  72.         {
  73.             Console.WriteLine("ERROR: " + ex.Message);
  74.         }
  75.     } // class
  76.  
  77.     public class MyTextRenderListener : IRenderListener
  78.     {
  79.         /** The print writer to which the information will be written. */
  80.         protected StreamWriter output;
  81.  
  82.         /**
  83.          * Creates a RenderListener that will look for text.
  84.          */
  85.         public MyTextRenderListener(StreamWriter output)
  86.         {
  87.             this.output = output;
  88.         }
  89.  
  90.         public void BeginTextBlock()
  91.         {
  92.             output.Write("<");
  93.         }
  94.  
  95.         public void EndTextBlock()
  96.         {
  97.             output.WriteLine(">");
  98.         }
  99.  
  100.         public void RenderImage(ImageRenderInfo renderInfo)
  101.         {
  102.         }
  103.  
  104.         public void RenderText(TextRenderInfo renderInfo)
  105.         {
  106.             output.Write("<");
  107.             output.Write(renderInfo.GetText());
  108.             output.Write(">");
  109.         }
  110.     } // class
  111. } // namespace  
  112. //csharp/5531

回复 "C# 用 iTextSharp 将 PDF 转成文本"

这儿你可以回复上面这条便签

captcha