Channels ▼
RSS

.NET

Generating 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 the Acrobat Forms Data Format (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.

Tools

  • 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

Platform

Windows 2000 Server (Service Pack 2), Internet Information Server 5.0

Installation Procedure

  1. Install Adobe Acrobat 5.0 Full Version.
  2. Go to http://partners.adobe.com/asn/developer/acrosdk/forms.html to download the Acrobat FDF Toolkit package.
  3. Follow installation instructions of FDF Toolkit Reference document at http://partners.adobe.com/asn/developer/acrosdk/docs/fdftk/FDFtkRef.pdf. 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.)

  4. 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
  5. 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 "http://www.yourserver.com/test.pdf"
    FdfAcX.FDFSetValue "txtMemo", "This is a test", false
    FdfAcX.FDFSaveToFile "C:\temp\test.fdf"
    FdfAcX.FDFClose
    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.dll
    

    We 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.

    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:

    FdfAcX_Doc.FDFSetFile("http://www.yourserver.com/test.pdf"); FdfAcX_Doc.FDFSetValue("txtMemo", " This is a test", false); FdfAcX_Doc.FDFSaveToFile(@"c:\temp\test.fdf"); FdfAcX_Doc.FDFClose();

    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.FDFSetFile("http://www.yourserver.com/test.pdf")
    FdfAcX_Doc.FDFSetValue("txtMemo", " This is a test ", false)
    FdfAcX_Doc.FDFSaveToFile("c:\temp\test.fdf")
    FdfAcX_Doc.FDFClose
    
    

    How to pass the generated file to web users?

    Create an ASP.NET page as follows and that is all there is to it.

    
    <script language="C#" runat="server">
    <font color="#000099">protected void</font> Page_Load(<font color="#000099">Object</font> Src, EventArgs E)
    
    {
      if (!IsPostBack) {
        <font color="#000099">Response.ContentType="Application/vnd.fdf";</font>
    
        <font color="#000099">Response.WriteFile(@"c:\temp\test.fdf");</font>
        <font color="#000099">Response.End();</font>
      }
    }
    
    


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video