6

I am using a third party tool to get the scanned content from the scanner. On button click it executes the code and gives the content as a FileStream. Now I need to save this FileStream content as a pdf file in to a specified folder.

After saving I need to open the file in browser. How can I save the FileStream as a PDF file?

halfer
  • 19,824
  • 17
  • 99
  • 186
ANP
  • 15,287
  • 22
  • 58
  • 79

5 Answers5

2

You can write the stream directly to the output buffer of the response.

So if you're at the point in your code where you have the filestream from the scanner. Simply read bytes from the scanner filestream and write them to the Response.OutputStream

Set the contentType to application/pdf

Make sure you return nothing else. The users browser will do whatever it is configured to do now, either save to disk or show in the browser. You can also save to disk on the server at this point as well in case you wanted a backup.

I'm assuming your file stream is already a pdf, otherwise you'll need to use something like itextsharp to create the pdf.

Edit Here's some rough and ready code to do it. You'll want to tidy this up, like adding exception trapping to make sure the file stream gets cleaned up properly.

    public void SaveToOutput(Stream dataStream)
    {
        dataStream.Seek(0, SeekOrigin.Begin);
        FileStream fileout = File.Create("somepath/file.pdf");

        const int chunk = 512;
        byte[] buffer = new byte[512];

        int bytesread = dataStream.Read(buffer,0,chunk);

        while (bytesread == chunk)
        {
            HttpContext.Current.Response.OutputStream.Write(buffer, 0, chunk);
            fileout.Write(buffer, 0, chunk);
            bytesread = dataStream.Read(buffer, 0, chunk);
        }

        HttpContext.Current.Response.OutputStream.Write(buffer, 0, bytesread);
        fileout.Write(buffer, 0, bytesread);
        fileout.Close();

        HttpContext.Current.Response.ContentType = "application/pdf";
    }

Simon

Simon Halsey
  • 5,459
  • 1
  • 21
  • 32
  • Yes file stream is already a pdf. Can you explain your solution little more. I guess you are giving me the correct solution but I am not able to get it properly. – ANP Jul 22 '11 at 11:02
  • Yes I need to save the content to disk. So how to do this? – ANP Jul 22 '11 at 11:09
1

You might want to take a look at the C# PDF Library on SourceForge: http://sourceforge.net/projects/pdflibrary/

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
1

If I'm understanding you correctly, the third party library is handing you a stream containing the data for the scanned document and you need to write it to a file? If that's the case you need to look up file I/O in C#. Here's a link and an example:

Stream sourceStream = scanner.GetOutput(); // whereever the source stream is
FileStream targetStream = File.OpenWrite(filename, FileMode.Create());
int bytesRead = 0;
byte[] buffer = new byte[2048];
while (true) {
     bytesRead = sourceStream.Read(buffer, 0, buffer.length);
     if (bytesRead == 0)
         break;
     targetStream.Write(buffer, 0, bytesRead);
}
sourceStream.Close();
targetStream.Close();
Phill
  • 1,302
  • 1
  • 9
  • 18
  • Yes it is saving the file. When I open it shows error message that adobe reader could not open this file. – ANP Jul 22 '11 at 10:34
  • This is just saving the stream. But the actual data is not in PDF format. You need to use a separate library to _translate_ that stream to PDF using third party libraries mentioned in the other answers. – Alex R. Jul 24 '11 at 04:44
0

not sure, but maybe check this

http://sourceforge.net/projects/itextsharp/

iTextSharp + FileStream = Corrupt PDF file

Community
  • 1
  • 1
0

Another prominent PDF library (which I have used in the past as well) is iTextSharp. You can take a look at this tutorial on how to convert your Stream to PDF then have the user download it.

Alex R.
  • 4,664
  • 4
  • 30
  • 40