iTextsharp - 如何在AcroFields.SetField之后获得可编辑字段?

时间:2013-05-24 02:49:05

标签: itextsharp

我使用iTextSharp填写PDF表单模板。我在pdf模板中有很多表单字段。然后,软件填充字段并将模板保存到新文件。但是,新PDF文件中的字段不再可编辑。

我希望在调用AcroFields.SetField之后仍然可以编辑某些字段,这可能吗?

感谢您的任何答案或建议。

1 个答案:

答案 0 :(得分:1)

在PDFStamper对象上调用FormFlattening。

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

测试代码 - master的路径在表单的标题中:

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需要引用itextsharp.dll(在我的例子中是\ itextsharp-all-5.3.4 \ itextsharp.dll)并在顶部:

导入iTextSharp.text

导入iTextSharp.text.pdf