Flattening a form field in PDF

Q: When we fill a field in PDFtron, and do a flatten, the text is gone
in the saved document. This is the simple code we use:

Dim doc4 As PDFDoc = New PDFDoc(TextBox1.Text)
doc4.InitSecurityHandler()

Dim fld As Field = doc4.GetField("{naam}")
If Not fld Is Nothing Then
fld.SetValue(“Hello world”)
fld.Flatten(doc4.GetPage(1))
Else
MsgBox(“Field search failed.”)
End If

’ Regenerate field appearances.
'doc4.FlattenAnnotations()
doc4.RefreshFieldAppearances()
doc4.FlattenAnnotations()

doc4.Save(TextBox4.Text, 0)
doc4.Close()
MsgBox(“Done.”)

If we leave out the fld.flatten or the doc4.flattenfields, it works
okay, but the field is still a field.

A: The problem is that you forgot to generate the appearance
(fld.RefreshAppearance()) before flattening the field. Your code
should look as follows:

Dim doc4 As PDFDoc = New PDFDoc(TextBox1.Text)
doc4.InitSecurityHandler()

Dim fld As Field = doc4.GetField("{naam}")
If Not fld Is Nothing Then
fld.SetValue(“Hello world”)
’ you could also refresh and flatten individual fields as follows
’ (but in that case you don’t need to call these operations on the
document)
’ fld.RefreshAppearance()
’ fld.Flatten(doc4.GetPage(1))
Else
MsgBox(“Field search failed.”)
End If

doc4.RefreshFieldAppearances()
doc4.FlattenAnnotations()

doc4.Save(TextBox4.Text, 0)
doc4.Close()