3

I am generating a pdf using iTextSharp. I would like to display it on the webpage and let the user save it from the online pdf viewer. dynamicpdf has a drawtoweb() method, but it is not free to use and I cannot find the same functionality using iTextSharp. How can I display the pdf?

string newFile = "Pdf Document.pdf";
        Document doc = new Document();
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(newFile, FileMode.Create));
        doc.AddCreator("Myself");
        doc.AddTitle("Sample PDF Document");
        doc.Open();
        doc.Add(new Paragraph("Hello, World!"));
        doc.Close();
Starwfanatic
  • 584
  • 2
  • 13
  • 29

2 Answers2

2

Is there a reason you can't just display the PDF like any other website does? Just send the generated file to the user as a response stream with the correct meta data. It will open automatically in its default PDF viewer, most likely inside the browser.
As far as I understand the DrawToWeb method of dynamicpdf that's exactly what it is doing.

UPDATE:
Some research brought up a solution like this:
Add an iframe to your web page:

<IFrame runat="server" id="iframepdf">
</IFrame>

and populate it with the PDF file:

iframepdf.Attributes.Add("src", "showpdf.ashx?pdf=" + xyz);

See How <iframe src can read local temp file? for more info.

Community
  • 1
  • 1
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Perhaps. How would I go about doing that? I'm looking at Response.TransmitFile, but it wont accept Response.TransmitFile(doc); or Response.TransmitFile("Pdf Document.pdf"); – Starwfanatic Apr 05 '11 at 17:20
  • Thanks, but im getting a resource cannot be found error in the iframe. – Starwfanatic Apr 05 '11 at 18:11
0

I ended up just buying a license to dynamicpdf. It was much easier to work with than itextsharp.

Starwfanatic
  • 584
  • 2
  • 13
  • 29