C#編程讀取文檔Doc、Docx及Pdf內(nèi)容的方法
本文實(shí)例講述了C#編程讀取文檔Doc、Docx及Pdf內(nèi)容的方法。分享給大家供大家參考。具體分析如下:
Doc文檔:Microsoft Word 14.0 Object Library (GAC對(duì)象,調(diào)用前需要安裝word。安裝的word版本不同,COM的版本號(hào)也會(huì)不同)
Docx文檔:Microsoft Word 14.0 Object Library (GAC對(duì)象,調(diào)用前需要安裝word。安裝的word版本不同,COM的版本號(hào)也會(huì)不同)
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+)", ""); } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
unity3D實(shí)現(xiàn)物體任意角度自旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了unity3D實(shí)現(xiàn)物體任意角度自旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Unity實(shí)現(xiàn)卡片循環(huán)滾動(dòng)效果的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Unity實(shí)現(xiàn)卡片循環(huán)滾動(dòng)的效果,文中的實(shí)現(xiàn)步驟講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-12-12C#實(shí)現(xiàn)對(duì)象XML序列化的方法
這篇文章主要介紹了C#實(shí)現(xiàn)對(duì)象XML序列化的方法,是C#常見的實(shí)用技巧,需要的朋友可以參考下2014-11-11Unity實(shí)現(xiàn)場(chǎng)景漫游相機(jī)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)場(chǎng)景漫游相機(jī),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10C#中調(diào)用SAPI實(shí)現(xiàn)語音識(shí)別的2種方法
這篇文章主要介紹了C#中調(diào)用SAPI實(shí)現(xiàn)語音識(shí)別的2種方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06C# HttpClient上傳文件并附帶其它參數(shù)方式
這篇文章主要介紹了C# HttpClient上傳文件并附帶其它參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11