C # Methods of reading various files Notepad and Pdf,word

Source: Internet
Author: User
#region file operations
        /// <summary>
        /// read notepad
        /// </ summary>
        /// <param name = "file_model"> </ param>
        /// <returns> </ returns>
        private HotLine12345.Model.KL_CONTENT_INFO Read_Txt (HotLine12345.Model.KL_FILE file_model)
        {
            HotLine12345.Model.KL_CONTENT_INFO model = new HotLine12345.Model.KL_CONTENT_INFO ();
            model.KL_CONTENT_ID = Convert.ToInt32 (file_model.KL_ID);
            model.KL_CONTENT_TITLE = file_model.File_Name;
            model.KL_FILE = file_model.ID.ToString ();
            #region read notepad
            string inforContent = "";
            string FilePath = Server.MapPath (file_model.File_Path);
            FileInfo file = new FileInfo (FilePath);
            if (file.Exists)
            {
                try
                {
                    FileStream stream = new FileStream (FilePath, FileMode.Open);
                    StreamReader reader = new StreamReader (stream, Encoding.GetEncoding ("gb2312")); // Encoding.GetEncoding ("gb2312"), Encoding.UTF8
                    //reader.ReadLine (); // Read one line at a time
                    inforContent = reader.ReadToEnd (); // Read all data at once
                    reader.Close ();
                    stream.Close ();
                }
                catch (Exception ce)
                {
                    throw ce;
                }
            }
            #endregion
            model.KL_CONTENT = inforContent;
            return model;
        }
        /// <summary>
        /// read word document
        /// </ summary>
        /// <param name = "file_model"> </ param>
        /// <returns> </ returns>
        private HotLine12345.Model.KL_CONTENT_INFO Read_Word (HotLine12345.Model.KL_FILE file_model)
        {
            HotLine12345.Model.KL_CONTENT_INFO model = new HotLine12345.Model.KL_CONTENT_INFO ();
            model.KL_CONTENT_ID = Convert.ToInt32 (file_model.KL_ID);
            model.KL_CONTENT_TITLE = file_model.File_Name;
            model.KL_FILE = file_model.ID.ToString ();
            #region Reading word documents
            string inforContent = "";
            string FilePath = Server.MapPath (file_model.File_Path);
            FileInfo file = new FileInfo (FilePath);
            if (file.Exists)
            {
                try
                {
                    Word.Application app = new Microsoft.Office.Interop.Word.Application ();
                    Word.Document doc = null;
                    object unknow = Type.Missing;
                    app.Visible = false;
                    app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
                    object file2 = FilePath;
                    doc = app.Documents.Open (ref file2, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
                    inforContent = doc.Content.Text;
                    // Close the file
                    doc.Close (ref unknow, ref unknow, ref unknow);
                    // Close COM
                    app.Quit (ref unknow, ref unknow, ref unknow);

                }
                catch (Exception ce)
                {
                    throw ce;
                }
            }
            #endregion
            model.KL_CONTENT = inforContent;
            return model;
        }
        /// <summary>
        /// read PDF content
        /// </ summary>
        /// <param name = "file_model"> </ param>
        /// <returns> </ returns>
        private HotLine12345.Model.KL_CONTENT_INFO Read_PDF (HotLine12345.Model.KL_FILE file_model)
        {
            HotLine12345.Model.KL_CONTENT_INFO model = new HotLine12345.Model.KL_CONTENT_INFO ();
            model.KL_CONTENT_ID = Convert.ToInt32 (file_model.KL_ID);
            model.KL_CONTENT_TITLE = file_model.File_Name;
            model.KL_FILE = file_model.ID.ToString ();
            #region Reading PDF content
            string inforContent = "";
            string FilePath = Server.MapPath (file_model.File_Path);
            string txtPath = IndexDic + "\\" + Path.GetFileNameWithoutExtension (FilePath) + ".txt";
            FileInfo file = new FileInfo (FilePath);
            if (file.Exists)
            {
                try
                {
                    PDDocument doc = PDDocument.load (FilePath);
                    PDFTextStripper pdfStripper = new PDFTextStripper ();
                    inforContent = pdfStripper.getText (doc);
                    doc.close ();
                    //

                }
                catch (Exception ce)
                {
                    throw ce;
                }
            }
            #endregion
            model.KL_CONTENT = inforContent;
            return model;
        }
        #endregion
<span style = "color: # cc0000; background-color: rgb (255, 255, 255);"> pdf Please download the relevant components
PDFBox-0.7.3.dll
IKVM.Runtime.dll
IKVM.GNU.Classpath.dll
FontBox-0.1.0-dev.dll </ span> 

Related Article

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.