samedi 7 juillet 2018

VB.net - iTextSharp: Generate PDF with multiple checkboxes in PdfPTable

I was wasting (too) much hours for this topic, so I'm asking for your help.

I have to create a PDF with VB.net and iTextSharp (v4.1.2.0), with PdfPTable and different number of table rows, including formular fields for one textbox and one checkbox per row. I was able to create the formular field for the textbox, but not for the checkbox. The table cells for checkbox are empty (it seems there is "nothing" in these table cells).

Please find below my sample code (instead of reading data from database I go through the loop 12 times, so code is more clear), my attempt for creating the checkbox is in column 3 and 4:

    Dim objPdfDocument As New Document(PageSize.A4, 70, 30, 40, 20)

    Dim strPdfDocumentName As String = Application.StartupPath & "\Test.pdf"

    With objPdfDocument
        Dim objPdfWriter As pdf.PdfWriter = pdf.PdfWriter.GetInstance(objPdfDocument, New FileStream(strPdfDocumentName, FileMode.Create))

        .AddCreationDate()
        .AddCreator(My.Application.Info.Title & " - " & My.Application.Info.ProductName)
        .AddProducer()

        .Open()

        Dim objPosTabelle As pdf.PdfPTable = New pdf.PdfPTable(4)
        objPosTabelle.SetWidthPercentage(New Single(3) {75, 150, 150, 150}, PageSize.A4)

        Dim objPdfZelle As pdf.PdfPCell
        Dim objPdfTextField As pdf.TextField, objPdfRadioCheckField As pdf.RadioCheckField, objPdfFormField As pdf.PdfFormField, objPdfEvents As pdf.events.FieldPositioningEvents

        For i As Integer = 1 To 12
            For j As Integer = 1 To 4
                objPdfZelle = New pdf.PdfPCell
                objPdfZelle.FixedHeight = 18

                If j = 1
                    objPdfZelle =  New pdf.PdfPCell(New Phrase("Row " & i.ToString.Trim))

                Elseif j = 2
                    objPdfTextField = new pdf.TextField(objPdfWriter, new Rectangle(100, 100, 100, 100), "TextBox" & i.ToString.Trim)
                    objPdfEvents = new pdf.events.FieldPositioningEvents(objPdfWriter, objPdfTextField.GetTextField())
                    objPdfZelle.CellEvent = objPdfEvents

                Elseif j = 3
                    objPdfRadioCheckField = new pdf.RadioCheckField(objPdfWriter, new Rectangle(100, 100, 100, 100), "RadioCheck" & i.ToString.Trim, "Yes")
                    objPdfRadioCheckField.CheckType = pdf.RadioCheckField.TYPE_CHECK
                    objPdfEvents = new pdf.events.FieldPositioningEvents(objPdfWriter, objPdfRadioCheckField.GetRadioGroup(False, False))
                    objPdfZelle.CellEvent = objPdfEvents

                Elseif j = 4
                    objPdfRadioCheckField = new pdf.RadioCheckField(objPdfWriter, new Rectangle(100, 100, 100, 100), "CheckBox" & i.ToString.Trim, "Yes")
                    objPdfRadioCheckField.CheckType = pdf.RadioCheckField.TYPE_CHECK

                    objPdfFormField = objPdfRadioCheckField.CheckField
                    objPdfFormField.FieldName = "FormFeld" & i.ToString.Trim
                    objPdfFormField.AddKid(objPdfRadioCheckField.RadioField)

                    ' Error: Object could not be converted from  type ""iTextSharp.text.pdf.PdfFormField"" to type ""iTextSharp.text.IElement""
                    'objPdfZelle.AddElement(objPdfFormField)
                End If

                objPosTabelle.AddCell(objPdfZelle)
            Next
        Next

        .Add(objPosTabelle)

        .Close()
    End With

Thank you very much in advance for your help!




Aucun commentaire:

Enregistrer un commentaire