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 = "

Hiding a node in a TreeView control

I had a web page with a treeView control and I wanted to hide a particular node for a specific condition. I thought this would be pretty easy and I might just have to set the "Visible" property of the node. But I was surprised to see that the node does not have this property maybe because the node is not a control.

If the treeview is built from a data source, you can use the TreeNodeDataBound event to acheive this. But its not as simple as doing
e.node.visible because as I said, visible is not a property

Even treeviewControl.Nodes.Remove(e.node) will not work if you are trying to remove a node that is a leaf node (node that does not have children).

So to remove a node, just try this code
e.Node.Parent.ChildNodes.Remove(e.Node);

Unable to resolve dimension value "match_parent" in attribute "layout_width"

Unable to resolve dimension value "match_parent" in attribute "layout_width"


If you get the above error while developing any Android application, just replace change "match_parent" with "fill_parent"

I spend some time figuring this out .. so thought maybe this would help someone out there who has similar issues