Wednesday, April 6, 2011

Create PDF file from a template using Aspose.net

There are many applications where reports or custom documents have to be created in PDF formats. There are various tools available in the market to create PDF document on the fly. If you selected the tool provided by Aspose (http://www.aspose.com/community/files/default.aspx), read on ..

My requirement was to create a PDF document on the fly within a ASP.net application. The document to be created was a certificate and it had a fixed format. Only some of the text (like name, date) within the certificate had to fetched from the database and the document had to be created on user request (on demand, on the fly).

Instead of creating the whole document from scratch each time the document is to be created, I thought the best approach would be to create a PDF template and add form fields for all the fields that would change. Then use Aspose control to open the template, fill the fields. I thought this would be an efficient way to create these documents and response time would be reduced drastically. It would also be easier to code because most of the content of the document is in place and I do not have to code to place the content with the document.

As this was a ASP.net application and the document was to be created only the user requested for it (using a link within a web page), I did not want to save the document on the web server because I did not want to go through the hassle of having to delete these documents later or having piles of documents using my web server space. So the solution was to use streams and manage everything in memory.

The document also had to be secure and the user must not be able to edit the fields or make any changes to the document. The only permission that was to be available to the users was to open the document and print it. So here is the code I finally came up with



public static MemoryStream CreateCertificate(string pName, string pDate)
{
//open template
string lpdfTemplate = "

No comments:

Post a Comment