SUBSCRIBE - [ Tech News ] [ Make Money Blogging Tips ] [ Online Marketing Tips ] [ Web Dev News ]
Powered by MaxBlogPress  

Using Open Source iTextSharp .NET PDF library to generate PDF on the fly

October 15, 2008 by MK  
Filed under ASP.NET

Quite recently I worked on a project to generate a number of PDF’s from a single page template with data coming from an XML file. I searched for a number of options to convert an existing HTML page to PDF using .NET Open Source libraries. But I was not able to render the page correctly, my page was pure XHTML and a lot of libraries were not rendering the TAGS right. And as a result the PDF generating was all messed up with content and images everywhere.

Recommended reading - iText in Action: Creating and Manipulating PDF introduces iText and lowers the learning curve to its advanced features. Its numerous, valuable examples unlock many of the secrets hidden in Adobe’s PDF Reference. The examples are in Java but they can be easily adapted to .NET using one of iText’s .NET ports: iTextSharp (C#) or iText.NET (J#).

That is when I decided to generate PDF on the fly and not converting, i.e. writing content directly to the PDF instead of converting PDF from an existing HTML page. I will be writing more about the code to fill a PDF with data using FORM elements as placeholders in the PDF in the coming days. Right now I am pasting a quick script, this is what I used initially as a test to generate and create the PDF from scratch.

Please first ADD REFERENCE to iTextSharp DLL to your project before running the following code.

You can get the DLL by CLICKING HERE.

And here is the Home Page of ItextSharp project on SourceForge.Net - CLICK HERE.

//////////////FYI: I am using .NET 3.5///////////////////

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using iTextSharp.text;
using iTextSharp.text.pdf;

public partial class creator : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreatePDFDocument();
    }

    /// <summary>
    /// Creates the PDF document with a given content at a given location.
    /// </summary>
    /// <param name=”strFilePath”>The file path to write the new PDF to.</param>
    /// <param name=”strContent”>Content in HTML to write to the PDF.</param>
    public static void CreatePDFDocument()
    {
        MemoryStream MStream = new MemoryStream();
        Document document = new Document(PageSize.A4, 80, 50, 30, 65);
        try
        {
            PdfWriter writer = PdfWriter.GetInstance(document, MStream);
            document.Open();
            document.Add(new iTextSharp.text.Paragraph(”This is test and must work”));
            document.Close();
        }
        catch (Exception e)
        {
            throw e;
        }
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.ClearHeaders();
        HttpContext.Current.Response.ContentType = “application/pdf”;
        HttpContext.Current.Response.AddHeader(”Content-Disposition”, attachment;filename=myPDFNew.pdf”);
        HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer());
        HttpContext.Current.Response.End();
    }
}
////////////////////////////////

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • LinkedIn
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Yahoo! Buzz