//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingSystem.Text;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Text;usingGrapeCity.Documents.Pdf.AcroForms; namespaceDsPdfWeb.Demos{ // This sample loads the form created by the FormFields sample,// loops through all form fields found in that file,// and modifies the values of input fields.// The log of what was done (showing old and new values) is added to the form page.publicclassFillForm {publicintCreatePDF(Stream stream) { // The original file stream must be kept open while working with the loaded PDF, see LoadPDF for details:usingvar fs = File.OpenRead(Path.Combine("Resources", "PDFs", "FormFields.pdf"));var doc = newGcPdfDocument(); doc.Load(fs);var page = doc.Pages.Last;var sb = newStringBuilder(); sb.AppendLine("Log of updates made by the FillForm sample:\r\n"); foreach (Field fld in doc.AcroForm.Fields) {if (fld isCombTextField ctfld) { sb.Append($"CombTextField.Value was '{ctfld.Value}', "); ctfld.Value = "Comb text"; sb.AppendLine($"now '{ctfld.Value}'."); }elseif (fld isTextField tfld) { sb.Append($"TextField.Value was '{tfld.Value}', "); tfld.Value = $"Text updated on {Common.Util.TimeNow():u}"; sb.AppendLine($"now '{tfld.Value}'."); }elseif (fld isCheckBoxField cfld) { sb.Append($"CheckBoxField.Value was '{cfld.Checked}', "); cfld.Checked = !cfld.Checked; sb.AppendLine($"now '{cfld.Checked}'."); }elseif (fld isRadioButtonField rbfld) { sb.Append($"RadioButtonField.Value was '{rbfld.Value}', "); rbfld.Value = rbfld.Widgets.Count - 1; sb.AppendLine($"now '{rbfld.Value}'."); }elseif (fld isComboBoxField cmbfld) { sb.Append($"ComboBoxField selection was '{cmbfld.Items[cmbfld.SelectedIndex].Text}', "); cmbfld.SelectedIndex = cmbfld.Items.Count - 1; sb.AppendLine($"now '{cmbfld.Items[cmbfld.SelectedIndex].Text}'."); }elseif (fld isListBoxField lbfld) { sb.Append($"ListBoxField selection was '{lbfld.Items[lbfld.SelectedIndex].Text}', "); lbfld.SelectedIndex = lbfld.Items.Count - 1; sb.AppendLine($"now '{lbfld.Items[lbfld.SelectedIndex].Text}'."); }elseif (fld isSignatureField sfld) { sb.AppendLine("SignatureField found."); }elseif (fld isPushButtonField btnfld) { sb.AppendLine($"PushButtonField '{btnfld.Widget.ButtonAppearance.Caption}' found."); }else { sb.AppendLine($"Field '{fld}' found/"); } }// Add a log of what we did at the bottom of the page:var tl = newTextLayout(72) {MaxWidth = page.Size.Width,MaxHeight = page.Size.Height,MarginLeft = 80,MarginRight = 80,MarginBottom = 80,ParagraphAlignment = ParagraphAlignment.Far }; tl.Append(sb.ToString(), newTextFormat() { Font = StandardFonts.Times, FontSize = 12 }); tl.PerformLayout(true);var rc = tl.ContentRectangle; rc.Inflate(8, 8); page.Graphics.FillRectangle(rc, Color.LightYellow); page.Graphics.DrawRectangle(rc, Color.Orange); page.Graphics.DrawTextLayout(tl, PointF.Empty); // Done: doc.Save(stream);return doc.Pages.Count; } }}