PDF in asp.net

  • Thread starter Thread starter Tem
  • Start date Start date
T

Tem

Hello

how would i go about generating PDF on the fly with asp.net?
i found some itextsharp examples that can create PDF file, but i would like
to write a cutome handler than generates PDFs on the fly.

This is what i have some far

public void ProcessRequest (HttpContext context) {

context.Response.ContentType = "application/pdf";
context.Response.AddHeader("content-disposition", "attachment;
filename=\"document.pdf\"");

Document document = new Document();


PdfWriter.GetInstance(document, not sure how to put here);

document.Open();
document.Add(new Paragraph("hello world"));

document.Close();
}
 
Instead of "not sure how to put here" try

context.Response.OutputStream

Good luck.
 
It's been a long time since I played with iTextSharp, but you have to stream
it to an intermediate source before you can dump it to the browser. If I
remember correctly, I first created a memory stream then passed that to the
GetInstance.

Then, when I was done, I dumped the memory stream to the
Response.BinaryWrite.

If you need more help, I can dig around my old files to see if I have a
sample, I just can't find one right now.
 
Could you look for that sample?


Thank you


Mark Fitzpatrick said:
It's been a long time since I played with iTextSharp, but you have to
stream it to an intermediate source before you can dump it to the browser.
If I remember correctly, I first created a memory stream then passed that
to the GetInstance.

Then, when I was done, I dumped the memory stream to the
Response.BinaryWrite.

If you need more help, I can dig around my old files to see if I have a
sample, I just can't find one right now.
 
The MSDN article about HttpResponse.Flush() method has a great sample on how
to write bitmaps to response's OutputStream. I think all you need to do is
adapt that sample to your needs.

The main idea is that you write your output to some stream. So instead of a
FileStream you use any other stream, in this case OutputStream. It'll need
some tweaking. Perhaps use MemoryStream first, as it has been suggested,
then dump its contents to HttpResponse.OutputStream.

Try to just take an existing PDF file and send it using
Response.TransmitFile().
 
It's been a long time since I played with iTextSharp, but you have to
stream it to an intermediate source before you can dump it to the browser.
If I remember correctly, I first created a memory stream then passed that
to the GetInstance.

Then, when I was done, I dumped the memory stream to the
Response.BinaryWrite.

Siberix pretty much takes care of all of that for you:
http://www.siberix.com
 
MemoryStream memStream = new MemoryStream();

Document document= new Document();

PdfWriter writer = PdfWriter.getInstance(document, memStream );

writer.Open();

document.Open();

// do a bunch of stuff with the document object

document.Close();

writer.Close();

Response.OutputStream.Write(memStream .GetBuffer(), 0, memStream
..GetBuffer().Length);

Response.OutputStream.Flush();

Response.OutputStream.Close();



Of course, this was a while ago so I'm not sure if it works with the newer
releases but it should.

--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression
 
Just my 2 cents:

memStream.GetBuffer().Length may return an incorrect size, since the
internal buffer can grow bigger than its actual contents. memStream.Length
should be used instead, imho...
 
I got it to work

thanks for your help


Tem
Mark Fitzpatrick said:
MemoryStream memStream = new MemoryStream();

Document document= new Document();

PdfWriter writer = PdfWriter.getInstance(document, memStream );

writer.Open();

document.Open();

// do a bunch of stuff with the document object

document.Close();

writer.Close();

Response.OutputStream.Write(memStream .GetBuffer(), 0, memStream
.GetBuffer().Length);

Response.OutputStream.Flush();

Response.OutputStream.Close();



Of course, this was a while ago so I'm not sure if it works with the newer
releases but it should.
 
Back
Top