NoPassSetFormFields.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.Security;
using GrapeCity.Documents.Pdf.AcroForms;
using GrapeCity.Documents.Pdf.Spec;
using DsPdfWeb.Demos.Common;

namespace DsPdfWeb.Demos
{
    // This example shows how to load a password protected PDF Form without specifying the password,
    // and change the value of a text field and a comb-text field in the form.
    // The modified PDF is saved and re-opened with the password so that we can show it in the demo.
    public class NoPassSetFormFields
    {
        public int CreatePDF(Stream stream)
        {
            using var fsSrc = File.OpenRead(Path.Combine("Resources", "PDFs", "FormFields-password-user.pdf"));
            // Set up DecryptionOptions to allow loading password protected PDFs without password:
            var dopt = new DecryptionOptions() { ThrowExceptionIfInvalidPassword = false };
            var docSrc = new GcPdfDocument();
            docSrc.Load(fsSrc, dopt);

            // Change the value of CheckBoxField
            //
            // Note 1:
            // We cannot access the fields by names, like:
            //   doc.AcroForm.Fields["cbf1"];
            // because strings in a password protected PDF are encrypted
            // and cannot be accessed without specifying the password.
            // The only way is to access fields by indices.
            var cbf = (CheckBoxField)docSrc.AcroForm.Fields[0];
            cbf.Checked = true;

            // Change the value of RadioButtonField:
            var rbf = (RadioButtonField)docSrc.AcroForm.Fields[1];
            var values = rbf.GetCheckedAppearanceStreamNames();
            rbf.Value = values[0];

            // Change the value of the TextField, set it to the current date/time:
            var tf = (TextField)docSrc.AcroForm.Fields[2];
            // Note 2:
            // The value of a TextField is specified as a string, but it is not possible
            // to work with strings in a password protected PDF without specifying the password.
            // In such cases it is possible to use a workaround to set it using PdfName,
            // while it is not compliant with the PDF specification,
            // at least Adobe Acrobat supports this scenario:
            tf.PdfValue = new PdfName(Common.Util.TimeNow().ToString());

            // Change the value of a CombTextField to a random string:
            var ctf = (CombTextField)docSrc.AcroForm.Fields[3];
            var val = Util.LoremIpsum(1, 1, 1, 2, 3);
            val = val.Substring(0, Math.Min(val.Length, 10));
            ctf.PdfValue = new PdfName(val);

            // Note 3: appearance streams cannot be generated in a password protected PDF
            // that was loaded without specifying the password, so we set
            // the NeedAppearances entry of the AcroForm, which instructs Acrobat to generate
            // the missing appearance streams:
            docSrc.AcroForm.Set(PdfName.Std.NeedAppearances, PdfBool.True);

            // Demo site specific:
            // We save the modified password protected document to a temp file,
            // and load it again with the password, so that the demo site can show it
            // without asking the user for a password (the block is to delete the temp file):
            var fn = Path.GetTempFileName();
            {
                docSrc.Save(fn);
                var doc = new GcPdfDocument();
                using var fs = File.OpenRead(fn);
                doc.Load(fs, "user");
                doc.Save(stream);
            }
            File.Delete(fn);
            return docSrc.Pages.Count;
        }
    }
}