当前位置: 移动技术网 > IT编程>开发语言>c# > C#编程读取文档Doc、Docx及Pdf内容的方法

C#编程读取文档Doc、Docx及Pdf内容的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#编程读取文档doc、docx及pdf内容的方法。分享给大家供大家参考。具体分析如下: doc文档:microsoft word 14.0 object

本文实例讲述了c#编程读取文档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

/*
 作者: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#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网