user
user

Reputation: 735

Add one checkbox with iTextSharp

I'm trying to add just one checkbox using the iTextSharp library for C#. I've followed some examples and tried to modify them because they show how to create a group of checkboxes. How do I create just one?

private static void WriteFooter(Document doc, int page, int maxPages, bool lastPage, PdfWriter writer, PdfAppearance[] checkBoxAppearance)
    {
        if(!lastPage)
        {
            doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall));
            doc.NewPage();
        } else
        {
            PdfContentByte cb = writer.DirectContent;
            PdfFormField _checkGroup = PdfFormField.CreateRadioButton(writer, true);
            RadioCheckField _radioG;
            PdfFormField _radioField1;
            _radioG = new RadioCheckField(writer, new Rectangle(20, 20), null, "Yes");
            _radioG.CheckType = RadioCheckField.TYPE_CHECK;
            _radioField1 = _radioG.RadioField;
            _checkGroup.AddKid(_radioField1);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("No Users Require Deletion"), 20, 20, 0);
            writer.AddAnnotation(_checkGroup);
            cb = writer.DirectContent;
            doc.Add(
                new Paragraph(
                    "Operations Management:\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
            doc.Add(
                new Paragraph(
                    "Automation Manager or Designee: \n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
        }
    }

writer, and checkBoxAppearance are passed in from another function. Previously in my code, I am creating a table with checkboxes but I need to add one outside of this table. I followed the code from http://simpledotnetsolutions.wordpress.com/2012/11/01/itextsharp-creating-form-fields/

checkBoxAppearance is an array that defines the behavior of the checkbox.

And tried to modify it to have just one checkbox. I'm also trying to get text to show up next to it.

EDIT: I am also trying this:

private static void WriteFooter(Document doc, int page, int maxPages, bool lastPage, PdfWriter writer, PdfFormField checkboxField, PdfAppearance[] checkbox, PdfContentByte cb)
    {
        if(!lastPage)
        {
            doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall));
            doc.NewPage();
        } else
        {
            PdfFormField field;
            RadioCheckField checkBox;

            for (int i = 0; i < 1; i++ )
            {
                checkBox = new RadioCheckField(writer, new Rectangle(20, 20), "noDelete", "Yes");
                field = checkBox.CheckField;
                field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", checkbox[0]);
                field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", checkbox[1]);
                writer.AddAnnotation(field);
                //ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("No Users Need Deletion"), 210, 790, 0);
            }
            doc.Add(
                new Paragraph(
                    "Operations Management:\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
            doc.Add(
                new Paragraph(
                    "Automation Manager or Designee:\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
        }
    }

Upvotes: 1

Views: 14772

Answers (1)

user
user

Reputation: 735

Never mind, I found a better way. My solution was to put the check box in a table and add it to the PDF that way. It looks much cleaner than the way my boss originally wanted it.

private static void WriteFooter(Document doc, int page, int maxPages, bool lastPage, PdfWriter writer, PdfFormField checkboxField, PdfAppearance[] checkbox, PdfContentByte cb)
    {
        if(!lastPage)
        {
            doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall));
            doc.NewPage();
        } else
        {
            PdfFormField footerField = PdfFormField.CreateEmpty(writer);
            PdfPTable footerTbl = new PdfPTable(2);
            float[] footerWidths = new float[] { 1f, 4f };
            PdfPCell noDeleteCell = new PdfPCell();
            PdfPCell noDeleteText = new PdfPCell(new Paragraph("No Users Require Deletion", Fonts.Small));
            RadioCheckField fCell = new RadioCheckField(writer, new Rectangle(20, 20), "NoDeletion", "Yes");
            fCell.CheckType = RadioCheckField.TYPE_CROSS;
            PdfFormField footerCheck = null;
            footerCheck = fCell.CheckField;
            footerCheck.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", checkbox[0]);
            footerCheck.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", checkbox[1]);
            noDeleteCell.CellEvent = new ChildFieldEvent(footerField, footerCheck, 1, 20, 20);
            footerField.FieldName = "no_delete_table";
            footerTbl.SetWidths(footerWidths);
            footerTbl.AddCell(noDeleteCell);
            footerTbl.AddCell(noDeleteText);
            doc.Add(footerTbl);
            writer.AddAnnotation(footerField);
            doc.Add(
                new Paragraph(
                    "\n\nOperations Management: _______________________________________________   Date: ___________\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
            doc.Add(
                new Paragraph(
                    "Automation Manager or Designee: _______________________________________________   Date: ___________\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
            doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall));
        }
    }

Upvotes: 4

Related Questions