FormDataSubmit.vb
'' 完毕:
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Xml
Imports System.Text
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.AcroForms
Imports GrapeCity.Documents.Pdf.Actions
Imports GrapeCity.Documents.Pdf.Annotations
Imports GrapeCity.Documents.Text
'' 此示例创建一个可以提交到服务器的 AcroForm PDF。
'' 然后服务器使用(GcDocs.Pdf v3 中的新功能)GcPdfDocument.ImportFormDataFromCollection()
'' 方法将提交的数据导入到包含类似结构的 PDF 中
'' PDF表单,并将填写有用户提供的数据的表单发送回客户端。
''
'' 请注意,生成的 PDF 包含已填写的表单字段
'' 显示在客户端浏览器的默认 PDF 查看器中。
''
'' 此示例类似于现已过时的FormSubmitXml示例,
'' 但服务器端要简单得多,因为它使用新的 ImportFormDataFromCollection()
'' 接受与从客户端表单发送数据的方式非常相似的数据结构的方法,
'' 因此几乎不需要任何代码来操作该数据。
'' 另请参阅FormSubmit 示例。
Public Class FormDataSubmit
Function CreatePDF(ByVal stream As Stream) As Integer
Dim doc = New GcPdfDocument()
Dim page = doc.NewPage()
Dim rc = Util.AddNote(
"填写表单中的字段,然后单击“提交”将其发送回服务器。" +
"",
page)
Dim g = page.Graphics
Dim tf = New TextFormat() With {.Font = StandardFonts.Times, .FontSize = 14}
Dim ip = New PointF(72, rc.Bottom + 36)
Dim fldOffset = 72 * 2 + 46
Dim fldHeight = tf.FontSize * 1.2F
Dim dY = 32
'' 文本域:
g.DrawString("First name:", tf, ip)
Dim fldFirstName = New TextField() With {.Name = "FirstName", .Value = "John"}
fldFirstName.Widget.Page = page
fldFirstName.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
fldFirstName.Widget.DefaultAppearance.Font = tf.Font
fldFirstName.Widget.DefaultAppearance.FontSize = tf.FontSize
doc.AcroForm.Fields.Add(fldFirstName)
ip.Y += dY
'' 文本域:
g.DrawString("Last name:", tf, ip)
Dim fldLastName = New TextField() With {.Name = "LastName", .Value = "Smith"}
fldLastName.Widget.Page = page
fldLastName.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight)
fldLastName.Widget.DefaultAppearance.Font = tf.Font
fldLastName.Widget.DefaultAppearance.FontSize = tf.FontSize
doc.AcroForm.Fields.Add(fldLastName)
ip.Y += dY
'' 复选框:
g.DrawString("Subscribe to Mailing List:", tf, ip)
Dim fldCheckbox = New CheckBoxField() With {.Name = "Subscribe", .Checked = True}
fldCheckbox.Widget.Page = page
fldCheckbox.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight)
doc.AcroForm.Fields.Add(fldCheckbox)
ip.Y += dY
'' 多行文本框:
g.DrawString("Additional information:", tf, ip)
Dim fldAdditionalInfo = New TextField() With {.Name = "AdditionalInfo", .Multiline = True}
fldAdditionalInfo.Widget.Page = page
fldAdditionalInfo.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight * 2)
fldAdditionalInfo.Widget.DefaultAppearance.Font = tf.Font
fldAdditionalInfo.Widget.DefaultAppearance.FontSize = tf.FontSize
doc.AcroForm.Fields.Add(fldAdditionalInfo)
ip.Y += dY * 2
'' 提交表单按钮:
Dim btnSubmit = New PushButtonField()
btnSubmit.Widget.Rect = New RectangleF(ip.X + fldOffset, ip.Y, 72, fldHeight)
btnSubmit.Widget.ButtonAppearance.Caption = "Submit"
btnSubmit.Widget.Highlighting = HighlightingMode.Invert
btnSubmit.Widget.Page = page
'' 提交网址:
btnSubmit.Widget.Activate = New ActionSubmitForm("/Samples/HandleFormDataSubmit")
doc.AcroForm.Fields.Add(btnSubmit)
'' 重置表单按钮:
Dim btnReset = New PushButtonField()
btnReset.Widget.Rect = New RectangleF(ip.X + fldOffset + 72 * 1.5F, ip.Y, 72, fldHeight)
btnReset.Widget.ButtonAppearance.Caption = "Reset"
btnReset.Widget.Highlighting = HighlightingMode.Invert
btnReset.Widget.Page = page
btnReset.Widget.Activate = New ActionResetForm()
doc.AcroForm.Fields.Add(btnReset)
ip.Y += dY
'' 完毕:
doc.Save(stream)
Return doc.Pages.Count
End Function
'' 注意:下面的代码由 Web 示例浏览器控制器在表单时使用
'' 提交此示例准备的内容,它不是由 CreatePDF() 方法直接调用的。
'' 创建一个 GcPdfDocument,将 AcroForm PDF 加载到其中,并用数据填充它
'' 使用 GcPdfDocument.ImportFormDataFromCollection() 方法。
''
'' 当此样本准备表单时,样本控制器将调用此方法
'' 由用户提交。控制器方法将 IFormCollection 转换为它
'' 接收数组或键值对,其中键是字段名称,值是
'' 字符串值列表,然后调用此方法将值导入到
'' 兼容 ImportFormXML.pdf PDF 表单。然后将该表格发送回客户。
''
'' 调用此方法的控制器代码如下所示:
''
'' Public Function HandleFormDataSubmit(ByVal fields As IFormCollection) As IActionResult
'' Dim values = fields.ToList()
'' Dim fieldValues = values.Select(Function(kvp_) New KeyValuePair(Of String, IList(Of String))(kvp_.Key, kvp_.Value.ToArray())).ToArray()
'' Dim ms = Samples.FormDataSubmit.ImportFormData(fieldValues)
'' Dim result = New FileStreamResult(ms, "application/pdf")
'' Return result
'' End Function
''
Shared Function ImportFormData(ByVal fieldValues As KeyValuePair(Of String, IList(Of String))()) As Stream
Dim pdf = New GcPdfDocument()
Using fs = New FileStream(Path.Combine("Resources", "PDFs", "ImportFormFromCollection.pdf"), FileMode.Open, FileAccess.Read)
'' 加载兼容的空表单:
pdf.Load(fs)
'' 导入提交的数据:
pdf.ImportFormDataFromCollection(fieldValues)
'' 完毕:
Dim outMs = New MemoryStream()
pdf.Save(outMs)
outMs.Seek(0, SeekOrigin.Begin)
Return outMs
End Using
End Function
End Class