How can I extract text from pdf or word files (remove bold, images, and other rich text formatting media) in C#?
Thanks.
How can I extract text from pdf or word files (remove bold, images, and other rich text formatting media) in C#?
Thanks.
For PDF did you take a look at TallPDF
Also check this one: http://www.codeproject.com/KB/files/PDF_to_TEXT.aspx
Use Word object model, it's the only reliable way since Word format is not open and vary from version to version.
You have various options.
pdftotext:
Download the XPDF utilities. In the .zip file there are various commandline utilities. One is pdftotext(.exe)
. It can extract all text content from a well-behaving PDF file. Type pdftotext -help
to learn about some if its commandline parameters.
Ghostscript:
Install the latest version of Ghostscript (v.8.71). Ghostscript is a PostScript- and PDF-interpreter. You can use it to extract text from a PDF as well:
gswin32c.exe ^
-q ^
-sFONTPATH=c:/windows/fonts ^
-dNODISPLAY ^
-dSAFER ^
-dDELAYBIND ^
-dWRITESYSTEMDICT ^
-dSIMPLE ^
-f ps2ascii.ps ^
-dFirstPage=3 ^
-dLastPage=7 ^
input.pdf ^
-dQUIET
This will output text contained on pages 3-7 of input.pdf
to stdout. You can redirect this to a file by appending > /path/to/output.txt
to the command. (Check to make sure that the PostScript utility program ps2ascii.ps
is present in your Ghostscript's lib
subdirectory.)
If you omit the -dSIMPLE
parameter, the text output will be guessing line breaks and word spacings. For details look at the comments inside the ps2ascii.ps
file itself. You can even replace that param with -dCOMPLEX
for gaining additional text formatting info.
You might want to look at PDFBox. Here is a link to a Code Project page showing you how to use it in C# as well as other useful comments.
http://www.codeproject.com/KB/string/pdf2text.aspx
As for Word the suggestion of using the Word Object model is probably the most accurate.
You can use the filters designed for / used by the indexing service. They're designed to extract the plain text out of various documents, which is useful for searching inside a document. You can use it for Office files, PDFs, HTML and so on, basically any file type that has a filter. The only downside is that you have to install these filters on the server, so if you don't have direct access to the server this may not be possible. Some filters come pre-installed with Windows, but some, like PDF, you have to install yourself. For a C# implementation check out this article: Using IFilter in C#