Programmatically reading the contents of Word documents and PDFs in C #

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.