Friday, August 7, 2009

PDF generation using .net

I had got a requirement from client to generate membership certificate as a pdf document for the person who joins the website.We researched and planned to make use a third party library called itextsharpLib.

The requirement was to generate a pdf which had a background image and some text to be placed into the pdf which was not that difficult a requirement and hence was able to finish it off within a day with help from the internet mind you.

The problem with this requirement was that there was very less documentation in the internet about the itextsharp library for .net.

Also we had planned to dynamically generate a web page and convert the HTML to pdf. But there was a lot of complexity in the requirement to generate the pdf using HTML. So, I took the easier route and generated the pdf required pretty easily.

So I refered this link to get the required tutorial needed for my requirement.

Steps I went through to get the requirement done:

  1. Instantiate the memory stream stream, required for holding the pdf document generated.
  2. Create a document object document, with the required page size and other layout options.
  3. Instantiate a PdfWriter object writer, which requires the memory stream and document as parameters.
  4. Open the document object using document.Open() method.
  5. Created the Image element and added it to the document using document.Add(img) where img is the obj of the type Image which would be implemented using the IElement interface. Prior to the adding we needed to have initialized the img properties.
  6. Created a PdfContentbyte object using the line PdfContentByte cb = writer.DirectContent;
  7. Write the content on the image between the lines cb.BeginText(); and cb.EndText(); using the method cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "Text to be written", x-coordinate, y-coordinate,z-coordinate);
  8. Close the document object using document.close();
Now that we have the pdf document in the memory stream, putting the document in the HTTPHeader of the existing HTTPContext is pretty simple.
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/pdf";
string test = "document";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+test+".pdf");
HttpContext.Current.Response.BinaryWrite(MStream.GetBuffer());
HttpContext.Current.Response.End();

And there you are, you will get the pdf document as an attachment in your browser.

P.S: The blog entry is under construction. Will add the code snippet and downloadable source code shortly.

Thursday, August 6, 2009

My favourite sites and tools.

I am addicted to the internet and am obviously fascinated by the way in which the websites have evolved and so also the content. In this blog, I am going to list out a few of my favourite sites which I regularly go through to update myself with the happenings in the world.

I always believe that if you want to search anything on the internet, be it books, information or anything which you are interested in, try to see only from authentic sources. For example, if its book you are researching on, see the review of the book in amazon.com. You will also get related links related to the book that you have searched on amazon.

Below are the list of the few of my favourite sites

Technology related:
  • http://blogs.msdn.com/default.aspx - is the blog on Microsoft Technologies which I am interested in and I regularly go through
  • http://www.gartner.com/ is the blog which tells about the current trends in IT.
  • http://www.w3schools.com/ is the website to learn basic website building skills.
  • http://www.uxunleashed.com/ about the various user experience related blog. This blog is created by the previous organisation where I used to work.
  • http://www.readwriteweb.com/ which gives the latest technology trends of the world.
  • http://www.apifinder.com/ to search for any APIs that are available for free which might be needed for solving a problem.
Hobbies:
  • htttp://www.imdb.com has the complete list of hollywood and foreign language movies
  • http://www.the-numbers.com gives the list of hollywood movies box office performance.
  • http://www.livemocha.com if a person is interested in learning a foreign language online.
P.S: The list will be evergrowing as the web is ever growing. So will update this blog regularly. Also, there might be better sites than what I have mentioned in the blog entry. Suggestions and comments are always welcome.