0

I'm using visual basic in conjunction with iTextSharp to populate a PDF form.

All is wonderful except from the fact that the length of one of the form fields is too short.

From what I can determine, I need to remove the MAXLEN value for the field from the dictionary...but I'm jiggered if I can find out how to do this using VB and iTextSharp.

The field itself is called "internalP" and is currently set to a length of 4 characters. I need it set to 10 characters.

I did assume that I could edit the field somehow, but having spent several hours looking around, I think the solution is just to remove the MAXLEN property, it's just that I cannot find any example code.

Can anybody help please?

The code I am running so far is this:

Hi, my code is as follows:

 Imports iTextSharp.text.pdf
 Imports iTextSharp.text
 Imports System.IO


 Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim pdfTemplate As String = "z:\shared\LP1F.pdf"
    Dim newFile As String = "z:\shared\Final.pdf"

    Dim pdfReader As New PdfReader(pdfTemplate)
    Dim pdfStamper As New PdfStamper(pdfReader, New FileStream(newFile, FileMode.Create))

    Dim pdfFormFields As AcroFields = pdfStamper.AcroFields


    ' set form pdfFormFields
    '''
    'this was my first attempt but is not working
    'I receive a compilation error saying that I can't use nul'
    '''
     pdfFormFields.SetFieldProperty("internalP", "FieldMaxLength", 10, null)
     pdfFormFields.RegenerateField("internalP")
     pdfFormFields.SetField("internalP", "1234567890")


    '''
    ' therefore I started with this code, but then got stuck!!
    '''
    Dim item As AcroFields.Item
    item = pdfFormFields.GetFieldItem("internalP")
    Dim pdfDictionary As PdfDictionary = item.GetWidget(0)
    pdfDictionary.Remove(PdfName.MAXLEN)


    MessageBox.Show("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

End Class
Dan G
  • 23
  • 7
  • Please show the code you already have. – Amedee Van Gasse Mar 02 '16 at 18:35
  • Hi, I've updated my post with my code as it currently stands – Dan G Mar 02 '16 at 20:36
  • Do you need to programmatically change the field's size every single time or could you just go into an edit program like Adobe Acrobat, make the change once and be done with it? – Chris Haas Mar 02 '16 at 23:04
  • As it happens, for this project, once only will do, but for another project for the client, it will be dynamically assigned so I was trying to kill 2 birds with one stone. Thank you for your help though - works a treat :-) – Dan G Mar 03 '16 at 08:05

1 Answers1

1

Bruno's answer here gives you a deep understanding of what's going on and I really recommend that you read it. It is for a different property, however, so the below code should work for you.

''//Get the form item
Dim fi = pdfFormFields.GetFieldItem("internalP")

''//Get the merged propertoes
Dim props = fi.GetMerged(0)

''//Set a new value
props.Put(PdfName.MAXLEN, New PdfNumber(10))

I'd recommend throwing some null and bound checks in there but this should get you going.

Community
  • 1
  • 1
Chris Haas
  • 53,986
  • 12
  • 141
  • 274