FillForm.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Pdf.AcroForms

'' 此示例​​加载由FormFields示例创建的表单,
'' 循环遍历该文件中找到的所有表单字段,
'' 并修改输入字段的值。
'' 所做操作的日志(显示旧值和新值)将添加到表单页面。
Public Class FillForm
    Function CreatePDF(ByVal stream As Stream) As Integer
        Dim doc = New GcPdfDocument()

        '' 在处理加载的PDF时,原始文件流必须保持打开状态,请参阅LoadPDF了解详细信息:
        Using fs As New FileStream(Path.Combine("Resources", "PDFs", "FormFields.pdf"), FileMode.Open, FileAccess.Read)
            doc.Load(fs)
            Dim page = doc.Pages.Last
            Dim sb = New StringBuilder()
            sb.AppendLine("Log of updates made by the FillForm sample:" + vbCrLf)

            For Each fld In doc.AcroForm.Fields
                If TypeOf fld Is CombTextField Then
                    Dim ctfld = DirectCast(fld, CombTextField)
                    sb.Append($"CombTextField.Value was '{ctfld.Value}', ")
                    ctfld.Value = "Comb text"
                    sb.AppendLine($"now '{ctfld.Value}'.")
                ElseIf TypeOf fld Is TextField Then
                    Dim tfld = DirectCast(fld, TextField)
                    sb.Append($"TextField.Value was '{tfld.Value}', ")
                    tfld.Value = $"Text updated on {Util.TimeNow():u}"
                    sb.AppendLine($"now '{tfld.Value}'.")
                ElseIf TypeOf fld Is CheckBoxField Then
                    Dim cfld = DirectCast(fld, CheckBoxField)
                    sb.Append($"CheckBoxField.Value was '{cfld.Checked}', ")
                    cfld.Checked = Not cfld.Checked
                    sb.AppendLine($"now '{cfld.Checked}'.")
                ElseIf TypeOf fld Is RadioButtonField Then
                    Dim rbfld = DirectCast(fld, RadioButtonField)
                    sb.Append($"RadioButtonField.Value was '{rbfld.Value}', ")
                    rbfld.Value = rbfld.Widgets.Count - 1
                    sb.AppendLine($"now '{rbfld.Value}'.")
                ElseIf TypeOf fld Is ComboBoxField Then
                    Dim cmbfld = DirectCast(fld, ComboBoxField)
                    sb.Append($"ComboBoxField selection was '{cmbfld.Items(cmbfld.SelectedIndex).Text}', ")
                    cmbfld.SelectedIndex = cmbfld.Items.Count - 1
                    sb.AppendLine($"now '{cmbfld.Items(cmbfld.SelectedIndex).Text}'.")
                ElseIf TypeOf fld Is ListBoxField Then
                    Dim lbfld = DirectCast(fld, ListBoxField)
                    sb.Append($"ListBoxField selection was '{lbfld.Items(lbfld.SelectedIndex).Text}', ")
                    lbfld.SelectedIndex = lbfld.Items.Count - 1
                    sb.AppendLine($"now '{lbfld.Items(lbfld.SelectedIndex).Text}'.")
                ElseIf TypeOf fld Is SignatureField Then
                    Dim sfld = DirectCast(fld, SignatureField)
                    sb.AppendLine("SignatureField found.")
                ElseIf TypeOf fld Is PushButtonField Then
                    Dim btnfld = DirectCast(fld, PushButtonField)
                    sb.AppendLine($"PushButtonField '{btnfld.Widget.ButtonAppearance.Caption}' found.")
                Else
                    sb.AppendLine($"Field '{fld}' found/")
                End If
            Next
            '' 在页面底部添加我们所做操作的日志:
            Dim tl = New TextLayout(72) With {
                .MaxWidth = page.Size.Width,
                .MaxHeight = page.Size.Height,
                .MarginLeft = 80,
                .MarginRight = 80,
                .MarginBottom = 80,
                .ParagraphAlignment = ParagraphAlignment.Far
            }
            tl.Append(sb.ToString(), New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 12})
            tl.PerformLayout(True)
            Dim rc = tl.ContentRectangle
            rc.Inflate(8, 8)
            page.Graphics.FillRectangle(rc, Color.LightYellow)
            page.Graphics.DrawRectangle(rc, Color.Orange)
            page.Graphics.DrawTextLayout(tl, PointF.Empty)
            '' 完毕:
            doc.Save(stream)
        End Using
        ''
        Return doc.Pages.Count
    End Function
End Class