FormSubmitSound.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 is similar to the FormSubmit sample, but adds ActionSound to the
  16. // form submit and form reset buttons, so that the user gets audible feedback
  17. // when they submit or reset the 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 FormSubmitSound
  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. "As in the 'Submit Form' sample, the Submit button's Activate is associated with ActionSubmitForm, " +
  29. "and the Reset button's Activate is associated with ActionResetForm. " +
  30. "\nIn addition to these handlers, in this sample the buttons' MouseDown events are associated with ActionSound, " +
  31. "so that the user gets appropriate audible feedback when clicking the button.",
  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. // The URL for the submission:
  86. btnSubmit.Widget.Activate = new ActionSubmitForm("/Samples/HandleFormSubmitFields");
  87. // Also, associated the MouseDown event with a confirmation sound:
  88. btnSubmit.Widget.Events.MouseDown = new ActionSound(SoundObject.FromFile(Path.Combine("Resources", "Sounds", "form-submitted.wav")));
  89. doc.AcroForm.Fields.Add(btnSubmit);
  90.  
  91. // Reset form button:
  92. var btnReset = new PushButtonField();
  93. btnReset.Widget.Rect = new RectangleF(ip.X + fldOffset + 72 * 1.5f, ip.Y, 72, fldHeight);
  94. btnReset.Widget.ButtonAppearance.Caption = "Reset";
  95. btnReset.Widget.Highlighting = HighlightingMode.Invert;
  96. btnReset.Widget.Page = page;
  97. // Reset the form on button activation:
  98. btnReset.Widget.Activate = new ActionResetForm();
  99. // Also, associated the MouseDown event with a confirmation sound:
  100. btnReset.Widget.Events.MouseDown = new ActionSound(SoundObject.FromFile(Path.Combine("Resources", "Sounds", "form-reset.wav")));
  101. doc.AcroForm.Fields.Add(btnReset);
  102. ip.Y += dY;
  103.  
  104. // Done:
  105. doc.Save(stream);
  106. return doc.Pages.Count;
  107. }
  108. }
  109. }
  110.