FillForm.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Text
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Drawing
  11. Imports GrapeCity.Documents.Pdf.AcroForms
  12.  
  13. '' This sample loads the form created by the FormFields sample,
  14. '' loops through all form fields found in that file,
  15. '' and modifies the values of input fields.
  16. '' The log of what was done (showing old and new values) is added to the form page.
  17. Public Class FillForm
  18. Function CreatePDF(ByVal stream As Stream) As Integer
  19. Dim doc = New GcPdfDocument()
  20.  
  21. '' The original file stream must be kept open while working with the loaded PDF, see LoadPDF for details:
  22. Using fs As New FileStream(Path.Combine("Resources", "PDFs", "FormFields.pdf"), FileMode.Open, FileAccess.Read)
  23. doc.Load(fs)
  24. Dim page = doc.Pages.Last
  25. Dim sb = New StringBuilder()
  26. sb.AppendLine("Log of updates made by the FillForm sample:" + vbCrLf)
  27.  
  28. For Each fld In doc.AcroForm.Fields
  29. If TypeOf fld Is CombTextField Then
  30. Dim ctfld = DirectCast(fld, CombTextField)
  31. sb.Append($"CombTextField.Value was '{ctfld.Value}', ")
  32. ctfld.Value = "Comb text"
  33. sb.AppendLine($"now '{ctfld.Value}'.")
  34. ElseIf TypeOf fld Is TextField Then
  35. Dim tfld = DirectCast(fld, TextField)
  36. sb.Append($"TextField.Value was '{tfld.Value}', ")
  37. tfld.Value = $"Text updated on {Util.TimeNow():u}"
  38. sb.AppendLine($"now '{tfld.Value}'.")
  39. ElseIf TypeOf fld Is CheckBoxField Then
  40. Dim cfld = DirectCast(fld, CheckBoxField)
  41. sb.Append($"CheckBoxField.Value was '{cfld.Checked}', ")
  42. cfld.Checked = Not cfld.Checked
  43. sb.AppendLine($"now '{cfld.Checked}'.")
  44. ElseIf TypeOf fld Is RadioButtonField Then
  45. Dim rbfld = DirectCast(fld, RadioButtonField)
  46. sb.Append($"RadioButtonField.Value was '{rbfld.Value}', ")
  47. rbfld.Value = rbfld.Widgets.Count - 1
  48. sb.AppendLine($"now '{rbfld.Value}'.")
  49. ElseIf TypeOf fld Is ComboBoxField Then
  50. Dim cmbfld = DirectCast(fld, ComboBoxField)
  51. sb.Append($"ComboBoxField selection was '{cmbfld.Items(cmbfld.SelectedIndex).Text}', ")
  52. cmbfld.SelectedIndex = cmbfld.Items.Count - 1
  53. sb.AppendLine($"now '{cmbfld.Items(cmbfld.SelectedIndex).Text}'.")
  54. ElseIf TypeOf fld Is ListBoxField Then
  55. Dim lbfld = DirectCast(fld, ListBoxField)
  56. sb.Append($"ListBoxField selection was '{lbfld.Items(lbfld.SelectedIndex).Text}', ")
  57. lbfld.SelectedIndex = lbfld.Items.Count - 1
  58. sb.AppendLine($"now '{lbfld.Items(lbfld.SelectedIndex).Text}'.")
  59. ElseIf TypeOf fld Is SignatureField Then
  60. Dim sfld = DirectCast(fld, SignatureField)
  61. sb.AppendLine("SignatureField found.")
  62. ElseIf TypeOf fld Is PushButtonField Then
  63. Dim btnfld = DirectCast(fld, PushButtonField)
  64. sb.AppendLine($"PushButtonField '{btnfld.Widget.ButtonAppearance.Caption}' found.")
  65. Else
  66. sb.AppendLine($"Field '{fld}' found/")
  67. End If
  68. Next
  69. '' Add a log of what we did at the bottom of the page:
  70. Dim tl = New TextLayout(72) With {
  71. .MaxWidth = page.Size.Width,
  72. .MaxHeight = page.Size.Height,
  73. .MarginLeft = 80,
  74. .MarginRight = 80,
  75. .MarginBottom = 80,
  76. .ParagraphAlignment = ParagraphAlignment.Far
  77. }
  78. tl.Append(sb.ToString(), New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 12})
  79. tl.PerformLayout(True)
  80. Dim rc = tl.ContentRectangle
  81. rc.Inflate(8, 8)
  82. page.Graphics.FillRectangle(rc, Color.LightYellow)
  83. page.Graphics.DrawRectangle(rc, Color.Orange)
  84. page.Graphics.DrawTextLayout(tl, PointF.Empty)
  85. '' Done:
  86. doc.Save(stream)
  87. End Using
  88. ''
  89. Return doc.Pages.Count
  90. End Function
  91. End Class
  92.