Tuesday, August 4, 2009

Generate PDF Files Dynamically Using ASP.NET


There are currently many ways to generate PDF files dynamically. The most widely known way is to use ASP together with Acrobat Full Version (4.0 or 5.0) and Acrobat FDF Toolkit. With Microsoft .NET many developers are wondering about how to use ASP.NET instead of ASP to tackle this same situation. To my surprise I could not find any related documentation from Adobe. I asked this question in many forums and no one had an answer for me. I had to roll up my sleeves and to my surprise--it's not so difficult. Now, I will make it even easier for all of you.
Adobe Acrobat 5.0 Full Version, not Acrobat Reader 5.0
Acrobat FDF Toolkit Version 5, free downloaded
Microsoft .NET Framework SDK with Service Pack 1
Windows 2000 Server (Service Pack 2), Internet Information Server 5.0
Installation Procedure
Install Adobe Acrobat 5.0 Full Version.
Go to to download the Acrobat FDF Toolkit package.
Follow installation instructions of FDF Toolkit Reference document at That is, uncompress the Acrobat FDF Toolkit package, find two files: FdfAcX.dll and FdfTk.dll. Install them both in \WINNT\system32 directory, then go to the directory, and type:Regsvr32 FdfAcX.dll. (Note: you do NOT need to register FdfTk.dll, as FdfAcX.dll uses FdfTk.dll).
Create .NET compatible "wrapper" for FdfAcX.dll using TlbImp.exe(Type Library Importer). In Microsoft Command Window, type: tlbimp FdfAcX.dll /out:FdfAcX_NET.dll
Put the generated CLR assembley FdfAcx_NET.dll in your application's \bin directory. Remember the rules: The assembly file generated by Tlbimp.exe must be placed in the ASP.NET application's \bin directory. The original COM component file must be registered for the directory in which it resides.
Comparison between ASP and ASP.NET use of FDF Toolkit
I will use a simplest example to show you the difference between ASP and ASP.NET when using the FDF Toolkit. Suppose you create a PDF template file using Adobe Acrobat 5 Full version, in which you only create one Text field named txtMemo. You want to populate the Text field using ASP.NET. You put the template file named test.pdf in the web root directory.
In ASP, using VBScript, you could use object FdfApp.FdfApp exposed by FdfAcX.dll like this: Set FdfAcX = Server.CreateObject("FdfApp.FdfApp")
Then you can populate the Text field as follows: FdfAcX.FDFSetFile ""
FdfAcX.FDFSetValue "txtMemo", "This is a test", false
FdfAcX.FDFSaveToFile "C:\temp\test.fdf"
Set FdfAcX = nothing
How to do the same thing using ASP.NET?We need to carefully examine the structure of the CLR assembly FdfAcX_NET.dll, which is generated using TlbImp.exe. After typing command: TlbImp FdfAcX_NET.dllwe get the following:

From the above graph, we could see that FdfApp is an interface(abstract), it implements FdfAcX_NET.IfdfApp interface. While FdfAppClass is the real implementation, it is a class which implements FdfAcX_NET.FdfApp interface, which in turn implements FdfAcX_NET.IfdfApp interface. We can also see that method FDFCreate is in class FdfAppClass, with return type of object.
Similarly, we could find out that both IfdfApp and IfdfDoc, at the bottom of the graph, are just interfaces. You can collapse them to find the methods defined inside them. FdfDoc is an interface, which implements IfdfDoc. FdfDocClass is the real implementation class, which implements FdfDoc interface. Methods FDFSetFile, FDFSetValue and FDFSaveToFile are all implemented in class FdfDocClass.
In order to use the assembly FdfAcX_NET.dll, we need to import it: <%@ Import Namespace="FdfAcX_NET" %>
Then instantiate to create an object of class FdfAppClass(the following is written in C#) in order to call its CreateObject method: FdfAppClass FdfAcX_App = new FdfAppClass();
According to the return type of method CreateObject, it is object type. I guess it has to be an object of FdfDoc, so that method FDFSetValue can be called later to set the Text field value. I cast the object type to FdfDoc like this: FdfDoc FdfAcX_Doc = (FdfDoc)FdfAcX_App.FDFCreate();
The following is the equivalent code in ASP: FdfAcX_Doc.FDFSetFile("");
FdfAcX_Doc.FDFSetValue("txtMemo", " This is a test", false);
Here is the VB.NET version: Dim FdfAcX_App As FdfAppClass
FdfAcX_App = new FdfAppClass()
Dim FdfAcX_Doc As FdfDoc
FdfAcX_Doc = FdfAcX_App.FDFCreate
FdfAcX_Doc.FDFSetValue("txtMemo", " This is a test ", false)
How to pass the generated file to web users?Create an ASP.NET page as follows and that is all there is to it.