2017-07-07 59 views
0

我正在一個項目上工作,我需要從文件夾中讀取多個pdf文件,並在點擊按鈕時顯示其內容。 我正面臨着一次讀取多個文件的問題。我怎麼能讀多個PDF文件?有人幫助我嗎?如何從asp.net中的文件夾中讀取多個pdf文件?

protected void btnShowContent_Click(object sender, EventArgs e) 
      { 
       //if (fileUpload.HasFile) 
       //{ 

        foreach (string file in Directory.GetFiles(@"E:\\Rida\","*.pdf")) 
        { 
        string str = ""; 
        str = str + ", " + file.ToString(); 
        PdfReader reader = new PdfReader(file); 
         string strPDFFile = file.ToString().Trim(); 
         StringBuilder strPdfContent = new StringBuilder(); 
        string pdfText = strPdfContent.ToString(); 
         string contents = File.ReadAllText(strPDFFile); 

         for (int i = 1; i <= reader.NumberOfPages; i++) 
         { 
          ITextExtractionStrategy objExtractStrategy = new SimpleTextExtractionStrategy(); 
          string strLineText = PdfTextExtractor.GetTextFromPage(reader, i, objExtractStrategy); 
          strLineText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strLineText))); 
          strPdfContent.Append(strLineText); 
          strPdfContent.Append(contents); 

          strPdfContent.Append("<br/>"); 
         } 
        reader.Close(); 
        lblPdfContent.Text = strPdfContent.ToString(); 
        }    
      } 

此行將我的pdf文件內容轉換爲特殊字符。我該怎麼做才能避免這種轉換。?

string contents = File.ReadAllText(strPDFFile); 
+0

爲什麼你需要行''字符串內容= File.ReadAllText();''呢? ''strPdfContent''已經有內容了!!? –

+0

你期望'File.ReadAllText'做什麼?它不會給你PDF文件的文本內容。 PDF是一種需要解釋的特殊格式。 –

+0

幾乎看起來像沒有任何理由進入這兩條線,他們只是錯了,什麼也沒做......'string pdfText = strPdfContent.ToString();字符串內容= File.ReadAllText(strPDFFile);'' –

回答

0

據我瞭解PDF解析和操作在.NET環境中,您可以用iTextSharp的,它是一個PDF庫,它允許你創建,調整,檢查和維護PDF文檔。 使用庫,它可以幫助你解決你的問題!

https://sourceforge.net/projects/itextsharp/

http://jadn.co.uk/w/ReadPdfUsingCsharp.htm

+0

我正在使用這個庫..但我面臨的問題是當我運行我的代碼時,它只是從文件夾中讀取第一個文件,並將下一個文件轉換爲一些特殊的字符。 –

+0

已經完成了,使用iTextSharp讀取單個文本文件,您必須一次性讀取多個文件。 閱讀這篇文章可能會有所幫助,因爲PDF是一種特殊的格式,它有點難以處理! https://stackoverflow.com/questions/83152/reading-pdf-documents-in-net –

相關問題