我正在一個項目上工作,我需要從文件夾中讀取多個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);
爲什麼你需要行''字符串內容= File.ReadAllText();''呢? ''strPdfContent''已經有內容了!!? –
你期望'File.ReadAllText'做什麼?它不會給你PDF文件的文本內容。 PDF是一種需要解釋的特殊格式。 –
幾乎看起來像沒有任何理由進入這兩條線,他們只是錯了,什麼也沒做......'string pdfText = strPdfContent.ToString();字符串內容= File.ReadAllText(strPDFFile);'' –