编程读取文档Doc,Docx,Pdf的内容

19 篇文章 0 订阅

    最近项目需要实现一个功能:读取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配置

 

代码下载

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值