jeudi 28 mai 2015

Can't Check Checkboxes when the Stamper is in append mode

I'm using ItextSharp.text.pdf with Visual Basic 2012. Trying to fill out the f941.pdf from IRS.gov. All was good except it was breaking Usage Rights. So I researched that and created the stamper in append mode. This fixed the usage rights but now I cannot set the checkboxes.

   `MyReader = New PdfReader(myFileName)
    PdfOutputStream = New FileStream(outFileName, FileMode.OpenOrCreate)
    Stamper = New PdfStamper(MyReader, PdfOutputStream, Chr(0), True)
    FormAcroFields = Stamper.AcroFields
    KeysList = FormAcroFields.Fields.Keys.ToList
    SetCheck(Index, True)`

I'm not sure if the Chr(0) is correct in the stamper definition. I get no errors and the result is KeysList(List of String) is filled with the forms field keys. Calling SetCheck(FieldNumber,True) should set the checkbox.

Private Function SetCheck(ByRef Fieldnumber As Integer, Optional State As Boolean = True) As Boolean
    Dim Dstates() As String = FormAcroFields.GetAppearanceStates(KeysList(Fieldnumber))
    If State = True Then
        FormAcroFields.SetField(KeysList(Fieldnumber), Dstates(0))
    Else
        If Dstates.Length > 1 Then
            FormAcroFields.SetField(KeysList(Fieldnumber), Dstates(1))
        Else
            FormAcroFields.SetField(KeysList(Fieldnumber), "")
        End If
    End If
    Return True
End Function

Dstates(0) will hold the value to set the checkbox and if there is a Dstates(1) it would be the off value. If there is only a Dstates(0) then using "" value seems to turn them off. This worked great until I used the append mode.

I found some similar posts but the solutions were in JS and I don't know how to translate that to VB.




Aucun commentaire:

Enregistrer un commentaire