P.S.
P.S.

Reputation: 33

How to add a check box in a PDF file using iText 7?

How can I add a checkobox into a Pdf file like it is done in this question, but using iText 7?

I want the result to look like this:

expected result

Upvotes: 3

Views: 8549

Answers (3)

madhu527
madhu527

Reputation: 4772

enter image description here

    Paragraph p = new Paragraph("");
    PdfFont font = PdfFontFactory.createFont(StandardFonts.ZAPFDINGBATS);
    Text chunk = new Text("4").setFont(font).setFontSize(14);
    p.add(chunk);
    Table tableSection = new Table(1);

    p.setFontColor(WebColors.getRGBColor("#ffffff"));
    Cell cell = new Cell();
    cell.add(p);
    cell.setPaddingLeft(10f);
    cell.setPaddingRight(10f);
    cell.setBackgroundColor(WebColors.getRGBColor("#000000"));
    tableSection.addCell(cell);
    document.add(tableSection);

Upvotes: 0

Alexey Subach
Alexey Subach

Reputation: 12312

Adapting Bruno's answer to iText 7:

Paragraph p = new Paragraph("This is a tick box character: ");
PdfFont zapfdingbats = PdfFontFactory.createFont(FontConstants.ZAPFDINGBATS);
Text chunk = new Text("4").setFont(zapfdingbats).setFontSize(14);
p.add(chunk);
document.add(p);

Upvotes: 5

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

You already know how to check a check box field in an interactive PDF. You now want to know how to add a check box character to a PDF (not an interactive form).

Please take a look at the TickboxCharacter example in the official documentation. This example was written in answer to Remove left and right side borders in itextshap and want a rectanglur box (a totally different question in which the OP broke the rules and asked a new question in the comments of a correct answer to his original question).

As you can tell from this example, you need a font that knows how to draw a check box. ZapfDingbats is such a font:

Paragraph p = new Paragraph("This is a tick box character: ");
Font zapfdingbats = new Font(Font.FontFamily.ZAPFDINGBATS, 14);
Chunk chunk = new Chunk("o", zapfdingbats);
p.add(chunk);
document.add(p);

Another example, putting a check mark at an absolute position, can be found here: How to write in a specific location the zapfdingbatslist in a pdf document using iTextSharp

Upvotes: 5

Related Questions