''
'' This code is part of Document Solutions for PDF demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
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
'' This sample loads the form created by the FormFields sample,
'' loops through all form fields found in that file,
'' and modifies the values of input fields.
'' The log of what was done (showing old and new values) is added to the form page.
Public Class FillForm
Function CreatePDF(ByVal stream As Stream) As Integer
Dim doc = New GcPdfDocument()
'' The original file stream must be kept open while working with the loaded PDF, see LoadPDF for details:
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
'' Add a log of what we did at the bottom of the page:
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)
'' Done:
doc.Save(stream)
End Using
''
Return doc.Pages.Count
End Function
End Class