NK-Inc.com
PDF Programming Tutorials

PDF Programming Tutorials

Programming Dynamic PDF forms using FDFToolkit.net

Tags: | Categories: PDF Programming Posted by NicK on 7/27/2008 12:38 PM | Comments (0)

Basic principles of populating or updating a database with Adobe Acrobat PDF Form data using VB.net, ASP.net, and the FDF Toolkit .net component.

Scenerio: User fills out a PDF form online, and clicks submit. The form is sent to a server side script, see code, and the script parses the field information and stores it into a database.

Rules: Make sure when posting PDF Data to a buffer, do not post any HTML along with PDF Data. Remove any header information by declaring "Response.Clear()" before posting any PDF or PDF data MIME Content.

 

Imports FDFApp
Imports FDFApp.FDFApp_Class
Imports FDFApp.FDFDoc_Class
Public Class loadform
        Inherits System.Web.UI.Page
Private URLPath as String = ""
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    URLPath = Request.Url.ToString
    Try
        Update_Data(Request.QueryString("id") & "")
    Catch Ex as Exception
        Response.Write("Error: " & Ex.Message)
        Response.End
    End Try
End Sub
Public Function Update_Data(ByVal form_id As Integer) As Boolean
Try
    Dim FDFApp As New FDFApp.FDFApp_Class
    Dim FDFDoc As New FDFApp.FDFDoc_Class
    'FDFDoc = FDFApp.FDFOpenFromBuf(Request.BinaryRead(Request.TotalBytes), True, True)
    FDFDoc = FDFApp.FDFOpenFromStream(Request.InputStream, True, True)
    'Response.ContentType = FDFApp.MimeFDF
    Dim dr As DataRow, ds As New DataSet
    Dim da As New OleDbDataAdapter, sSQL(2) As String
    sSQL(0) = "SELECT * FROM TableName WHERE "
    sSQL(1) = "ID = " & form_id & ";"
    sSQL(2) = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " & Server.MapPath("Data/Database_Name.mdb") & ";"
    da = New OleDbDataAdapter(sSQL(0) & sSQL(1), sSQL(2))
    Dim cmd As New OleDbCommandBuilder(da)
    da.Fill(ds, "TableName")
    Try
        If ds.Tables("TableName").Rows.Count = 1 Then
            dr = ds.Tables("TableName").Rows(0)
            dr = FDFDoc.FDFSetDataRowFromValues(dr)
            da.Update(ds, "TableName")
            Response.ContentType = FDFApp.MimeFDF
            FDFDoc.FDFSetStatus("Success! Updated Database", True)
            Response.BinaryWrite(FDFDoc.FDFSavetoBuf(FDFApp.FDFType.FDF, False))
            Return True
        Else
            FDFDoc.FDFSetStatus("ERROR: This form did not update a record!", True)
            Response.ContentType = FDFApp.MimeFDF
            Response.BinaryWrite(FDFDoc.FDFSavetoBuf(FDFApp.FDFType.FDF, True))
            Return False
            Exit Function
        End If
    Catch ex As Exception
        FDFDoc.FDFSetStatus("ERROR: This form did not update a record!", True)
        Response.ContentType = FDFApp.MimeFDF
        Response.BinaryWrite(FDFDoc.FDFSavetoBuf(FDFApp.FDFType.FDF, True))
        Return False
        Exit Function
    End Try
Catch ex As Exception
    FDFDoc.FDFSetStatus("ERROR: This form did not update a record!", True)
        Response.Clear()
        Response.ContentType = FDFApp.MimeFDF
    Response.BinaryWrite(FDFDoc.FDFSavetoBuf(FDFApp.FDFType.FDF, True))
    Return False
    Exit Function
End Try