0

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

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

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

4

1 回答 1

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 项目需要参考 itextsharp.dll(在我的例子中为 \itextsharp-all-5.3.4\itextsharp.dll),并在顶部:

导入 iTextSharp.text

导入 iTextSharp.text.pdf

于 2013-05-24T18:08:33.703 回答