FormFields.vb
''
'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, 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 the various AcroForm fields
'' such as textbox, checkbox, push buttons and so on.
Public Class FormFields
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics
        Dim tf = New TextFormat()
        tf.Font = StandardFonts.Times
        tf.FontSize = 14
        Dim ip = New PointF(72, 72)
        Dim fldOffset = 72.0F * 2
        Dim fldHeight = tf.FontSize * 1.2F
        Dim dY = 32.0F

        '' Text field:
        g.DrawString("Text field:", tf, ip)
        Dim fldText = New TextField()
        fldText.Value = "Initial TextField value"
        fldText.Widget.Page = page
        fldText.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
        fldText.Widget.TextFormat.Font = tf.Font
        fldText.Widget.TextFormat.FontSize = tf.FontSize
        doc.AcroForm.Fields.Add(fldText)
        ip.Y += dY

        '' Checkbox:
        g.DrawString("Checkbox:", tf, ip)
        Dim fldCheckbox = New CheckBoxField()
        fldCheckbox.Value = 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

        '' Radio button:
        g.DrawString("Radio button:", tf, ip)
        Dim fldRadio = New RadioButtonField()
        fldRadio.Value = 1
        fldRadio.Widgets.Add(New WidgetAnnotation(page, New RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight)))
        fldRadio.Widgets.Add(New WidgetAnnotation(page, New RectangleF(ip.X + fldOffset, ip.Y + fldHeight * 1.2F, fldHeight, fldHeight)))
        fldRadio.Widgets.Add(New WidgetAnnotation(page, New RectangleF(ip.X + fldOffset, ip.Y + (fldHeight * 1.2F) * 2, fldHeight, fldHeight)))
        doc.AcroForm.Fields.Add(fldRadio)
        ip.Y = fldRadio.Widgets(fldRadio.Widgets.Count - 1).Rect.Y + dY

        '' CombTextField
        g.DrawString("CombText field:", tf, ip)
        Dim fldCombText = New CombTextField()
        fldCombText.Value = "123"
        fldCombText.Widget.TextFormat.FontSize = 12
        fldCombText.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
        fldCombText.Widget.Page = page
        doc.AcroForm.Fields.Add(fldCombText)
        ip.Y += dY

        '' Combo-box:
        g.DrawString("Combo box:", tf, ip)
        Dim fldComboBox = New ComboBoxField()
        fldComboBox.Items.Add(New ChoiceFieldItem("ComboBox Choice 1"))
        fldComboBox.Items.Add(New ChoiceFieldItem("ComboBox Choice 2"))
        fldComboBox.Items.Add(New ChoiceFieldItem("ComboBox Choice 3"))
        fldComboBox.SelectedIndex = 1
        fldComboBox.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
        fldComboBox.Widget.Page = page
        doc.AcroForm.Fields.Add(fldComboBox)
        ip.Y += dY

        '' List box:
        g.DrawString("List box:", tf, ip)
        Dim fldListBox = New ListBoxField()
        fldListBox.Items.Add(New ChoiceFieldItem("ListBox Choice 1"))
        fldListBox.Items.Add(New ChoiceFieldItem("ListBox Choice 2"))
        fldListBox.Items.Add(New ChoiceFieldItem("ListBox Choice 3"))
        fldListBox.SelectedIndexes = New Integer() {0, 2}
        fldListBox.MultiSelect = True
        fldListBox.CommitOnSelChange = True
        fldListBox.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 100, 50)
        fldListBox.Widget.Page = page
        doc.AcroForm.Fields.Add(fldListBox)
        ip.Y = fldListBox.Widget.Rect.Bottom - fldHeight + dY

        '' Signature field:
        g.DrawString("Signature field:", tf, ip)
        Dim fldSignature = New SignatureField()
        fldSignature.AlternateName = "All fields locked when the document is signed"
        fldSignature.LockedFields = New SignatureLockedFields()
        fldSignature.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 2, 72 - dY)
        fldSignature.Widget.TextFormat.FontSize = 8
        fldSignature.Widget.ButtonAppearance.Caption = "Click to sign"
        fldSignature.Widget.Border = New Border() With {.Width = 0.5F, .Color = Color.DarkSeaGreen}
        fldSignature.Widget.Page = page
        doc.AcroForm.Fields.Add(fldSignature)
        ip.Y += 72 - fldHeight

        '' Buttons:
        g.DrawString("Push buttons:", tf, ip)
        '' 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.Events.Activate = New ActionSubmitForm("Sample Form Submit URI")
        btnSubmit.Widget.Page = page
        doc.AcroForm.Fields.Add(btnSubmit)
        '' ip.Y += dY

        '' 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.Events.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