FormDataSubmit.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.Drawing
  6. Imports System.IO
  7. Imports System.Linq
  8. Imports System.Xml
  9. Imports System.Text
  10. Imports System.Collections.Generic
  11. Imports GrapeCity.Documents.Pdf
  12. Imports GrapeCity.Documents.Pdf.AcroForms
  13. Imports GrapeCity.Documents.Pdf.Actions
  14. Imports GrapeCity.Documents.Pdf.Annotations
  15. Imports GrapeCity.Documents.Text
  16.  
  17. '' This sample creates an AcroForm PDF that can be submitted to the server.
  18. '' The server then uses the (New in DsPdf v3) GcPdfDocument.ImportFormDataFromCollection()
  19. '' method to import the submitted data into a PDF that contains a similarly structured
  20. '' PDF form, And sends the form filled with user provided data back to the client.
  21. ''
  22. '' Note that the produced PDF with filled form fields
  23. '' Is shown in the client browser's default PDF viewer.
  24. ''
  25. '' This sample Is similar to the now obsolete FormSubmitXml sample,
  26. '' but the server side Is much simpler as it uses the New ImportFormDataFromCollection()
  27. '' method that accepts a data structure very similar to how data Is sent from the client form,
  28. '' so almost no code Is needed to manipulate that data.
  29. '' See also the FormSubmit sample.
  30. Public Class FormDataSubmit
  31. Function CreatePDF(ByVal stream As Stream) As Integer
  32. Dim doc = New GcPdfDocument()
  33. Dim page = doc.NewPage()
  34.  
  35. Dim rc = Util.AddNote(
  36. "Fill the fields in the form and click 'Submit' to send it back to the server. " +
  37. "The sample server will use the GcPdfDocument.ImportFormDataFromCollection() method " +
  38. "to import the submitted data into a different but compatible PDF form, " +
  39. "and the filled form will be sent back to your browser. " +
  40. "Note that the filled form is opened in the browser's default PDF viewer, " +
  41. "and does not have the 'Submit' and 'Reset' buttons.",
  42. page)
  43.  
  44. Dim g = page.Graphics
  45. Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
  46. Dim ip = New PointF(72, rc.Bottom + 36)
  47. Dim fldOffset = 72 * 2 + 46
  48. Dim fldHeight = tf.FontSize * 1.2F
  49. Dim dY = 32
  50.  
  51. '' Text field
  52. g.DrawString("First name:", tf, ip)
  53. Dim fldFirstName = New TextField() With {.Name = "FirstName", .Value = "John"}
  54. fldFirstName.Widget.Page = page
  55. fldFirstName.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
  56. fldFirstName.Widget.DefaultAppearance.Font = tf.Font
  57. fldFirstName.Widget.DefaultAppearance.FontSize = tf.FontSize
  58. doc.AcroForm.Fields.Add(fldFirstName)
  59. ip.Y += dY
  60.  
  61. '' Text field
  62. g.DrawString("Last name:", tf, ip)
  63. Dim fldLastName = New TextField() With {.Name = "LastName", .Value = "Smith"}
  64. fldLastName.Widget.Page = page
  65. fldLastName.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
  66. fldLastName.Widget.DefaultAppearance.Font = tf.Font
  67. fldLastName.Widget.DefaultAppearance.FontSize = tf.FontSize
  68. doc.AcroForm.Fields.Add(fldLastName)
  69. ip.Y += dY
  70.  
  71. '' Checkbox
  72. g.DrawString("Subscribe to Mailing List:", tf, ip)
  73. Dim fldCheckbox = New CheckBoxField() With {.Name = "Subscribe", .Checked = True}
  74. fldCheckbox.Widget.Page = page
  75. fldCheckbox.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight)
  76. doc.AcroForm.Fields.Add(fldCheckbox)
  77. ip.Y += dY
  78.  
  79. '' Multiline TextBox
  80. g.DrawString("Additional information:", tf, ip)
  81. Dim fldAdditionalInfo = New TextField() With {.Name = "AdditionalInfo", .Multiline = True}
  82. fldAdditionalInfo.Widget.Page = page
  83. fldAdditionalInfo.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight * 2)
  84. fldAdditionalInfo.Widget.DefaultAppearance.Font = tf.Font
  85. fldAdditionalInfo.Widget.DefaultAppearance.FontSize = tf.FontSize
  86. doc.AcroForm.Fields.Add(fldAdditionalInfo)
  87. ip.Y += dY * 2
  88.  
  89. '' Submit form button:
  90. Dim btnSubmit = New PushButtonField()
  91. btnSubmit.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72, fldHeight)
  92. btnSubmit.Widget.ButtonAppearance.Caption = "Submit"
  93. btnSubmit.Widget.Highlighting = HighlightingMode.Invert
  94. btnSubmit.Widget.Page = page
  95.  
  96. '' The URL for the submission
  97. btnSubmit.Widget.Activate = New ActionSubmitForm("/Samples/HandleFormDataSubmit")
  98. doc.AcroForm.Fields.Add(btnSubmit)
  99.  
  100. '' Reset form button:
  101. Dim btnReset = New PushButtonField()
  102. btnReset.Widget.Rect = New RectangleF(ip.X + fldOffset + 72 * 1.5F, ip.Y, 72, fldHeight)
  103. btnReset.Widget.ButtonAppearance.Caption = "Reset"
  104. btnReset.Widget.Highlighting = HighlightingMode.Invert
  105. btnReset.Widget.Page = page
  106. btnReset.Widget.Activate = New ActionResetForm()
  107. doc.AcroForm.Fields.Add(btnReset)
  108. ip.Y += dY
  109.  
  110. '' Done
  111. doc.Save(stream)
  112. Return doc.Pages.Count
  113. End Function
  114.  
  115. ''
  116. '' NOTE: the code below Is used by the web sample browser controller When the form
  117. '' prepared by this sample Is submitted, it Is Not directly called by the CreatePDF() method.
  118. ''
  119.  
  120. '' Creates a GcPdfDocument, loads an AcroForm PDF into it, And fills it with data
  121. '' using the GcPdfDocument.ImportFormDataFromCollection() method.
  122. ''
  123. '' This method Is called by the samples controller when the form prepared by this sample
  124. '' Is submitted by the user. The controller method converts the IFormCollection that it
  125. '' receives to an array Or key value pairs, where keys are field names And values are
  126. '' lists of string values, then calls this method to import the values into the
  127. '' compatible ImportFormXML.pdf PDF form. That form Is then sent back to the client.
  128. ''
  129. '' The controller code that calls this method looks Like this:
  130. ''
  131. '' Public Function HandleFormDataSubmit(ByVal fields As IFormCollection) As IActionResult
  132. '' Dim values = fields.ToList()
  133. '' Dim fieldValues = values.Select(Function(kvp_) New KeyValuePair(Of String, IList(Of String))(kvp_.Key, kvp_.Value.ToArray())).ToArray()
  134. '' Dim ms = Samples.FormDataSubmit.ImportFormData(fieldValues)
  135. '' Dim result = New FileStreamResult(ms, "application/pdf")
  136. '' Return result
  137. '' End Function
  138. Shared Function ImportFormData(ByVal fieldValues As KeyValuePair(Of String, IList(Of String))()) As Stream
  139. Dim pdf = New GcPdfDocument()
  140. Using fs = New FileStream(Path.Combine("Resources", "PDFs", "ImportFormFromCollection.pdf"), FileMode.Open, FileAccess.Read)
  141. '' Load compatible empty form:
  142. pdf.Load(fs)
  143. '' Import submitted data:
  144. pdf.ImportFormDataFromCollection(fieldValues)
  145. '' Done:
  146. Dim outMs = New MemoryStream()
  147. pdf.Save(outMs)
  148. outMs.Seek(0, SeekOrigin.Begin)
  149. Return outMs
  150. End Using
  151. End Function
  152. End Class
  153.