编程读取文档Doc,Docx,Pdf的内容
2012-12-05 10:06
295 查看
最近项目需要实现一个功能:读取doc,docx,pdf文件内容。在网上搜罗许久,还是发现有些好东西可以直接拿来使用,要不然就得自己发明轮子了。接下来我就简单介绍了用了哪些组件来实现这个功能的。
Doc文档:Microsoft Word 14.0 Object Library (GAC对象,调用前需要安装word。安装的word版本不同,COM的版本号也会不同)
Docx文档:Microsoft Word 14.0 Object Library (GAC对象,调用前需要安装word。安装的word版本不同,COM的版本号也会不同)
Pdf文档:PDFBox
Doc文档:Microsoft Word 14.0 Object Library (GAC对象,调用前需要安装word。安装的word版本不同,COM的版本号也会不同)
Docx文档:Microsoft Word 14.0 Object Library (GAC对象,调用前需要安装word。安装的word版本不同,COM的版本号也会不同)
Pdf文档:PDFBox
DEMO
/* 作者:GhostBear * 博客地址:Http://blog.csdn.net/ghostbear */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Text.RegularExpressions; using org.pdfbox.pdmodel; using org.pdfbox.util; using Microsoft.Office.Interop.Word; namespace TestPdfReader { class Program { static void Main(string[] args) { //PDF PDDocument doc = PDDocument.load(@"C:\resume.pdf"); PDFTextStripper pdfStripper = new PDFTextStripper(); string text = pdfStripper.getText(doc); string result = text.Replace('\t', ' ').Replace('\n', ' ').Replace('\r', ' ').Replace(" ", ""); Console.WriteLine(result); //Doc,Docx object docPath = @"C:\resume.doc"; object docxPath = @"C:\resume.docx"; object missing=System.Reflection.Missing.Value; object readOnly=true; Application wordApp; wordApp = new Application(); Document wordDoc = wordApp.Documents.Open(ref docPath, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); string text2 = FilterString(wordDoc.Content.Text); wordDoc.Close(ref missing, ref missing, ref missing); wordApp.Quit(ref missing, ref missing, ref missing); Console.WriteLine(text2); Console.Read(); } private static string FilterString(string input) { return Regex.Replace(input, @"(\a|\t|\n|\s+)", ""); } } }
小结
如果需要在IIS上运行该代码,则需要配置组件“Microsoft Word 14.0 Object Library”的DCOM配置。具体细节可以参考文章:Word组件的DCOM配置。代码下载
相关文章推荐
- C#编程读取文档Doc、Docx及Pdf内容的方法
- PHP读取doc,docx,xls,pdf,txt内容
- POI解析文档内容(txt,doc,docx,xls,xlsx,ppt,pdf)
- Ubuntu下使用python读取doc和docx文档的内容
- Ubuntu下使用python读取doc和docx文档的内容方法
- 将Doc或者Docx文档处理成html的代码逻辑;统计word中的字数,段数,句数,读取word中文档内容的代码逻辑
- java读取word,excel和pdf文档内容
- C#生成PDF文档,读取TXT文件内容
- Java maven 读取doc,docx文档
- PHP读取docx文档内容
- Java读写docx文档(读取内容并替换其中的关键字)
- java读取word,excel和pdf文档内容
- java读取txt/pdf/xls/xlsx/doc/docx/ppt/pptx
- C#读取PDF文档内容
- 使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现
- doc,docx,pdf,ppt等文件类型读取方法
- 用iTextSharp读取PDF格式文档中的文本内容
- C#生成PDF文档,读取TXT文件内容
- Java程序员从笨鸟到菜鸟之(一百零三)java操作office和pdf文件(一)java读取word,excel和pdf文档内容
- Linux系统中利用node.js提取Word(doc/docx)及PDF文本的内容