pramod
pramod

Reputation: 373

Create pdf document from System.IO.Stream object?

I have Stream object that contains data that i want to show in PDF document.

How do i create and Save PDF document from that Stream object? (The Stream object is created using MigraDoc tool)

Upvotes: 0

Views: 4597

Answers (1)

Richard
Richard

Reputation: 22016

I think you may be confused. MigraDoc creates Pdf documents. So if you have a stream from MigraDoc (which comes from it's PdfDocument object) I would guess that saving the stream to disk as "document.pdf" would be the best option.

See Jon Skeets answer to this here:

How do I save a stream to a file in C#?

Remember a stream is simply binary data. So when you want to read and write that data you use System.IO to read or write the stream into some location be it disk, memory or a network transmission.

I would take a look at the MigraDoc samples for more info:

http://www.pdfsharp.net/wiki/MigraDocSamples.ashx

if you are writing this to an HTTP output then I would do as follows:

byte[] buffer = new byte[8192];

pdfStream.Seek(0, SeekOrigin.Begin);

Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
Response.ContentType = "application/pdf";

int bytesRead = pdfStream.Read(buffer, 0, 8192);
while(bytesRead > 0)
{
byte[] buffer2 = new byte[bytesRead];
System.Buffer.BlockCopy(buffer, 0, buffer2, 0, bytesRead);

Response.BinaryWrite(buffer2);
Response.Flush();

bytesRead = pdfStream.Read(buffer, 0, 8192);
}
Response.End();

Upvotes: 4

Related Questions