Добавление элементов формы в таблицу с помощью iTextSharp

Я пытаюсь создать PDF-документ, который представляет собой список пользователей в табличном формате. Мне нужна таблица, чтобы в ней были флажки. Пока у меня есть таблица и флажки отдельно, но я не могу найти способ получить флажки в таблице. Я думаю, что мне нужно добавить их с помощью метода AddCell ().

Вот мой код:

static void Main(string[] args)
    {
        // create the document, filestream, writer
        Document doc = new Document(PageSize.LETTER);
        FileStream file = new FileStream(@"C:\Users\test\User List.pdf", FileMode.OpenOrCreate);
        PdfWriter writer = PdfWriter.GetInstance(doc, file);

        try
        {
            // metadata
            doc.AddAuthor("Test User");
            doc.AddCreator("Test Document");
            doc.AddKeywords("Test Document");
            doc.AddSubject("Test Document");
            doc.AddTitle("Test Document - Test User");

            // open up the PDF document
            doc.Open();

            PdfContentByte cb = writer.DirectContent;
            Font _bf = new Font(Font.FontFamily.HELVETICA, 9);

            // create a table
            PdfPTable table = new PdfPTable(3);
            PdfPCell cell = new PdfPCell(new Phrase("User List"));

            PdfFormField field = PdfFormField.CreateCheckBox(writer);
            field.SetWidget(new Rectangle(40, 500, 60, 530), PdfAnnotation.HIGHLIGHT_INVERT);
            field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
            field.FieldName = "Delete";
            writer.AddAnnotation(field);

            cell.Colspan = 3;
            cell.HorizontalAlignment = 0; //0 = left, 1 = center, 2 = right
            cell.VerticalAlignment = 1;
            table.AddCell("No.");
            table.AddCell("Users");
            table.AddCell("Delete");

            // create form fields
            PdfAppearance[] onOff = new PdfAppearance[2];
            Rectangle rect;
            PdfFormField checkbox;
            RadioCheckField _checkbox;

            onOff[0] = cb.CreateAppearance(10, 10);
            onOff[0].Rectangle(1, 1, 8, 8);
            onOff[0].Stroke();

            onOff[1] = cb.CreateAppearance(10, 10);
            onOff[1].SetRGBColorFill(255, 128, 128);
            onOff[1].Rectangle(1, 1, 8, 8);
            onOff[1].FillStroke();
            onOff[1].MoveTo(1, 1);
            onOff[1].LineTo(9, 9);
            onOff[1].MoveTo(1, 9);
            onOff[1].LineTo(9, 1);
            onOff[1].Stroke();

            for (int i = 0; i < 5; i++)
            {
                table.AddCell(i.ToString());
                table.AddCell("User " + i);
                //rect = new Rectangle(400, 675 - i*40, 415, 663 - i*40);
                rect = new Rectangle(400, 735 - i * 40, 415, 723 - i * 40);
                _checkbox = new RadioCheckField(writer, rect, "User "+(i+1), "Yes");
                checkbox = _checkbox.CheckField;
                checkbox.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
                checkbox.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1]);
                writer.AddAnnotation(checkbox);

                // this is where I am trying to add the checkbox to the table
                // what can I add to AddCell() to make the checkbox show up?
                table.AddCell();
                //ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("User " + (i + 1), _bf), 50, 665 - i * 40, 0);
            }

            cb = writer.DirectContent;
            doc.Add(table);

        }
        catch(DocumentException dex)
        {
            throw (dex);
        }
        finally
        {
            // close
            doc.Close();
            writer.Close();
            file.Close();
        }
    }

Есть ли способ сделать это с помощью iTextSharp? Может быть, есть лучшая библиотека для использования?

Ответы на вопрос(1)

Ваш ответ на вопрос