您的位置:首页 > 编程语言

编程读取文档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

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配置

代码下载

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: