28086

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

Question:

How can I add a checkobox into a Pdf file like it is done in <a href="https://stackoverflow.com/questions/34439850/itextsharp-5-5-6-enters-a-cross-symbol-when-check-type-style-is-check-mark" rel="nofollow">this</a> question, but using iText 7?

I want the result to look like this:

<a href="https://i.stack.imgur.com/H8Lil.png" rel="nofollow"><img alt="expected result" class="b-lazy" data-src="https://i.stack.imgur.com/H8Lil.png" data-original="https://i.stack.imgur.com/H8Lil.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Answer1:

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);

Answer2:

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 <em>character</em> to a PDF (not an interactive form).

Please take a look at the <a href="http://developers.itextpdf.com/examples/font-examples/showing-special-characters#1960-tickboxcharacter.java" rel="nofollow">TickboxCharacter</a> example in the official documentation. This example was written in answer to <a href="https://stackoverflow.com/questions/37991675" rel="nofollow">Remove left and right side borders in itextshap and want a rectanglur box</a> (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: <a href="https://stackoverflow.com/questions/16370428/how-to-write-in-a-specific-location-the-zapfdingbatslist-in-a-pdf-document-using" rel="nofollow">How to write in a specific location the zapfdingbatslist in a pdf document using iTextSharp</a>

Recommend