How to add text to a checkbox that I create using iTextSharp?

Derived from the Jeff S methodology found here , I can add a “Checkbox” to the PDF page like this:

PdfPTable tblFirstRow = new PdfPTable(5); tblFirstRow.SpacingBefore = 4f; tblFirstRow.HorizontalAlignment = Element.ALIGN_LEFT; . . . // code where textboxes are added has been elided for brevity PdfPCell cell204Submitted = new PdfPCell() { CellEvent = new DynamicCheckbox("checkbox204Submitted", "204 Submitted or on file") }; tblFirstRow.AddCell(cell204Submitted); doc.Add(tblFirstRow); 

DynamicCheckbox class based on Jeff S CustomCellLayout class:

 public class DynamicCheckbox : IPdfPCellEvent { private string fieldname; private string cap; public DynamicCheckbox(string name, String caption) { fieldname = name; cap = caption; } public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) { PdfWriter writer = canvases[0].PdfWriter; RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes"); ckbx.CheckType = RadioCheckField.TYPE_CHECK; ckbx.Text = cap; PdfFormField field = ckbx.CheckField; writer.AddAnnotation(field); } } 

My problem is that the checkbox text (line assigned by ckbx.Text) is not displayed. A check box (oversized) occupies the last cell in the row of the table, but there is no (visible) accompanying text.

What is missing in my code?

Note. I tried to reduce the size of the checkbox by doing the following:

 Rectangle tangle = new Rectangle(20, 20); //RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes"); RadioCheckField ckbx = new RadioCheckField(writer, tangle, fieldname, "Yes"); 

... but this attempt failed - with this code I can’t even “find” a flag in the generated PDF file, clicking willy-nilly in column 5 does not cause any flags ...

0
checkbox pdf-generation itextsharp rectangles
source share
3 answers

As described in ISO-32000-1, a check box is a field of type Button. If you define text for a button, you want to define the text that appears on the button. However: in the case of a flag, there is no such text! Instead, you have two views: one for the value Off and one for the value Yes .

An enthusiastic assumption made by an attentive reader will be that you do not want to add text (to the button), but want to add a shortcut (for the check box). Again, you should refer to ISO-32000-1, and you will find that the specification says nothing about the labels for the flags. The concept simply does not exist at the AcroForm level.

This does not mean that the concept does not exist at all. Many PDF tools let you check the boxes preceded by a label. When you look inside the PDF file, you will find that this shortcut is part of the content, and the checkbox is represented by the orientation of the widgets.

Let me take a look at the official documentation, and not to upset yourself anywhere on the Internet except the official website. In particular: take a look at the Buttons from chapter 7 of my book. You will see that you can set the text for the real button:

 PushbuttonField button = new PushbuttonField(writer, rect, "Buttons"); button.setText("Push me"); 

This is not possible with flags (for the obvious reason that the appearance of the flag is completely different). If we want to add a label, we can add it, for example, as follows:

 checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "Yes"); field = checkbox.getCheckField(); field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]); field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", onOff[1]); writer.addAnnotation(field); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(LANGUAGES[i], font), 210, 790 - i * 40, 0); 

Here you can find the C # version of the following examples: http://tinyurl.com/itextsharpIIA2C07

+1
source share

Others answered this tag. The rectangle that you called the "ball" should be designed for the rectangle that is included in the event handler, similar

 Rectangle tangle = new Rectangle( rectangle.Left, rectangle.Top - PDFStyle.boxsize - 4.5f, rectangle.Left + PDFStyle.boxsize, rectangle.Top - 4.5f ); 

If PDFStyle.boxsize is the width / height of the checkbox, and 4.5f is filling the edge of the cell. Basically, a rectangle does not belong to a cell, but is absolute for a page.

+2
source share

Creating a flag, and then accompanying text on the right, can be done as follows:

 PdfPCell cell204Submitted = new PdfPCell() { CellEvent = new DynamicCheckbox("checkbox204Submitted") }; tblFirstRow.AddCell(cell204Submitted); // . . . Chunks and an anchor created; that code has been elided for brevity Paragraph parCkbxText = new Paragraph(); parCkbxText.Add(Chunk204SubmittedPreamble); parCkbxText.Add(ChunkBoldNote); parCkbxText.Add(Chunk204Midsection); parCkbxText.Add(anchorPayeeSetup204); PdfPCell cellCkbxText = new PdfPCell(parCkbxText); cellCkbxText.BorderWidth = PdfPCell.NO_BORDER; tblFirstRow.AddCell(cellCkbxText); public class DynamicCheckbox : IPdfPCellEvent { private string fieldname; public DynamicCheckbox(string name) { fieldname = name; } public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) { PdfWriter writer = canvases[0].PdfWriter; RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes"); ckbx.CheckType = RadioCheckField.TYPE_CHECK; ckbx.BackgroundColor = BaseColor.ORANGE; ckbx.FontSize = 6; ckbx.TextColor = BaseColor.WHITE; PdfFormField field = ckbx.CheckField; writer.AddAnnotation(field); } } 
+1
source share

All Articles