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. // Note that if you download this sample and run it in standalone mode,
  19. // you will need to update the submission URL so that it points to a valid server.
  20. public class FormSubmit
  21. {
  22. public int CreatePDF(Stream stream)
  23. {
  24. var doc = new GcPdfDocument();
  25. var page = doc.NewPage();
  26.  
  27. var rc = Common.Util.AddNote(
  28. "In this sample the Submit button is associated with the ActionSubmitForm action, " +
  29. "with the URL pointing to a POST handler running on our sample server. " +
  30. "When the form is submitted, that handler receives a collection of form field names " +
  31. "and field values from the filled form, and sends it back in a simple HTML page. " +
  32. "If you download this sample, to successfully run it you will need to set up your own " +
  33. "handler, and change the Submit button action's URL to point to that handler.",
  34. page);
  35.  
  36. var g = page.Graphics;
  37. var tf = new TextFormat() { Font = StandardFonts.Times, FontSize = 14 };
  38. var ip = new PointF(72, rc.Bottom + 36);
  39. float fldOffset = 72 * 2 + 46;
  40. float fldHeight = tf.FontSize * 1.2f;
  41. float dY = 32;
  42.  
  43. // Text field:
  44. g.DrawString("First name:", tf, ip);
  45. var fldFirstName = new TextField() { Name = "FirstName", Value = "John" };
  46. fldFirstName.Widget.Page = page;
  47. fldFirstName.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight);
  48. fldFirstName.Widget.DefaultAppearance.Font = tf.Font;
  49. fldFirstName.Widget.DefaultAppearance.FontSize = tf.FontSize;
  50. doc.AcroForm.Fields.Add(fldFirstName);
  51. ip.Y += dY;
  52.  
  53. // Text field:
  54. g.DrawString("Last name:", tf, ip);
  55. var fldLastName = new TextField() { Name = "LastName", Value = "Smith" };
  56. fldLastName.Widget.Page = page;
  57. fldLastName.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight);
  58. fldLastName.Widget.DefaultAppearance.Font = tf.Font;
  59. fldLastName.Widget.DefaultAppearance.FontSize = tf.FontSize;
  60. doc.AcroForm.Fields.Add(fldLastName);
  61. ip.Y += dY;
  62.  
  63. // Checkbox:
  64. g.DrawString("Subscribe to Mailing List:", tf, ip);
  65. var fldCheckbox = new CheckBoxField() { Name = "Subscribe", Checked = true };
  66. fldCheckbox.Widget.Page = page;
  67. fldCheckbox.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight);
  68. doc.AcroForm.Fields.Add(fldCheckbox);
  69. ip.Y += dY;
  70.  
  71. // Multiline TextBox:
  72. g.DrawString("Additional information:", tf, ip);
  73. var fldAdditionalInfo = new TextField() { Name = "AdditionalInfo", Multiline = true };
  74. fldAdditionalInfo.Widget.Page = page;
  75. fldAdditionalInfo.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight * 2);
  76. fldAdditionalInfo.Widget.DefaultAppearance.Font = tf.Font;
  77. fldAdditionalInfo.Widget.DefaultAppearance.FontSize = tf.FontSize;
  78. doc.AcroForm.Fields.Add(fldAdditionalInfo);
  79. ip.Y += dY * 2;
  80.  
  81. // Submit form button:
  82. var btnSubmit = new PushButtonField();
  83. btnSubmit.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72, fldHeight);
  84. btnSubmit.Widget.ButtonAppearance.Caption = "Submit";
  85. btnSubmit.Widget.Highlighting = HighlightingMode.Invert;
  86. btnSubmit.Widget.Page = page;
  87.  
  88. // The URL for the submission:
  89. btnSubmit.Widget.Activate = new ActionSubmitForm("/Samples/HandleFormSubmitFields");
  90. doc.AcroForm.Fields.Add(btnSubmit);
  91.  
  92. // Reset form button:
  93. var btnReset = new PushButtonField();
  94. btnReset.Widget.Rect = new RectangleF(ip.X + fldOffset + 72 * 1.5f, ip.Y, 72, fldHeight);
  95. btnReset.Widget.ButtonAppearance.Caption = "Reset";
  96. btnReset.Widget.Highlighting = HighlightingMode.Invert;
  97. btnReset.Widget.Page = page;
  98. btnReset.Widget.Activate = new ActionResetForm();
  99. doc.AcroForm.Fields.Add(btnReset);
  100. ip.Y += dY;
  101.  
  102. // Done:
  103. doc.Save(stream);
  104. return doc.Pages.Count;
  105. }
  106. }
  107. }
  108.