Wednesday, February 08, 2006

iTextSharp: Generating a Basic PDF file (ASP.NET/C#)

iTextSharp is free library for .NET that allows you to create PDF documents. It can be used to dynamically create PDF's which can be streamed to the user. As there is no HTML being sent to the user, a WebHandler (ashx file) is a more appropriate way to generate your PDF than an aspx page. Download itextsharp-3.0.10-dll.zip (latest version as of 08 Feb 2006) and save the dll in the archive to your websites bin directory.

Here is a basic sample of creating a PDF (more complex samples may follow in future posts).

BasicPDF.ashx

<%@ WebHandler Language="C#" Class="MyNamespace.BasicPDF" %>
using System;
using System.IO;
using System.Web;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace MyNamespace
{
 public class BasicPDF: IHttpHandler 
 {
  
  public bool IsReusable
  {
   get
   {
    return true;
   }
  }
  
  /// <summary>
  /// Font used for any hyperlinks added to the PDF
  /// </summary>
  private Font LinkFont
  {
   get
   {
    return new Font(Font.HELVETICA, Font.DEFAULTSIZE, Font.UNDERLINE, Color.BLUE);
   }
  }
  
  public void ProcessRequest(HttpContext ctx)
  {
   // make sure it is sent as a PDF
   ctx.Response.ContentType="application/pdf";
   // make sure it is downloaded rather than viewed in the browser window
   ctx.Response.AddHeader("Content-disposition", "attachment; filename=BasicPDF.pdf");
   
   // create a MemoryStream (as there may not be write access to any folder on the server)
   using(MemoryStream m = new MemoryStream())
   {
    // create the PDF document
    Document document = new Document(PageSize.A4);
    PdfWriter.GetInstance(document, m);
    
    // set meta data
    document.AddTitle("Test PDF");
    document.AddSubject("This is a PDF generated by a WebHandler in ASP.NET");
    document.AddKeywords("test document,foo,bar,baz");
    document.AddAuthor("John Doe");
    document.AddCreator("My Web Application");
    
    // open document to add content
    document.Open();
    
    // create a paragraph 
    Paragraph p = new Paragraph();
    
    // create a phrase object (which will contain the text to go in the paragraph)
    Phrase content = new Phrase();
    
    // start adding content to the phrase
    content.Add(new Chunk("Download iTextSharp from: "));
    
    Anchor link = new Anchor("itextsharp.sourceforge.net",LinkFont);
    link.Reference = "http://itextsharp.sourceforge.net";
    
    // add link to phrase
    content.Add(link);
    
    // add the phrase to the paragraph
    p.Add(content);
    
    // add the paragraph to the document
    document.Add(p);
    
    // add another paragraph
    document.Add(new Paragraph("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. "
     + "Praesent ante dui, adipiscing ac, pretium in, cursus non, dui. "
     + "Vivamus risus tellus, semper non, posuere eget, elementum eu, nulla. "
     + "Nulla scelerisque arcu et odio."));
    
    // close the document
    document.Close();
    
    // stream the PDF to the user
    ctx.Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
   }
   ctx.Response.End();
  }
 }
}

Tags: , , , , ,

3 comments:

Robert said...

Thanks, this solved my problem. But how do I get the pdf to open in the browser instead of the adobe viewer?

Sam said...

I think using 'inline' instead of attachment will work:

ctx.Response.AddHeader("Content-disposition", "inline; filename=BasicPDF.pdf");

Anonymous said...

itz good. it sloved my all problems with saving PDF on the fly. thankx again.