FormDataSubmit.vb
''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Xml
Imports System.Text
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.AcroForms
Imports GrapeCity.Documents.Pdf.Actions
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Text

'' This sample creates an AcroForm PDF that can be submitted to the server.
'' The server then uses the (New in DsPdf v3) GcPdfDocument.ImportFormDataFromCollection()
'' method to import the submitted data into a PDF that contains a similarly structured
'' PDF form, And sends the form filled with user provided data back to the client.
''
'' Note that the produced PDF with filled form fields
'' Is shown in the client browser's default PDF viewer.
''
'' This sample Is similar to the now obsolete FormSubmitXml sample,
'' but the server side Is much simpler as it uses the New ImportFormDataFromCollection()
'' method that accepts a data structure very similar to how data Is sent from the client form,
'' so almost no code Is needed to manipulate that data.
'' See also the FormSubmit sample.
Public Class FormDataSubmit
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()

        Dim rc = Util.AddNote(
                "Fill the fields in the form and click 'Submit' to send it back to the server. " +
                "The sample server will use the GcPdfDocument.ImportFormDataFromCollection() method " +
                "to import the submitted data into a different but compatible PDF form, " +
                "and the filled form will be sent back to your browser. " +
                "Note that the filled form is opened in the browser's default PDF viewer, " +
                "and does not have the 'Submit' and 'Reset' buttons.",
                page)

        Dim g = page.Graphics
        Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
        Dim ip = New PointF(72, rc.Bottom + 36)
        Dim fldOffset = 72 * 2 + 46
        Dim fldHeight = tf.FontSize * 1.2F
        Dim dY = 32

        '' Text field
        g.DrawString("First name:", tf, ip)
        Dim fldFirstName = New TextField() With {.Name = "FirstName", .Value = "John"}
        fldFirstName.Widget.Page = page
        fldFirstName.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
        fldFirstName.Widget.DefaultAppearance.Font = tf.Font
        fldFirstName.Widget.DefaultAppearance.FontSize = tf.FontSize
        doc.AcroForm.Fields.Add(fldFirstName)
        ip.Y += dY

        '' Text field
        g.DrawString("Last name:", tf, ip)
        Dim fldLastName = New TextField() With {.Name = "LastName", .Value = "Smith"}
        fldLastName.Widget.Page = page
        fldLastName.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
        fldLastName.Widget.DefaultAppearance.Font = tf.Font
        fldLastName.Widget.DefaultAppearance.FontSize = tf.FontSize
        doc.AcroForm.Fields.Add(fldLastName)
        ip.Y += dY

        '' Checkbox
        g.DrawString("Subscribe to Mailing List:", tf, ip)
        Dim fldCheckbox = New CheckBoxField() With {.Name = "Subscribe", .Checked = True}
        fldCheckbox.Widget.Page = page
        fldCheckbox.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight)
        doc.AcroForm.Fields.Add(fldCheckbox)
        ip.Y += dY

        '' Multiline TextBox
        g.DrawString("Additional information:", tf, ip)
        Dim fldAdditionalInfo = New TextField() With {.Name = "AdditionalInfo", .Multiline = True}
        fldAdditionalInfo.Widget.Page = page
        fldAdditionalInfo.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight * 2)
        fldAdditionalInfo.Widget.DefaultAppearance.Font = tf.Font
        fldAdditionalInfo.Widget.DefaultAppearance.FontSize = tf.FontSize
        doc.AcroForm.Fields.Add(fldAdditionalInfo)
        ip.Y += dY * 2

        '' Submit form button:
        Dim btnSubmit = New PushButtonField()
        btnSubmit.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72, fldHeight)
        btnSubmit.Widget.ButtonAppearance.Caption = "Submit"
        btnSubmit.Widget.Highlighting = HighlightingMode.Invert
        btnSubmit.Widget.Page = page

        '' The URL for the submission
        btnSubmit.Widget.Activate = New ActionSubmitForm("/Samples/HandleFormDataSubmit")
        doc.AcroForm.Fields.Add(btnSubmit)

        '' Reset form button:
        Dim btnReset = New PushButtonField()
        btnReset.Widget.Rect = New RectangleF(ip.X + fldOffset + 72 * 1.5F, ip.Y, 72, fldHeight)
        btnReset.Widget.ButtonAppearance.Caption = "Reset"
        btnReset.Widget.Highlighting = HighlightingMode.Invert
        btnReset.Widget.Page = page
        btnReset.Widget.Activate = New ActionResetForm()
        doc.AcroForm.Fields.Add(btnReset)
        ip.Y += dY

        '' Done
        doc.Save(stream)
        Return doc.Pages.Count
    End Function

    ''
    '' NOTE: the code below Is used by the web sample browser controller When the form
    '' prepared by this sample Is submitted, it Is Not directly called by the CreatePDF() method.
    ''

    '' Creates a GcPdfDocument, loads an AcroForm PDF into it, And fills it with data
    '' using the GcPdfDocument.ImportFormDataFromCollection() method.
    ''
    '' This method Is called by the samples controller when the form prepared by this sample
    '' Is submitted by the user. The controller method converts the IFormCollection that it
    '' receives to an array Or key value pairs, where keys are field names And values are
    '' lists of string values, then calls this method to import the values into the
    '' compatible ImportFormXML.pdf PDF form. That form Is then sent back to the client.
    ''
    '' The controller code that calls this method looks Like this:
    ''
    '' Public Function HandleFormDataSubmit(ByVal fields As IFormCollection) As IActionResult
    ''   Dim values = fields.ToList()
    ''   Dim fieldValues = values.Select(Function(kvp_) New KeyValuePair(Of String, IList(Of String))(kvp_.Key, kvp_.Value.ToArray())).ToArray()
    ''   Dim ms = Samples.FormDataSubmit.ImportFormData(fieldValues)
    ''   Dim result = New FileStreamResult(ms, "application/pdf")
    ''   Return result
    '' End Function
    Shared Function ImportFormData(ByVal fieldValues As KeyValuePair(Of String, IList(Of String))()) As Stream
        Dim pdf = New GcPdfDocument()
        Using fs = New FileStream(Path.Combine("Resources", "PDFs", "ImportFormFromCollection.pdf"), FileMode.Open, FileAccess.Read)
            '' Load compatible empty form:
            pdf.Load(fs)
            '' Import submitted data:
            pdf.ImportFormDataFromCollection(fieldValues)
            '' Done:
            Dim outMs = New MemoryStream()
            pdf.Save(outMs)
            outMs.Seek(0, SeekOrigin.Begin)
            Return outMs
        End Using
    End Function
End Class