PDF in asp.net

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();
}
 
A

Ashot Geodakov

Instead of "not sure how to put here" try

context.Response.OutputStream

Good luck.
 
M

Mark Fitzpatrick

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.
 
T

Tem

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.
 
A

Ashot Geodakov

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().
 
M

Mark Rae [MVP]

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
 
M

Mark Fitzpatrick

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
 
A

Ashot Geodakov

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...
 
T

Tem

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top