FormSubmit.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 GrapeCity.Documents.Text
  8. Imports GrapeCity.Documents.Pdf
  9. Imports GrapeCity.Documents.Pdf.AcroForms
  10. Imports GrapeCity.Documents.Pdf.Annotations
  11. Imports GrapeCity.Documents.Pdf.Actions
  12.  
  13. '' This sample demonstrates how to create an AcroForm PDF that the user can submit.
  14. '' Here we submit it To the sample server, which receives the data and sends it back
  15. '' in a special form.
  16. Public Class FormSubmit
  17. Function CreatePDF(ByVal stream As Stream) As Integer
  18. Dim doc = New GcPdfDocument()
  19. Dim page = doc.NewPage()
  20.  
  21. Dim rc = Util.AddNote(
  22. "In this sample the Submit button is associated with the ActionSubmitForm action, " +
  23. "with the URL pointing to a POST handler running on our sample server. " +
  24. "When the form is submitted, that handler receives a collection of form field names " +
  25. "and field values from the filled form, and sends it back in a simple HTML page. " +
  26. "If you download this sample, to successfully run it you will need to set up your own " +
  27. "handler, and change the Submit button action's URL to point to that handler.",
  28. page)
  29.  
  30. Dim g = page.Graphics
  31. Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
  32. Dim ip = New PointF(72, rc.Bottom + 36)
  33. Dim fldOffset = 72.0F * 2 + 46
  34. Dim fldHeight = tf.FontSize * 1.2F
  35. Dim dY = 32.0F
  36.  
  37. '' Text field:
  38. g.DrawString("First name:", tf, ip)
  39. Dim fldFirstName = New TextField() With {.Name = "FirstName", .Value = "John"}
  40. fldFirstName.Widget.Page = page
  41. fldFirstName.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
  42. fldFirstName.Widget.DefaultAppearance.Font = tf.Font
  43. fldFirstName.Widget.DefaultAppearance.FontSize = tf.FontSize
  44. doc.AcroForm.Fields.Add(fldFirstName)
  45. ip.Y += dY
  46.  
  47. '' Text field:
  48. g.DrawString("Last name:", tf, ip)
  49. Dim fldLastName = New TextField() With {.Name = "LastName", .Value = "Smith"}
  50. fldLastName.Widget.Page = page
  51.  
  52. fldLastName.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
  53. fldLastName.Widget.DefaultAppearance.Font = tf.Font
  54. fldLastName.Widget.DefaultAppearance.FontSize = tf.FontSize
  55. doc.AcroForm.Fields.Add(fldLastName)
  56. ip.Y += dY
  57.  
  58. '' Checkbox:
  59. g.DrawString("Subscribe to Mailing List:", tf, ip)
  60. Dim fldCheckbox = New CheckBoxField() With {.Name = "Subscribe", .Checked = True}
  61. fldCheckbox.Widget.Page = page
  62. fldCheckbox.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight)
  63. doc.AcroForm.Fields.Add(fldCheckbox)
  64. ip.Y += dY
  65.  
  66. '' Multiline TextBox:
  67. g.DrawString("Additional information:", tf, ip)
  68. Dim fldAdditionalInformation = New TextField() With {.Name = "AdditionalInformation", .Multiline = True}
  69. fldAdditionalInformation.Widget.Page = page
  70. fldAdditionalInformation.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight * 2)
  71. fldAdditionalInformation.Widget.DefaultAppearance.Font = tf.Font
  72. fldAdditionalInformation.Widget.DefaultAppearance.FontSize = tf.FontSize
  73. doc.AcroForm.Fields.Add(fldAdditionalInformation)
  74. ip.Y += dY * 2
  75.  
  76. '' Submit form button:
  77. Dim btnSubmit = New PushButtonField()
  78. btnSubmit.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72, fldHeight)
  79. btnSubmit.Widget.ButtonAppearance.Caption = "Submit"
  80. btnSubmit.Widget.Highlighting = HighlightingMode.Invert
  81. btnSubmit.Widget.Page = page
  82.  
  83. '' The URL for the submission:
  84. btnSubmit.Widget.Activate = New ActionSubmitForm("/Samples/HandleFormSubmitFields")
  85. doc.AcroForm.Fields.Add(btnSubmit)
  86.  
  87. '' Reset form button:
  88. Dim btnReset = New PushButtonField()
  89. btnReset.Widget.Rect = New RectangleF(ip.X + fldOffset + 72 * 1.5F, ip.Y, 72, fldHeight)
  90. btnReset.Widget.ButtonAppearance.Caption = "Reset"
  91. btnReset.Widget.Highlighting = HighlightingMode.Invert
  92. btnReset.Widget.Activate = New ActionResetForm()
  93. btnReset.Widget.Page = page
  94. doc.AcroForm.Fields.Add(btnReset)
  95. ip.Y += dY
  96. ''
  97. '' Done:
  98. doc.Save(stream)
  99. Return doc.Pages.Count
  100. End Function
  101. End Class
  102.