当前位置: 首页 > 编程笔记 >

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

丌官凯康
2023-03-14
本文向大家介绍C#编程读取文档Doc、Docx及Pdf内容的方法,包括了C#编程读取文档Doc、Docx及Pdf内容的方法的使用技巧和注意事项,需要的朋友参考一下

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

 类似资料:
  • 我尝试了不同的方法,如iframe、object,在网页上作为预览查看/显示文档。iFrame和google文档仅适用于公共可用的文档。如何显示/预览本地主机文件夹中存储的文档。

  • 本文向大家介绍C#使用iTextSharp从PDF文档获取内容的方法,包括了C#使用iTextSharp从PDF文档获取内容的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#使用iTextSharp从PDF文档获取内容的方法。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的C#程序设计有所帮助。

  • 我正在编写一个java代码,它利用Apache-poi读取ms-office.doc文件,利用itext jar API创建并写入pdf文件。我已经阅读了.doc文件中打印的文本和表格。现在我正在寻找一个读取文档中写入的图像的解决方案。我已经编写了如下代码来读取文档文件中的图像。为什么这段代码不起作用。 存在的问题是:1。条件if(Picture.HasPicture(run))不满足,但文档具有

  • 本文向大家介绍Python解析并读取PDF文件内容的方法,包括了Python解析并读取PDF文件内容的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Python解析并读取PDF文件内容的方法。分享给大家供大家参考,具体如下: 一、问题描述 利用python,去读取pdf文本内容。 二、效果 三、运行环境 python2.7 四、需要安装的库 五、实现源代码 代码1(win64) 代码