FormFields.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Pdf;
  9. using GrapeCity.Documents.Text;
  10. using GrapeCity.Documents.Pdf.AcroForms;
  11. using GrapeCity.Documents.Pdf.Annotations;
  12. using GrapeCity.Documents.Pdf.Actions;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // This sample demonstrates how to create the various AcroForm fields
  17. // such as textbox, checkbox, push buttons and so on.
  18. public class FormFields
  19. {
  20. public int CreatePDF(Stream stream)
  21. {
  22. var doc = new GcPdfDocument();
  23. var page = doc.NewPage();
  24. var g = page.Graphics;
  25. var tf = new TextFormat();
  26. tf.Font = StandardFonts.Times;
  27. tf.FontSize = 14;
  28. var ip = new PointF(72, 72);
  29. float fldOffset = 72 * 2;
  30. float fldHeight = tf.FontSize * 1.2f;
  31. float dY = 32;
  32.  
  33. // Text field:
  34. g.DrawString("Text field:", tf, ip);
  35. var fldText = new TextField();
  36. fldText.Value = "Initial TextField value";
  37. fldText.Widget.Page = page;
  38. fldText.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight);
  39. fldText.Widget.DefaultAppearance.Font = tf.Font;
  40. fldText.Widget.DefaultAppearance.FontSize = tf.FontSize;
  41. doc.AcroForm.Fields.Add(fldText);
  42. ip.Y += dY;
  43.  
  44. // Checkbox:
  45. g.DrawString("Checkbox:", tf, ip);
  46. var fldCheckbox = new CheckBoxField();
  47. fldCheckbox.Checked = true;
  48. fldCheckbox.Widget.Page = page;
  49. fldCheckbox.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight);
  50. doc.AcroForm.Fields.Add(fldCheckbox);
  51. ip.Y += dY;
  52.  
  53. // Radio button:
  54. g.DrawString("Radio button:", tf, ip);
  55. var fldRadio = new RadioButtonField();
  56. fldRadio.Value = 1;
  57. fldRadio.Widgets.Add(new WidgetAnnotation(page, new RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight)));
  58. fldRadio.Widgets.Add(new WidgetAnnotation(page, new RectangleF(ip.X + fldOffset, ip.Y + fldHeight * 1.2f, fldHeight, fldHeight)));
  59. fldRadio.Widgets.Add(new WidgetAnnotation(page, new RectangleF(ip.X + fldOffset, ip.Y + (fldHeight * 1.2f) * 2, fldHeight, fldHeight)));
  60. doc.AcroForm.Fields.Add(fldRadio);
  61. ip.Y = fldRadio.Widgets[fldRadio.Widgets.Count - 1].Rect.Y + dY;
  62.  
  63. // CombTextField:
  64. g.DrawString("CombText field:", tf, ip);
  65. var fldCombText = new CombTextField();
  66. fldCombText.Value = "123";
  67. fldCombText.Widget.DefaultAppearance.FontSize = 12;
  68. fldCombText.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight);
  69. fldCombText.Widget.Page = page;
  70. doc.AcroForm.Fields.Add(fldCombText);
  71. ip.Y += dY;
  72.  
  73. // Combo-box:
  74. g.DrawString("Combo box:", tf, ip);
  75. var fldComboBox = new ComboBoxField();
  76. fldComboBox.Items.Add(new ChoiceFieldItem("ComboBox Choice 1"));
  77. fldComboBox.Items.Add(new ChoiceFieldItem("ComboBox Choice 2"));
  78. fldComboBox.Items.Add(new ChoiceFieldItem("ComboBox Choice 3"));
  79. fldComboBox.SelectedIndex = 1;
  80. fldComboBox.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight);
  81. fldComboBox.Widget.Page = page;
  82. doc.AcroForm.Fields.Add(fldComboBox);
  83. ip.Y += dY;
  84.  
  85. // List box:
  86. g.DrawString("List box:", tf, ip);
  87. var fldListBox = new ListBoxField();
  88. fldListBox.Items.Add(new ChoiceFieldItem("ListBox Choice 1"));
  89. fldListBox.Items.Add(new ChoiceFieldItem("ListBox Choice 2"));
  90. fldListBox.Items.Add(new ChoiceFieldItem("ListBox Choice 3"));
  91. fldListBox.SelectedIndexes = new int[] { 0, 2 };
  92. fldListBox.MultiSelect = true;
  93. fldListBox.CommitOnSelChange = true;
  94. fldListBox.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 100, 50);
  95. fldListBox.Widget.Page = page;
  96. doc.AcroForm.Fields.Add(fldListBox);
  97. ip.Y = fldListBox.Widget.Rect.Bottom - fldHeight + dY;
  98.  
  99. // Signature field:
  100. g.DrawString("Signature field:", tf, ip);
  101. var fldSignature = new SignatureField();
  102. fldSignature.AlternateName = "All fields locked when the document is signed";
  103. fldSignature.LockedFields = new SignatureLockedFields();
  104. fldSignature.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 2, 72 - dY);
  105. fldSignature.Widget.DefaultAppearance.FontSize = 8;
  106. fldSignature.Widget.ButtonAppearance.Caption = "Click to sign";
  107. fldSignature.Widget.Border = new Border() { Width = 0.5f, Color = Color.DarkSeaGreen };
  108. fldSignature.Widget.Page = page;
  109. doc.AcroForm.Fields.Add(fldSignature);
  110. ip.Y += 72 - fldHeight;
  111.  
  112. // Buttons:
  113. g.DrawString("Push buttons:", tf, ip);
  114.  
  115. // Submit form button:
  116. var btnSubmit = new PushButtonField();
  117. btnSubmit.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72, fldHeight);
  118. btnSubmit.Widget.ButtonAppearance.Caption = "Submit";
  119. btnSubmit.Widget.Highlighting = HighlightingMode.Invert;
  120. btnSubmit.Widget.Activate = new ActionSubmitForm("Sample Form Submit URI");
  121. btnSubmit.Widget.Page = page;
  122. doc.AcroForm.Fields.Add(btnSubmit);
  123. // ip.Y += dY;
  124.  
  125. // Reset form button:
  126. var btnReset = new PushButtonField();
  127. btnReset.Widget.Rect = new RectangleF(ip.X + fldOffset + 72 * 1.5f, ip.Y, 72, fldHeight);
  128. btnReset.Widget.ButtonAppearance.Caption = "Reset";
  129. btnReset.Widget.Highlighting = HighlightingMode.Invert;
  130. btnReset.Widget.Activate = new ActionResetForm();
  131. btnReset.Widget.Page = page;
  132. doc.AcroForm.Fields.Add(btnReset);
  133. ip.Y += dY;
  134.  
  135. // Done:
  136. doc.Save(stream);
  137. return doc.Pages.Count;
  138. }
  139. }
  140. }
  141.