FillForm.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 System.Text;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Pdf.AcroForms;
  12.  
  13. namespace DsPdfWeb.Demos
  14. {
  15. // This sample loads the form created by the FormFields sample,
  16. // loops through all form fields found in that file,
  17. // and modifies the values of input fields.
  18. // The log of what was done (showing old and new values) is added to the form page.
  19. public class FillForm
  20. {
  21. public int CreatePDF(Stream stream)
  22. {
  23. // The original file stream must be kept open while working with the loaded PDF, see LoadPDF for details:
  24. using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "FormFields.pdf"));
  25. var doc = new GcPdfDocument();
  26. doc.Load(fs);
  27. var page = doc.Pages.Last;
  28. var sb = new StringBuilder();
  29. sb.AppendLine("Log of updates made by the FillForm sample:\r\n");
  30.  
  31. foreach (Field fld in doc.AcroForm.Fields)
  32. {
  33. if (fld is CombTextField ctfld)
  34. {
  35. sb.Append($"CombTextField.Value was '{ctfld.Value}', ");
  36. ctfld.Value = "Comb text";
  37. sb.AppendLine($"now '{ctfld.Value}'.");
  38. }
  39. else if (fld is TextField tfld)
  40. {
  41. sb.Append($"TextField.Value was '{tfld.Value}', ");
  42. tfld.Value = $"Text updated on {Common.Util.TimeNow():u}";
  43. sb.AppendLine($"now '{tfld.Value}'.");
  44. }
  45. else if (fld is CheckBoxField cfld)
  46. {
  47. sb.Append($"CheckBoxField.Value was '{cfld.Checked}', ");
  48. cfld.Checked = !cfld.Checked;
  49. sb.AppendLine($"now '{cfld.Checked}'.");
  50. }
  51. else if (fld is RadioButtonField rbfld)
  52. {
  53. sb.Append($"RadioButtonField.Value was '{rbfld.Value}', ");
  54. rbfld.Value = rbfld.Widgets.Count - 1;
  55. sb.AppendLine($"now '{rbfld.Value}'.");
  56. }
  57. else if (fld is ComboBoxField cmbfld)
  58. {
  59. sb.Append($"ComboBoxField selection was '{cmbfld.Items[cmbfld.SelectedIndex].Text}', ");
  60. cmbfld.SelectedIndex = cmbfld.Items.Count - 1;
  61. sb.AppendLine($"now '{cmbfld.Items[cmbfld.SelectedIndex].Text}'.");
  62. }
  63. else if (fld is ListBoxField lbfld)
  64. {
  65. sb.Append($"ListBoxField selection was '{lbfld.Items[lbfld.SelectedIndex].Text}', ");
  66. lbfld.SelectedIndex = lbfld.Items.Count - 1;
  67. sb.AppendLine($"now '{lbfld.Items[lbfld.SelectedIndex].Text}'.");
  68. }
  69. else if (fld is SignatureField sfld)
  70. {
  71. sb.AppendLine("SignatureField found.");
  72. }
  73. else if (fld is PushButtonField btnfld)
  74. {
  75. sb.AppendLine($"PushButtonField '{btnfld.Widget.ButtonAppearance.Caption}' found.");
  76. }
  77. else
  78. {
  79. sb.AppendLine($"Field '{fld}' found/");
  80. }
  81. }
  82. // Add a log of what we did at the bottom of the page:
  83. var tl = new TextLayout(72)
  84. {
  85. MaxWidth = page.Size.Width,
  86. MaxHeight = page.Size.Height,
  87. MarginLeft = 80,
  88. MarginRight = 80,
  89. MarginBottom = 80,
  90. ParagraphAlignment = ParagraphAlignment.Far
  91. };
  92. tl.Append(sb.ToString(), new TextFormat() { Font = StandardFonts.Times, FontSize = 12 });
  93. tl.PerformLayout(true);
  94. var rc = tl.ContentRectangle;
  95. rc.Inflate(8, 8);
  96. page.Graphics.FillRectangle(rc, Color.LightYellow);
  97. page.Graphics.DrawRectangle(rc, Color.Orange);
  98. page.Graphics.DrawTextLayout(tl, PointF.Empty);
  99.  
  100. // Done:
  101. doc.Save(stream);
  102. return doc.Pages.Count;
  103. }
  104. }
  105. }
  106.