''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Pdf.Security
Imports GrapeCity.Documents.Pdf.AcroForms
Imports GrapeCity.Documents.Pdf.Spec
'' 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 Function CreatePDF(ByVal stream As Stream) As Integer
Using fsSrc = File.OpenRead(Path.Combine("Resources", "PDFs", "FormFields-password-user.pdf"))
'' Set up DecryptionOptions to allow loading password protected PDFs without password:
Dim dopt = New DecryptionOptions() With {.ThrowExceptionIfInvalidPassword = False}
Dim 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.
Dim cbf = DirectCast(docSrc.AcroForm.Fields(0), CheckBoxField)
cbf.Checked = True
'' Change the value of RadioButtonField:
Dim rbf = DirectCast(docSrc.AcroForm.Fields(1), RadioButtonField)
Dim values = rbf.GetCheckedAppearanceStreamNames()
rbf.Value = values(0)
'' Change the value of the TextField, set it to the current date/time:
Dim tf = DirectCast(docSrc.AcroForm.Fields(2), TextField)
' 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(Util.TimeNow().ToString())
'' Change the value of a CombTextField to a random string:
Dim ctf = DirectCast(docSrc.AcroForm.Fields(3), CombTextField)
Dim 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:
Dim fn = Path.GetTempFileName()
docSrc.Save(fn)
Dim doc = New GcPdfDocument()
Using fs = File.OpenRead(fn)
doc.Load(fs, "user")
doc.Save(stream)
End Using
File.Delete(fn)
'' Done:
Return docSrc.Pages.Count
End Using
End Function
End Class