Skip to content

Instantly share code, notes, and snippets.

@sharpred
Created December 3, 2010 11:01
Show Gist options
  • Save sharpred/726832 to your computer and use it in GitHub Desktop.
Save sharpred/726832 to your computer and use it in GitHub Desktop.
sample code to rename pdf fields created with period/full stops in fieldname
Function ChangeFieldNamesInForm()
Try
Dim sourcePDF As String = "source.pdf"
Dim outputPDF As String = "destination.pdf"
Dim fsw As FileStream = New FileStream(outputPDF, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim reader As PdfReader = New PdfReader(sourcePDF)
Dim stamper As PdfStamper = New PdfStamper(reader, fsw)
Dim root As PdfDictionary = reader.Catalog()
Dim form As PdfDictionary = root.GetAsDict(PdfName.ACROFORM)
Dim fields As PdfArray = form.GetAsArray(PdfName.FIELDS)
Dim stamperfields As AcroFields = stamper.AcroFields
Dim fieldkey As String
Dim test As Boolean = True
Dim fieldcount As Integer = stamperfields.Fields.Keys.Count - 1
Dim fieldsarray(fieldcount) As String
Dim i As Integer = 0
Dim item As AcroFields.Item
Dim dic As PdfDictionary
Dim rect As PdfArray
Dim newname As String
Dim name As String
Dim llx As String
Dim lly As String
Dim ulx As String
Dim uly As String
Dim fieldtype As String
Dim field As PdfFormField = Nothing
'================================================================
For Each fieldkey In stamperfields.Fields.Keys
'you need to keep a period "." for checkbox and radio button groups
newname = fieldkey.Replace(".", "_")
newname = newname.Replace("cb_", "cb.")
newname = newname.Replace("rb_", "rb.")
newname = newname.Replace("_", "")
fieldsarray(i) = fieldkey
i += 1
item = stamperfields.Fields(fieldkey)
dic = item.GetValue(0)
rect = dic.Get(PdfName.RECT)
llx = rect.ArrayList(0).ToString
lly = rect.ArrayList(1).ToString
ulx = rect.ArrayList(2).ToString
uly = rect.ArrayList(3).ToString
name = dic.Get(PdfName.T).ToString
Dim newrect As iTextSharp.text.Rectangle = New iTextSharp.text.Rectangle(llx, lly, ulx, uly)
fieldtype = dic.Get(PdfName.FT).ToString
Select Case fieldtype
Case "/Tx"
'add the new field to the form
Dim newfield As iTextSharp.text.pdf.TextField
newfield = New iTextSharp.text.pdf.TextField(stamper.Writer, newrect, newname)
field = newfield.GetTextField
Case "/Btn"
Dim responselength As Integer = newname.LastIndexOf(".")
Dim buttonresponse As String = Mid(newname, responselength + 2)
Dim checkbutton = New RadioCheckField(stamper.Writer, newrect, newname, buttonresponse)
field = checkbutton.CheckField
Case Else
End Select
'========================================
'need to work out how to determine the page number but for now go with hard coded
stamper.AddAnnotation(field, 1)
Next
'remove the original fields from the stamper
For j = 0 To fieldcount
'do not remove form instance id
test = stamperfields.RemoveField(fieldsarray(j))
If test = True Then
Console.Write(fieldsarray(j) & " removed")
End If
Next
Console.Write("all fields removed")
stamper.Close()
reader.Close()
Catch ex As Exception
Console.Write(ex.Message)
Return False
End Try
Return True
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment