FormFields.vb
'' 完毕:
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

'' 该示例演示了如何创建各种 AcroForm 字段
'' 例如文本框、复选框、按钮等。
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

        '' 文本域:
        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.DefaultAppearance.Font = tf.Font
        fldText.Widget.DefaultAppearance.FontSize = tf.FontSize
        doc.AcroForm.Fields.Add(fldText)
        ip.Y += dY

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

        '' 单选按钮:
        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

        '' 梳状文本字段:
        g.DrawString("CombText field:", tf, ip)
        Dim fldCombText = New CombTextField()
        fldCombText.Value = "123"
        fldCombText.Widget.DefaultAppearance.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

        '' 组合框:
        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

        '' 列表框:
        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

        '' 签名栏:
        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.DefaultAppearance.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

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

        '' 重置表单按钮:
        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
        ''
        '' 完毕:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class