FormSubmit.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using GrapeCity.Documents.Pdf;
  6. using GrapeCity.Documents.Pdf.AcroForms;
  7. using GrapeCity.Documents.Pdf.Actions;
  8. using GrapeCity.Documents.Pdf.Annotations;
  9. using GrapeCity.Documents.Text;
  10. using System.Drawing;
  11. using System.IO;
  12.  
  13. namespace DsPdfWeb.Demos
  14. {
  15. // This sample demonstrates how to create an AcroForm PDF that the user can submit.
  16. // Here we submit it to the sample server, which receives the data and sends it back
  17. // in a special form.
  18. public class FormSubmit
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. var doc = new GcPdfDocument();
  23. var page = doc.NewPage();
  24.  
  25. var rc = Common.Util.AddNote(
  26. "In this sample the Submit button is associated with the ActionSubmitForm action, " +
  27. "with the URL pointing to a POST handler running on our sample server. " +
  28. "When the form is submitted, that handler receives a collection of form field names " +
  29. "and field values from the filled form, and sends it back in a simple HTML page. " +
  30. "If you download this sample, to successfully run it you will need to set up your own " +
  31. "handler, and change the Submit button action's URL to point to that handler.",
  32. page);
  33.  
  34. var g = page.Graphics;
  35. var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
  36. var ip = new PointF(72, rc.Bottom + 36);
  37. float fldOffset = 72 * 2 + 46;
  38. float fldHeight = tf.FontSize * 1.2f;
  39. float dY = 32;
  40.  
  41. // Text field:
  42. g.DrawString("First name:", tf, ip);
  43. var fldFirstName = new TextField() { Name = "FirstName", Value = "John" };
  44. fldFirstName.Widget.Page = page;
  45. fldFirstName.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight);
  46. fldFirstName.Widget.DefaultAppearance.Font = tf.Font;
  47. fldFirstName.Widget.DefaultAppearance.FontSize = tf.FontSize;
  48. doc.AcroForm.Fields.Add(fldFirstName);
  49. ip.Y += dY;
  50.  
  51. // Text field:
  52. g.DrawString("Last name:", tf, ip);
  53. var fldLastName = new TextField() { Name = "LastName", Value = "Smith" };
  54. fldLastName.Widget.Page = page;
  55. fldLastName.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight);
  56. fldLastName.Widget.DefaultAppearance.Font = tf.Font;
  57. fldLastName.Widget.DefaultAppearance.FontSize = tf.FontSize;
  58. doc.AcroForm.Fields.Add(fldLastName);
  59. ip.Y += dY;
  60.  
  61. // Checkbox:
  62. g.DrawString("Subscribe to Mailing List:", tf, ip);
  63. var fldCheckbox = new CheckBoxField() { Name = "Subscribe", Checked = true };
  64. fldCheckbox.Widget.Page = page;
  65. fldCheckbox.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight);
  66. doc.AcroForm.Fields.Add(fldCheckbox);
  67. ip.Y += dY;
  68.  
  69. // Multiline TextBox:
  70. g.DrawString("Additional information:", tf, ip);
  71. var fldAdditionalInfo = new TextField() { Name = "AdditionalInfo", Multiline = true };
  72. fldAdditionalInfo.Widget.Page = page;
  73. fldAdditionalInfo.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight * 2);
  74. fldAdditionalInfo.Widget.DefaultAppearance.Font = tf.Font;
  75. fldAdditionalInfo.Widget.DefaultAppearance.FontSize = tf.FontSize;
  76. doc.AcroForm.Fields.Add(fldAdditionalInfo);
  77. ip.Y += dY * 2;
  78.  
  79. // Submit form button:
  80. var btnSubmit = new PushButtonField();
  81. btnSubmit.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72, fldHeight);
  82. btnSubmit.Widget.ButtonAppearance.Caption = "Submit";
  83. btnSubmit.Widget.Highlighting = HighlightingMode.Invert;
  84. btnSubmit.Widget.Page = page;
  85.  
  86. // The URL for the submission:
  87. btnSubmit.Widget.Activate = new ActionSubmitForm("/Samples/HandleFormSubmitFields");
  88. doc.AcroForm.Fields.Add(btnSubmit);
  89.  
  90. // Reset form button:
  91. var btnReset = new PushButtonField();
  92. btnReset.Widget.Rect = new RectangleF(ip.X + fldOffset + 72 * 1.5f, ip.Y, 72, fldHeight);
  93. btnReset.Widget.ButtonAppearance.Caption = "Reset";
  94. btnReset.Widget.Highlighting = HighlightingMode.Invert;
  95. btnReset.Widget.Page = page;
  96. btnReset.Widget.Activate = new ActionResetForm();
  97. doc.AcroForm.Fields.Add(btnReset);
  98. ip.Y += dY;
  99.  
  100. // Done:
  101. doc.Save(stream);
  102. return doc.Pages.Count;
  103. }
  104. }
  105. }
  106.