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

ぃ、小莉子 提交于 2019-12-08 04:40:55

问题


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:


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/38372981/how-to-add-a-check-box-in-a-pdf-file-using-itext-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!