Recent projects need to implement a feature: Read Doc,docx,pdf file content. On the internet for a long time, or found that some good things can be used directly, otherwise you have to invent the wheel. Next I will briefly describe what components are used to implement this functionality.
Doc Document: Microsoft Word 14.0 the object Library (GAC object, which requires you to install Word before calling.) Different versions of the installed version of Word, COM version number will also be different)
Docx Document: Microsoft Word 14.0 the object Library (GAC object, which you need to install Word before calling.) Different versions of the installed version of Word, COM version number will also be different)
PDF Document: PDFBox
DEMO
/* Author: ghostbear * Blog Address: 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 mi
Ssing, 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+) "," ""); }
}
}
Summary
If you need to run the code on IIS, you need to configure the DCOM configuration for the component Microsoft Word 14.0 Object Library. Specific details can be referenced in the article: DCOM configuration for the word component.
Code Download: http://download.csdn.net/detail/ghostbear/4847887