views:

33

answers:

1

I have a PDF document with a number of text fields, several of which have a maximum length - namely, a maximum number of allowable characters.

Is there a way using iTextSharp to determine this setting? Here's the code I have so far:

Dim reader As New iTextSharp.text.pdf.PdfReader("Foobar.pdf")
Dim inputFields As IDictionary(Of String, iTextSharp.text.pdf.AcroFields.Item) = reader.AcroFields.Fields

For Each key As String In inputFields.Keys
    Dim PDFFieldName As String = key
    Dim MaxFieldLength As Integer = ???

    ...
Next

I need to set MaxFieldLength to the number of allowable characters for the current form field being iterated over.

Thanks

+1  A: 

I think you're looking for something like this:

    Dim reader As New PdfReader("YourPdf.pdf")
    Dim fields As IDictionary(Of String, iTextSharp.text.pdf.AcroFields.Item) = reader.AcroFields.Fields
    For Each key As String In fields.Keys
        Dim fieldItem = reader.AcroFields.GetFieldItem(key)
        Dim pdfDictionary As PdfDictionary = fieldItem.GetWidget(0)

        Dim pdfFieldName As String = key
        Dim maxFieldLength As Integer = Int32.Parse(pdfDictionary.GetAsNumber(PdfName.MAXLEN).ToString())

        Console.WriteLine("Field={0}, MaxLen={1}", pdfFieldName, maxFieldLength.ToString())
    Next

I would like to find detailed docs on the PdfName class.

Jay Riggs
@Jay: Thanks! I won't be back at the client's site to test this until next week, but once I do I'll be sure to let you know if it worked or not.
Scott Mitchell
@Jay: Made it out to the client's site yesterday and had the opportunity to implement this code - worked as advertised, thanks!
Scott Mitchell
@Scott - great!
Jay Riggs