Example 11 - Advanced: Import FDF, xFDF, XML, XDP, or PDF data, Output PDF Fields and Values

Imports FDFApp
Imports System.Data
Partial Public Class example_11
    Inherits System.Web.UI.Page
    Private Debug_Me As Boolean = False
    Private _example_dataset_datasource As String = ""
    Protected Sub Load_Examples_GridView(ByVal grid As GridView, Optional ByVal IsDefault As Boolean = False)
        Dim ds As DataSet, selectedIndex As Integer = 0
        ds = Examples_Dataset
        grid.DataMember = ds.Tables(0).TableName
        grid.DataSource = ds.Tables(0).DefaultView
        grid.DataBind()
        grid.Visible = True
        grid.Focus()
        Me.lblMessage_Bottom.Text = "Status: scroll to see gridview..."
    End Sub
    Protected Property Examples_Dataset() As DataSet
        Get
	  Dim ds As DataSet
	  If Not Session("Example_Dataset") Is Nothing Then
	      ds = New DataSet("Examples")
	      ds = CType(Session("Example_Dataset"), DataSet)
	  Else
	      ds = New DataSet("Examples")
	      ds.Tables(0).Columns.Add("FieldName", GetType(System.String))
	      ds.Tables(0).Columns.Add("FieldValue", GetType(System.String))
	  End If
	  Return ds
        End Get
        Set(ByVal value As DataSet)
	  Session("Example_Dataset") = CType(value, DataSet)
        End Set
    End Property
    Private Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click
        Page.Validate()
        If (Page.IsValid) Then
	  Example()
        Else
	  Update_Message("Message - Sorry, Capcha is invalid!")
        End If
    End Sub
    Public Sub Example()
        ' DECLARE FDFApp & FDFDoc Classes
        Dim cFDFApp As New FDFApp.FDFApp_Class
        Dim cFDFDoc As New FDFApp.FDFDoc_Class
        ' INITIALIZE FDFDOC Class
		'cFDFDoc = cFDFApp.FDFCreate
        HttpContext.Current.Session("parseData") = Nothing
        ' LOAD FILE
        If Not String.IsNullOrEmpty(StripCodeFromString(frmFDFURL.Text) & "") Then
            If StripCodeFromString(frmFDFURL.Text).ToString.EndsWith(".pdf") Then
                cFDFDoc = cFDFApp.PDFOpenFromURL(StripCodeFromString(frmFDFURL.Text), False, False)
            ElseIf StripCodeFromString(frmFDFURL.Text).ToString.EndsWith(".fdf") Or StripCodeFromString(frmFDFURL.Text).ToString.EndsWith(".xfdf") Then
                'cFDFDoc = cFDFApp.FDFOpenWithiText(PDFUpload1.FileBytes, False, False)
                cFDFDoc = cFDFApp.FDFOpenFromURL(StripCodeFromString(frmFDFURL.Text).ToString, False, False)
            ElseIf StripCodeFromString(frmFDFURL.Text).ToString.EndsWith(".xml") Or StripCodeFromString(frmFDFURL.Text).ToString.EndsWith(".xdp") Then
                'cFDFDoc = cFDFApp.FDFOpenWithiText(PDFUpload1.FileBytes, False, False)
                cFDFDoc = cFDFApp.FDFOpenFromURL(StripCodeFromString(frmFDFURL.Text).ToString, False, False)
            End If
        ElseIf PDFUpload1.HasFile = True Then
            If PDFUpload1.FileName.EndsWith(".pdf") Then
                cFDFDoc = cFDFApp.PDFOpenFromBuf(PDFUpload1.FileBytes, False, False)
            ElseIf PDFUpload1.FileName.EndsWith(".fdf") Or PDFUpload1.FileName.EndsWith(".xfdf") Then
                'cFDFDoc = cFDFApp.FDFOpenWithiText(PDFUpload1.FileBytes, False, False)
                cFDFDoc = cFDFApp.FDFOpenFromBuf(PDFUpload1.FileBytes, False, False)
            ElseIf PDFUpload1.FileName.EndsWith(".xml") Or PDFUpload1.FileName.EndsWith(".xdp") Then
                'cFDFDoc = cFDFApp.FDFOpenWithiText(PDFUpload1.FileBytes, False, False)
                cFDFDoc = cFDFApp.FDFOpenFromBuf(PDFUpload1.FileBytes, False, False)
            End If
        Else
            Update_Message("Please select a PDF file url or upload a PDF.")
            Examples_Selection.DataSource = Nothing
            Examples_Selection.DataBind()
            Exit Sub
        End If
        ' CREATE Examples DATASET
        Dim ds As New DataSet
        ds = New DataSet
        Dim dt As New DataTable("Examples")
        dt.Columns.Add("FieldNumber", GetType(System.Int32))
        dt.Columns.Add("FieldName", GetType(System.String))
        dt.Columns.Add("FieldValue", GetType(System.String))
        ds.Tables.Add(dt)
        Dim dr As DataRow, fldNumber As Integer = 0
        If (cFDFApp.Determine_Type(PDFUpload1.FileBytes) = FDFDoc_Class.FDFType.FDF Or cFDFApp.Determine_Type(PDFUpload1.FileBytes) = FDFDoc_Class.FDFType.xFDF Or cFDFApp.Determine_Type(PDFUpload1.FileBytes) = FDFDoc_Class.FDFType.XDP Or cFDFApp.Determine_Type(PDFUpload1.FileBytes) = FDFDoc_Class.FDFType.PDF Or cFDFApp.Determine_Type(PDFUpload1.FileBytes) = FDFDoc_Class.FDFType.XPDF Or cFDFApp.Determine_Type(PDFUpload1.FileBytes) = FDFDoc_Class.FDFType.XML) And cFDFDoc.XDPFieldCount >= 1 Then
            ' LOOP THROUGH EACH XDP FIELD/FIELD VALUE
            For Each fld In cFDFDoc.XDPGetFields
                dr = ds.Tables(0).NewRow
                fldNumber += 1
                dr("FieldNumber") = fldNumber + 0
                If CStr(fld.FieldName & "").Length > 30 Then
                    dr("FieldName") = StripCodeFromString(CStr(fld.FieldName).Substring(0, 29)) & ".."
                Else
                    dr("FieldName") = StripCodeFromString(fld.FieldName & "")
                End If

                If fld.FieldValue.Count > 0 Then
                    If CStr(fld.FieldValue(0).Length & "").Length > 30 Then
                        dr("FieldValue") = StripCodeFromString(CStr(fld.FieldValue(0)).Substring(0, 29)) & ".."
                    Else
                        dr("FieldValue") = StripCodeFromString(fld.FieldValue(0) & "")
                    End If
                End If
                ds.Tables(0).Rows.Add(dr)
            Next
            Examples_Dataset = ds
            ' UPDATE GRIDVIEW
            Load_Examples_GridView(Examples_Selection, False)

        ElseIf cFDFDoc.FDFFieldCount >= 1 Then
            ' LOOP THROUGH EACH FDF FIELD/FIELD VALUE
            For Each fld In cFDFDoc.FDFGetFields
                dr = ds.Tables(0).NewRow
                fldNumber += 1
                dr("FieldNumber") = fldNumber + 0
                If CStr(fld.FieldName & "").Length > 30 Then
                    dr("FieldName") = StripCodeFromString(CStr(fld.FieldName).Substring(0, 29)) & ".."
                Else
                    dr("FieldName") = StripCodeFromString(fld.FieldName & "")
                End If

                If CStr(fld.FieldValue(0).Length & "").Length > 30 Then
                    dr("FieldValue") = StripCodeFromString(CStr(fld.FieldValue(0)).Substring(0, 29)) & ".."
                Else
                    dr("FieldValue") = StripCodeFromString(fld.FieldValue(0) & "")
                End If
                ds.Tables(0).Rows.Add(dr)
            Next
            Examples_Dataset = ds
            ' UPDATE GRIDVIEW
            Load_Examples_GridView(Examples_Selection, False)
        Else
            Examples_Selection.DataSource = Nothing
            Examples_Selection.DataBind()
        End If
        Exit Sub
    End Sub
    Public Sub Example(ByVal InputStream As System.IO.Stream)
        Me.form1.Target = "_self"
        Dim cFDFApp As New FDFApp.FDFApp_Class
        Dim cFDFDoc As New FDFApp.FDFDoc_Class
        cFDFDoc = cFDFApp.FDFCreate
        If Not InputStream Is Nothing Then
	  If InputStream.CanRead Then
	      InputStream.Position = 0
	  End If
	  cFDFDoc = cFDFApp.FDFOpenFromStream(InputStream, True, False)
        Else
	  Update_Message("Please select a PDF file url or upload a PDF.")
	  Examples_Selection.DataSource = Nothing
	  Examples_Selection.DataBind()
	  Exit Sub
        End If
        Dim ds As New Data.DataSet
        ds = New Data.DataSet
        Dim dt As New Data.DataTable("Examples")
        dt.Columns.Add("FieldNumber", GetType(System.Int32))
        dt.Columns.Add("FieldName", GetType(System.String))
        dt.Columns.Add("FieldValue", GetType(System.String))
        ds.Tables.Add(dt)
        Dim dr As Data.DataRow
        Dim fldNumber2 As Integer
        If cFDFDoc.FDFFieldCount >= 1 Then
	  For Each fld In cFDFDoc.FDFGetFields
	      If Not String.IsNullOrEmpty(fld.FieldName & "") Then
		dr = ds.Tables(0).NewRow
		fldNumber2 += 1
		dr("FieldNumber") = fldNumber2 + 0
                    If CStr(fld.FieldName & "").Length > 30 Then
                        dr("FieldName") = StripCodeFromString(CStr(fld.FieldName).Substring(0, 29)) & ".."
                    Else
                        dr("FieldName") = StripCodeFromString(fld.FieldName & "")
                    End If

                    If CStr(fld.FieldValue(0).Length & "").Length > 30 Then
                        dr("FieldValue") = StripCodeFromString(CStr(fld.FieldValue(0)).Substring(0, 29)) & ".."
                    Else
                        dr("FieldValue") = StripCodeFromString(fld.FieldValue(0) & "")
                    End If
		ds.Tables(0).Rows.Add(dr)
	      End If

	  Next
	  Examples_Dataset = ds
	  Load_Examples_GridView(Examples_Selection, False)
        ElseIf cFDFDoc.XDPFieldCount >= 1 Then
	  For Each fld In cFDFDoc.XDPGetFields
	      dr = ds.Tables(0).NewRow
	      fldNumber2 += 1
	      dr("FieldNumber") = fldNumber2 + 0
                If CStr(fld.FieldName & "").Length > 30 Then
                    dr("FieldName") = StripCodeFromString(CStr(fld.FieldName).Substring(0, 29)) & ".."
                Else
                    dr("FieldName") = StripCodeFromString(fld.FieldName & "")
                End If

                If CStr(fld.FieldValue(0).Length & "").Length > 30 Then
                    dr("FieldValue") = StripCodeFromString(CStr(fld.FieldValue(0)).Substring(0, 29)) & ".."
                Else
                    dr("FieldValue") = StripCodeFromString(fld.FieldValue(0) & "")
                End If
	      ds.Tables(0).Rows.Add(dr)
	  Next
	  Examples_Dataset = ds
	  Load_Examples_GridView(Examples_Selection, False)
        Else
	  Examples_Selection.DataSource = Nothing
	  Examples_Selection.DataBind()
        End If
        Exit Sub
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        PDFUpload1.CssClass = "file"
        '_example_dataset_datasource = Examples_Dataset
        Me.SubmitButton.Text = "Submit"
        Me.SubmitButton.Enabled = True
        Me.SubmitButton.CssClass = "button button-250 bgRed white"
        Me.ResetButton.CssClass = "button button-250 bgRed white"
        Me.ResetButton.Text = "Reset"
        If Not IsPostBack Then
            Me.SubmitButton.Enabled = True
            Me.SubmitButton.CssClass = "button button-250 bgRed white"
            If Not String.IsNullOrEmpty(Request("act") & "") Then
                If Request("act") = "parse" Then
                    If CType(HttpContext.Current.Session("parseData"), System.IO.Stream) Is Nothing Then
                        HttpContext.Current.Session("parseData") = CType(Request.InputStream, System.IO.Stream)
                    End If
                    If Not CType(HttpContext.Current.Session("parseData"), System.IO.Stream) Is Nothing Then
                        Example(CType(HttpContext.Current.Session("parseData"), System.IO.Stream))
                    End If
                End If
            End If
            Dim SubmitURL As String = HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.LastIndexOf("/") + 1) & "default.aspx?example=11&act=parse"
            Me.frmFDFPostURL.Text = SubmitURL.ToString.ToLower
        End If
        Update_Message("Complete this form, and then press submit.")
    End Sub
    Protected Sub Load_Acrobat_Submit_Button_Action(ByVal url As String)
        Dim cFDFApp As New FDFApp.FDFApp_Class
        Dim cFDFDoc As New FDFApp.FDFDoc_Class
        cFDFDoc = cFDFApp.FDFCreate
        cFDFDoc.FDFSetSubmitFormAction("SubmitButton", FDFDoc_Class.FDFActionTrigger.FDFUp, url)
        Dim PDFPath As String = HttpContext.Current.Request.Url.AbsoluteUri.Substring(0, HttpContext.Current.Request.Url.AbsoluteUri.LastIndexOf("/") + 1) & "example_11_acrobat_blank.pdf"
        cFDFDoc.FDFSetFile(PDFPath)
        cFDFDoc.FDFSavetoFile(Server.MapPath("example_11_acrobat.fdf"), FDFDoc_Class.FDFType.FDF, False)
        cFDFDoc.FDFClose()
    End Sub
    Protected Sub Save_Acrobat_Submit_Button_Action(ByVal cFDFDoc As FDFDoc_Class)
        cFDFDoc.FDFSavetoFile(Server.MapPath("example_11_acrobat.fdf"), FDFDoc_Class.FDFType.FDF, False)
        cFDFDoc.FDFClose()
    End Sub
    Private Sub Update_Message(ByVal strMsg As String)
        Me.lblMessage_Top.Text = "Status: " & strMsg
        Me.lblMessage_Bottom.Text = "Status: " & strMsg
    End Sub
    Private Sub ResetButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ResetButton.Click
        Me.form1.Target = "_self"
        Me.SubmitButton.Enabled = True
        Me.SubmitButton.CssClass = "button button-250 bgRed white"


        Example()
    End Sub

    

End Class