0
如果我保存上傳PDF文件,它拋出一個損壞的格式msg.Thing是,如果我下面的代碼串 = resumetext ParsePdf(resumedoc.PostedFile.InputStream)除去 ; Pdf文件保存完好。我可以打開它。 如果我添加上面的代碼Pdf文件被保存爲損壞的格式。iTextSharp的PDF文本提取
我可以在兩種情況下完美地閱讀文本。請幫助我理清這個問題。
string ext = System.IO.Path.GetExtension(FileUp.PostedFile.FileName);
if (ext == ".pdf")
{
//resumedoc is input type='file'
string resumetext = ParsePdf(resumedoc.PostedFile.InputStream);
string Filename = "dddd" + ext;
string FilePath = "E:\\temp\\" + Filename;
FileUp.PostedFile.SaveAs(FilePath);//Problem arises only here
Response.Write("Uploaded");
}
public string ParsePdf(Stream inFileName)
{
StringBuilder text = new StringBuilder();
PdfReader pdfReader = new PdfReader(inFileName);
for (int page = 1; page <= pdfReader.NumberOfPages; page++)
{
try
{
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
//ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
text.Append(System.Environment.NewLine);
currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
text.Append(currentText);
}
catch { }
}
return text.ToString();
}
HABE你咬第一調用'FileUp.PostedFile.SaveAs(文件路徑)'和僅在其後'字符串resumetext = ParsePdf(resumedoc.PostedFile.InputStream)'? – mkl
與你的問題無關,但請看這篇文章解釋爲什麼你不應該使用'Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default,Encoding.UTF8,Encoding.Default.GetBytes(currentText)));'http ://stackoverflow.com/a/10191879/231316 –