Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PDF in asp.net

3 views
Skip to first unread message

Tem

unread,
Oct 10, 2007, 5:52:54 PM10/10/07
to
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();
}

Ashot Geodakov

unread,
Oct 10, 2007, 6:06:50 PM10/10/07
to
Instead of "not sure how to put here" try

context.Response.OutputStream

Good luck.

"Tem" <tem...@yahoo.com> wrote in message
news:OATpwf4C...@TK2MSFTNGP05.phx.gbl...

Mark Fitzpatrick

unread,
Oct 10, 2007, 6:17:46 PM10/10/07
to
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.

--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

"Tem" <tem...@yahoo.com> wrote in message
news:OATpwf4C...@TK2MSFTNGP05.phx.gbl...

Message has been deleted

Tem

unread,
Oct 10, 2007, 6:29:50 PM10/10/07
to
It doesn't work I get a blank page


"Ashot Geodakov" <a_geo...@nospam.hotmail.com> wrote in message
news:uma7cn4C...@TK2MSFTNGP06.phx.gbl...

Ashot Geodakov

unread,
Oct 10, 2007, 6:46:14 PM10/10/07
to
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().

"Tem" <tem...@yahoo.com> wrote in message

news:ubOCZ04C...@TK2MSFTNGP06.phx.gbl...

Mark Rae [MVP]

unread,
Oct 10, 2007, 6:47:57 PM10/10/07
to
"Mark Fitzpatrick" <mark...@fitzme.com> wrote in message
news:uzHfht4C...@TK2MSFTNGP05.phx.gbl...

> 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


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Mark Fitzpatrick

unread,
Oct 10, 2007, 7:13:41 PM10/10/07
to
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

"Tem" <tem...@yahoo.com> wrote in message

news:ODwshx4C...@TK2MSFTNGP02.phx.gbl...
> Could you look for that sample?
>
>
> Thank you


>
>
> "Mark Fitzpatrick" <mark...@fitzme.com> wrote in message
> news:uzHfht4C...@TK2MSFTNGP05.phx.gbl...

Ashot Geodakov

unread,
Oct 10, 2007, 7:23:59 PM10/10/07
to
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...

"Mark Fitzpatrick" <mark...@fitzme.com> wrote in message

news:%23l6RxM5...@TK2MSFTNGP02.phx.gbl...

Brian Simmons

unread,
Oct 11, 2007, 10:47:46 AM10/11/07
to
I asked a question about PDF/Word document generation recently, and one of
the responses I got was to use:
http://www.aspose.com/
Specifically:
http://www.aspose.com/Products/Aspose.Pdf/

Downloaded the trial, absolutely loved it, company purchased it, and we've
never looked back.

Not affiliated in anyway, just a very satisfied customer/user.

hth,
Brian

"Tem" <tem...@yahoo.com> wrote in message
news:OATpwf4C...@TK2MSFTNGP05.phx.gbl...

Tem

unread,
Oct 11, 2007, 2:07:31 PM10/11/07
to
I got it to work

thanks for your help


Tem


"Mark Fitzpatrick" <mark...@fitzme.com> wrote in message

news:%23l6RxM5...@TK2MSFTNGP02.phx.gbl...

0 new messages