Imports System.IO
Imports iTextSharp.text.pdf
Namespace PDFTest
Public Class PdfTest
Public Shared Sub ReplacePdfForm()
Dim fileNameExisting As String = "C:\path\to\existing.pdf"
Dim fileNameNew As String = "C:\path\to\new.pdf"
Using existingFileStream = New FileStream(fileNameExisting, FileMode.Open)
Using newFileStream = New FileStream(fileNameNew, FileMode.Create)
' Open existing PDF
Dim pdfReader = New PdfReader(existingFileStream)
' PdfStamper, which will create
Dim stamper = New PdfStamper(pdfReader, newFileStream)
Dim form = stamper.AcroFields
Dim fieldKeys = form.Fields.Keys
For Each fieldKey As String In fieldKeys
form.SetField(fieldKey, "REPLACED!")
Next
' "Flatten" the form so it wont be editable/usable anymore
stamper.FormFlattening = True
' You can also specify fields to be flattened, which
' leaves the rest of the form still be editable/usable
stamper.PartialFormFlattening("field1")
stamper.Close()
pdfReader.Close()
End Using
End Using
End Sub
End Class
End Namespace