0

If i save a uploaded pdf file it throws a corrupted format msg.Thing is if i remove below code string resumetext = ParsePdf(resumedoc.PostedFile.InputStream); Pdf file is saved perfectly.I can open it. if i add the above code Pdf file is saved as a corrupted format.

I can read the text perfectly in both conditions.Please help me to sort out the issue.

 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 you gritted first calling `FileUp.PostedFile.SaveAs(FilePath)` and only thereafter `string resumetext = ParsePdf(resumedoc.PostedFile.InputStream)`? – mkl Nov 23 '13 at 14:40
  • Not related to your problem, but please see this post explaining why you should never use `Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));` http://stackoverflow.com/a/10191879/231316 – Chris Haas Nov 23 '13 at 16:16

1 Answers1

1

My guess is that by reading the stream you're moving the stream's current position and that's freaking out the SaveAs() which is expecting the position to be at zero. After parsing but before writing try:

FileUp.PostedFile.InputStream.Seek(0, SeekOrigin.Begin);
Chris Haas
  • 53,986
  • 12
  • 141
  • 274