FillForm.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Text;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Pdf.AcroForms;

namespace DsPdfWeb.Demos
{
    // 此示例​​加载由FormFields示例创建的表单,
    // 循环遍历该文件中找到的所有表单字段,
    // 并修改输入字段的值。
    // 所做操作的日志(显示旧值和新值)将添加到表单页面。
    public class FillForm
    {
        public int CreatePDF(Stream stream)
        {
            // 在处理加载的PDF时,原始文件流必须保持打开状态,请参阅LoadPDF了解详细信息:
            using var fs = File.OpenRead(Path.Combine("Resources", "PDFs", "FormFields.pdf"));
            var doc = new GcPdfDocument();
            doc.Load(fs);
            var page = doc.Pages.Last;
            var sb = new StringBuilder();
            sb.AppendLine("Log of updates made by the FillForm sample:\r\n");

            foreach (Field fld in doc.AcroForm.Fields)
            {
                if (fld is CombTextField ctfld)
                {
                    sb.Append($"CombTextField.Value was '{ctfld.Value}', ");
                    ctfld.Value = "Comb text";
                    sb.AppendLine($"now '{ctfld.Value}'.");
                }
                else if (fld is TextField tfld)
                {
                    sb.Append($"TextField.Value was '{tfld.Value}', ");
                    tfld.Value = $"Text updated on {Common.Util.TimeNow():u}";
                    sb.AppendLine($"now '{tfld.Value}'.");
                }
                else if (fld is CheckBoxField cfld)
                {
                    sb.Append($"CheckBoxField.Value was '{cfld.Checked}', ");
                    cfld.Checked = !cfld.Checked;
                    sb.AppendLine($"now '{cfld.Checked}'.");
                }
                else if (fld is RadioButtonField rbfld)
                {
                    sb.Append($"RadioButtonField.Value was '{rbfld.Value}', ");
                    rbfld.Value = rbfld.Widgets.Count - 1;
                    sb.AppendLine($"now '{rbfld.Value}'.");
                }
                else if (fld is ComboBoxField 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}'.");
                }
                else if (fld is ListBoxField 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}'.");
                }
                else if (fld is SignatureField sfld)
                {
                    sb.AppendLine("SignatureField found.");
                }
                else if (fld is PushButtonField btnfld)
                {
                    sb.AppendLine($"PushButtonField '{btnfld.Widget.ButtonAppearance.Caption}' found.");
                }
                else
                {
                    sb.AppendLine($"Field '{fld}' found/");
                }
            }
            // 在页面底部添加我们所做操作的日志:
            var tl = new TextLayout(72)
            {
                MaxWidth = page.Size.Width,
                MaxHeight = page.Size.Height,
                MarginLeft = 80,
                MarginRight = 80,
                MarginBottom = 80,
                ParagraphAlignment = ParagraphAlignment.Far
            };
            tl.Append(sb.ToString(), new TextFormat() { 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);

            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}