• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

OC/C/C++ 水墨上仙 1395次浏览

Doc文档:Microsoft Word 14.0 Object Library (GAC对象,调用前需要安装word。安装的word版本不同,COM的版本号也会不同)
Docx文档:Microsoft Word 14.0 Object Library (GAC对象,调用前需要安装word。安装的word版本不同,COM的版本号也会不同)
Pdf文档:PDFBox

/*
     作者: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+)", "");
           
        }
    }
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#编程读取文档Doc,Docx,Pdf的内容
喜欢 (0)
加载中……