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

'' This sample demonstrates how to create an AcroForm PDF that the user can submit.
'' Here we submit it To the sample server, which receives the data and sends it back
'' in a special form.
Public Class FormSubmit
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()

        Dim rc = Util.AddNote(
            "In this sample the Submit button is associated with the ActionSubmitForm action, " +
            "with the URL pointing to a POST handler running on our sample server. " +
            "When the form is submitted, that handler receives a collection of form field names " +
            "and field values from the filled form, and sends it back in a simple HTML page. " +
            "If you download this sample, to successfully run it you will need to set up your own " +
            "handler, and change the Submit button action's URL to point to that handler.",
            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.0F * 2 + 46
        Dim fldHeight = tf.FontSize * 1.2F
        Dim dY = 32.0F

        '' 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 fldAdditionalInformation = New TextField() With {.Name = "AdditionalInformation", .Multiline = True}
        fldAdditionalInformation.Widget.Page = page
        fldAdditionalInformation.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight * 2)
        fldAdditionalInformation.Widget.DefaultAppearance.Font = tf.Font
        fldAdditionalInformation.Widget.DefaultAppearance.FontSize = tf.FontSize
        doc.AcroForm.Fields.Add(fldAdditionalInformation)
        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/HandleFormSubmitFields")
        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.Activate = New ActionResetForm()
        btnReset.Widget.Page = page
        doc.AcroForm.Fields.Add(btnReset)
        ip.Y += dY
        ''
        '' Done:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class