NoPassSetFormFields.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.Pdf.Security;
  10. using GrapeCity.Documents.Pdf.AcroForms;
  11. using GrapeCity.Documents.Pdf.Spec;
  12. using DsPdfWeb.Demos.Common;
  13.  
  14. namespace DsPdfWeb.Demos
  15. {
  16. // This example shows how to load a password protected PDF Form without specifying the password,
  17. // and change the value of a text field and a comb-text field in the form.
  18. // The modified PDF is saved and re-opened with the password so that we can show it in the demo.
  19. public class NoPassSetFormFields
  20. {
  21. public int CreatePDF(Stream stream)
  22. {
  23. using var fsSrc = File.OpenRead(Path.Combine("Resources", "PDFs", "FormFields-password-user.pdf"));
  24. // Set up DecryptionOptions to allow loading password protected PDFs without password:
  25. var dopt = new DecryptionOptions() { ThrowExceptionIfInvalidPassword = false };
  26. var docSrc = new GcPdfDocument();
  27. docSrc.Load(fsSrc, dopt);
  28.  
  29. // Change the value of CheckBoxField
  30. //
  31. // Note 1:
  32. // We cannot access the fields by names, like:
  33. // doc.AcroForm.Fields["cbf1"];
  34. // because strings in a password protected PDF are encrypted
  35. // and cannot be accessed without specifying the password.
  36. // The only way is to access fields by indices.
  37. var cbf = (CheckBoxField)docSrc.AcroForm.Fields[0];
  38. cbf.Checked = true;
  39.  
  40. // Change the value of RadioButtonField:
  41. var rbf = (RadioButtonField)docSrc.AcroForm.Fields[1];
  42. var values = rbf.GetCheckedAppearanceStreamNames();
  43. rbf.Value = values[0];
  44.  
  45. // Change the value of the TextField, set it to the current date/time:
  46. var tf = (TextField)docSrc.AcroForm.Fields[2];
  47. // Note 2:
  48. // The value of a TextField is specified as a string, but it is not possible
  49. // to work with strings in a password protected PDF without specifying the password.
  50. // In such cases it is possible to use a workaround to set it using PdfName,
  51. // while it is not compliant with the PDF specification,
  52. // at least Adobe Acrobat supports this scenario:
  53. tf.PdfValue = new PdfName(Common.Util.TimeNow().ToString());
  54.  
  55. // Change the value of a CombTextField to a random string:
  56. var ctf = (CombTextField)docSrc.AcroForm.Fields[3];
  57. var val = Util.LoremIpsum(1, 1, 1, 2, 3);
  58. val = val.Substring(0, Math.Min(val.Length, 10));
  59. ctf.PdfValue = new PdfName(val);
  60.  
  61. // Note 3: appearance streams cannot be generated in a password protected PDF
  62. // that was loaded without specifying the password, so we set
  63. // the NeedAppearances entry of the AcroForm, which instructs Acrobat to generate
  64. // the missing appearance streams:
  65. docSrc.AcroForm.Set(PdfName.Std.NeedAppearances, PdfBool.True);
  66.  
  67. // Demo site specific:
  68. // We save the modified password protected document to a temp file,
  69. // and load it again with the password, so that the demo site can show it
  70. // without asking the user for a password (the block is to delete the temp file):
  71. var fn = Path.GetTempFileName();
  72. {
  73. docSrc.Save(fn);
  74. var doc = new GcPdfDocument();
  75. using var fs = File.OpenRead(fn);
  76. doc.Load(fs, "user");
  77. doc.Save(stream);
  78. }
  79. File.Delete(fn);
  80. return docSrc.Pages.Count;
  81. }
  82. }
  83. }
  84.