2

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

Community
  • 1
  • 1
P.S.
  • 23
  • 1
  • 3
  • Please have a look at [this](http://gitlab.itextsupport.com/itext7/samples/blob/develop/samples/src/test/java/com/itextpdf/samples/sandbox/acroforms/CheckboxCell.java) and [this](http://gitlab.itextsupport.com/itext7/samples/blob/develop/samples/src/test/java/com/itextpdf/samples/sandbox/acroforms/CheckboxCell2.java) sample from iText7 sandbox repository (note: the links may change), which are essentially the ported samples from Bruno's answer to the question you refer to. – Alexey Subach Jul 14 '16 at 11:38
  • Sorry, I need only picture "check" or "uncheck", no iteractive form, do you have any solution? – P.S. Jul 14 '16 at 14:01

3 Answers3

4

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

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you, but I am using iTexpdf 7.0.0 in Java... This solution not work. – P.S. Jul 15 '16 at 08:17
  • I just need shows in pdf this symilar two icons: https://cdn3.iconfinder.com/data/icons/ico-nic-lists/128/Checklist_Check_Checkmark_Checked_Unchecked_Checkbox_Properties_Property_Options_Preferences_Choose_Choice_Select_Selected_Mark_Marked_Todo_To_Do_To-do-512.png – P.S. Jul 15 '16 at 08:33
  • See the answer by @AlexeySubach if you need iText 7 (read [my chapter of fonts](http://developers.itextpdf.com/content/itext-7-building-blocks/chapter-1-introducing-pdffont-class) for more info). – Bruno Lowagie Jul 15 '16 at 15:53
4

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);
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
0

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);
madhu527
  • 4,644
  • 1
  • 28
  • 29