为了复制PDF中的表单,我正在执行以下操作(使用this code作为参考):
Dim pdfDocumentR As PdfDocument
Dim writer As PdfWriter = New PdfWriter(DEST)
writer.SetSmartMode(True)
Dim pdfDocumentW As New PdfDocument(writer)
Dim tmp As ByteArrayOutputStream
Dim form As PdfAcroForm
Dim fields As IDictionary(Of String, PdfFormField)
Dim tf As PdfFormField
For Each documento As Documento In documentos
tmp = New ByteArrayOutputStream()
pdfDocumentR = New PdfDocument(New PdfReader(SRC), New PdfWriter(tmp))
form = PdfAcroForm.GetAcroForm(pdfDocumentR, False)
fields = form.GetFormFields()
documento.campos.remove("templateID")
Dim keys As Dictionary(Of String, String).KeyCollection = documento.campos.Keys
For Each key As String In keys
Dim value As String
documento.campos.TryGetValue(key, value)
fields.TryGetValue(key, tf)
tf.SetValue(value)
Next
form.FlattenFields()
pdfDocumentR.Close()
pdfDocumentR = New PdfDocument(New PdfReader(New MemoryStream(tmp.GetBuffer())))
pdfDocumentR.CopyPagesTo(1, pdfDocumentR.GetNumberOfPages(), pdfDocumentW, New PdfPageFormCopier())
pdfDocumentR.Close()
Next
pdfDocumentW.Close()
当它试图压扁表单时,我得到'System.NullReferenceException' - form.FlattenFields()。
有人可以帮助我吗?