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

作者:欧阳不疯 时间:2023-01-16 06:59:29 

本文实例讲述了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#程序设计有所帮助。

标签:C#,Doc,Docx,Pdf
0
投稿

猜你喜欢

  • spring boot启动加载数据原理分析

    2021-06-16 07:56:15
  • C# 10个常用特性汇总

    2023-03-22 01:04:13
  • Android 10 启动之servicemanager源码解析

    2023-05-16 15:04:53
  • 一文带你学会规则引擎Drools的应用

    2022-04-03 08:40:59
  • Java Base64 加密与解密示例代码

    2022-11-13 15:11:56
  • Java实现微信红包分配规则

    2021-06-16 09:18:07
  • Java简单实现定时器

    2023-07-16 18:10:58
  • Java使用RedisTemplate模糊删除key操作

    2023-06-24 06:45:25
  • Android Activity之间的数据传递方法总结

    2023-05-26 03:43:21
  • java实现网站微信扫码支付

    2023-06-18 18:58:19
  • springboot结合maven实现多模块打包

    2022-01-16 07:13:51
  • C# 给PPT中的图表添加趋势线的方法

    2022-02-27 10:18:40
  • Spring BeanDefinition使用介绍

    2023-11-24 10:29:10
  • java复制文件和java移动文件的示例分享

    2023-09-17 08:18:06
  • 详解Jvm中时区设置方式

    2023-12-09 02:59:19
  • Java NIO Path接口和Files类配合操作文件的实例

    2023-10-20 09:29:01
  • android实现图片反转效果

    2022-09-24 20:48:11
  • 利用Java理解sql的语法(实例讲解)

    2023-02-16 06:18:06
  • 一篇文章带你入门Java Script

    2023-09-12 06:09:24
  • SpringCloud迈向云原生的步骤

    2023-08-21 22:59:46
  • asp之家 软件编程 m.aspxhome.com