Leo Cai
Leo Cai

Reputation: 610

iTextsharp - how can I get editable field after AcroFields.SetField?

I'm using iTextSharp to fill PDF form template. I have many form fields in a pdf template. Then the software fills the fields and save the template to a new file. But, the fields in the new PDF file are no longer editable.

I want to have some of the fields still editable after calling AcroFields.SetField, is that possible?

Thanks for any answers or suggestions.

Upvotes: 0

Views: 3117

Answers (1)

rheitzman
rheitzman

Reputation: 2297

Call FormFlattening on the PDFStamper object.

                ' flatten the form to remove editting options, set it to false
                ' to leave the form open to subsequent manual edits
                pdfStamper.FormFlattening = False

Test code - path for master is in form's title:

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Dim newFile As String = Me.Text.Replace(".pdf", "_Out.pdf")

    ' create w/overwrite copy of the template
    Dim pdfReader As New PdfReader(pdfTemplate)
    Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create))

    pdfFormFields = pdfStamper.AcroFields

    ' set form pdfFormFields ' field names are case sensitive
    pdfFormFields.SetField("NAME", "Firstname Lastname")
    pdfFormFields.SetField("PHONE", "805.555.1212")

    '' report by reading values from completed PDF
    Dim sTmp As String = "Completed: " + pdfFormFields.GetField("NAME") + " " + _
    pdfFormFields.GetField("PHONE")
    MessageBox.Show(sTmp, "Finished")

    ' flatten the form to remove editting options, set it to false
    ' to leave the form open to subsequent manual edits
    pdfStamper.FormFlattening = False

    ' close the pdf
    pdfStamper.Close()
End Sub

VB Project needs reference to itextsharp.dll (in my case \itextsharp-all-5.3.4\itextsharp.dll) and at top:

Imports iTextSharp.text

Imports iTextSharp.text.pdf

Upvotes: 1

Related Questions