A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sunrise2 高级黑马   /  2014-7-21 22:42  /  2003 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 sunrise2 于 2014-7-21 22:44 编辑

       C#解析PDF的方式有很多,比较好用的有ITestSharp和PdfBox
        PDF内容页如果是图片类型,例如扫描件,则需要进行OCR(光学字符识别)。
        文本内容的PDF文档,解析的过程中,我目前仅发现能以字符串的形式读取的,不能够读取其中的表格。据说PDF文档结构中是没有表格概念的,因此这个自然是读不到的,如果果真如此,则PDF中表格内容的解析,只能对获取到的字符串按照一定的逻辑自行解析了。
     ITestSharp是一C#开源项目,PdfBox为Java开源项目,借助于IKVM在.Net平台下有实现。

     Pdf转换Image,使用的是GhostScript,可以以API的方式调用,也可以以Windows命令行的方式调用。
    OCR使用的是Asprise,识别效果较好(商业),另外还可以使用MS的ImageScaning(2007)或OneNote(2010)(需要依赖Office组件),Tessert(HP->Google)(效果很差)。
ITestSharp辅助类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;

  4. using iTextSharp.text.pdf;
  5. using iTextSharp.text.pdf.parser;
  6. using System.IO;

  7. namespace eyuan
  8. {
  9.     public static class ITextSharpHandler
  10.     {
  11.         /// <summary>
  12.         /// 读取PDF文本内容
  13.         /// </summary>
  14.         /// <param name="fileName"></param>
  15.         /// <returns></returns>
  16.         public static string ReadPdf(string fileName)
  17.         {
  18.             if (!File.Exists(fileName))
  19.             {
  20.                 LogHandler.LogWrite(@"指定的PDF文件不存在:" + fileName);
  21.                 return string.Empty;
  22.             }
  23.             //
  24.             string fileContent = string.Empty;
  25.             StringBuilder sbFileContent = new StringBuilder();
  26.             //打开文件
  27.             PdfReader reader = null;
  28.             try
  29.             {
  30.                 reader = new PdfReader(fileName);
  31.             }
  32.             catch (Exception ex)
  33.             {
  34.                 LogHandler.LogWrite(string.Format(@"加载PDF文件{0}失败,错误:{1}", new string[] { fileName, ex.ToString() }));

  35.                 if (reader != null)
  36.                 {
  37.                     reader.Close();
  38.                     reader = null;
  39.                 }

  40.                 return string.Empty;
  41.             }

  42.             try
  43.             {
  44.                 //循环各页(索引从1开始)
  45.                 for (int i = 1; i <= reader.NumberOfPages; i++)
  46.                 {
  47.                     sbFileContent.AppendLine(PdfTextExtractor.GetTextFromPage(reader, i));

  48.                 }

  49.             }
  50.             catch (Exception ex)
  51.             {
  52.                 LogHandler.LogWrite(string.Format(@"解析PDF文件{0}失败,错误:{1}", new string[] { fileName, ex.ToString() }));

  53.             }
  54.             finally
  55.             {
  56.                 if (reader != null)
  57.                 {
  58.                     reader.Close();
  59.                     reader = null;
  60.                 }
  61.             }
  62.             //
  63.             fileContent = sbFileContent.ToString();
  64.             return fileContent;
  65.         }
  66.         /// <summary>
  67.         /// 获取PDF页数
  68.         /// </summary>
  69.         /// <param name="fileName"></param>
  70.         /// <returns></returns>
  71.         public static int GetPdfPageCount(string fileName)
  72.         {
  73.             if (!File.Exists(fileName))
  74.             {
  75.                 LogHandler.LogWrite(@"指定的PDF文件不存在:" + fileName);
  76.                 return -1;
  77.             }
  78.             //打开文件
  79.             PdfReader reader = null;
  80.             try
  81.             {
  82.                 reader = new PdfReader(fileName);
  83.             }
  84.             catch (Exception ex)
  85.             {
  86.                 LogHandler.LogWrite(string.Format(@"加载PDF文件{0}失败,错误:{1}", new string[] { fileName, ex.ToString() }));

  87.                 if (reader != null)
  88.                 {
  89.                     reader.Close();
  90.                     reader = null;
  91.                 }

  92.                 return -1;
  93.             }
  94.             //
  95.             return reader.NumberOfPages;
  96.         }
  97.     }
  98. }
复制代码

PDFBox辅助类
  1. using org.pdfbox.pdmodel;
  2. using org.pdfbox.util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;

  7. namespace eyuan
  8. {
  9.     public static class PdfBoxHandler
  10.     {
  11.         /// <summary>
  12.         /// 使用PDFBox组件进行解析
  13.         /// </summary>
  14.         /// <param name="input">PDF文件路径</param>
  15.         /// <returns>PDF文本内容</returns>
  16.         public static string ReadPdf(string input)
  17.         {
  18.             if (!File.Exists(input))
  19.             {
  20.                 LogHandler.LogWrite(@"指定的PDF文件不存在:" + input);
  21.                 return null;
  22.             }
  23.             else
  24.             {
  25.                 PDDocument pdfdoc = null;
  26.                 string strPDFText = null;
  27.                 PDFTextStripper stripper = null;

  28.                 try
  29.                 {
  30.                     //加载PDF文件
  31.                     pdfdoc = PDDocument.load(input);
  32.                 }
  33.                 catch (Exception ex)
  34.                 {
  35.                     LogHandler.LogWrite(string.Format(@"加载PDF文件{0}失败,错误:{1}", new string[] { input, ex.ToString() }));

  36.                     if (pdfdoc != null)
  37.                     {
  38.                         pdfdoc.close();
  39.                         pdfdoc = null;
  40.                     }

  41.                     return null;
  42.                 }

  43.                 try
  44.                 {
  45.                     //解析PDF文件
  46.                     stripper = new PDFTextStripper();
  47.                     strPDFText = stripper.getText(pdfdoc);

  48.                   

  49.                 }
  50.                 catch (Exception ex)
  51.                 {
  52.                     LogHandler.LogWrite(string.Format(@"解析PDF文件{0}失败,错误:{1}", new string[] { input, ex.ToString() }));

  53.                 }
  54.                 finally
  55.                 {
  56.                     if (pdfdoc != null)
  57.                     {
  58.                         pdfdoc.close();
  59.                         pdfdoc = null;
  60.                     }
  61.                 }

  62.                 return strPDFText;
  63.             }

  64.         }
  65.     }
  66. }
复制代码
另外附上PDF转Image,然后对Image进行OCR的代码。


调用示例
  1. 1 GhostscriptHandler ghostscriptHandler = new GhostscriptHandler();
  2. 2                         string tempJpgFileName = string.Format(GhostScriptImageName, Guid.NewGuid().ToString());
  3. 3                         int pdfPageCount = ITextSharpHandler.GetPdfPageCount(fileName);
  4.                          ghostscriptHandler.Convert(fileName, tempJpgFileName, 1, pdfPageCount, "jpeg", 100, 100);
  5.                         fileContent = AspriseOCRHandler.ReadImage(fileName);
复制代码




1 个回复

倒序浏览
谢谢分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马