如何使用 iText 7 在 PDF 文件中添加复选框?

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

如何像 this 问题中那样将复选框添加到 Pdf 文件中,但使用的是 iText 7?

我希望结果如下所示:

您已经知道如何在交互式 PDF 中选中复选框字段。您现在想知道如何向 PDF(不是交互式表单)添加复选框 字符

请看一下 TickboxCharacter example in the official documentation. This example was written in answer to (一个完全不同的问题,OP 违反了规则,并在对他原始问题的正确答案的评论中提出了一个新问题)。

从这个例子可以看出,您需要一种知道如何绘制复选框的字体。 ZapfDingbats就是这样的字体:

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

另一个例子,在绝对位置打勾,可以在这里找到:How to write in a specific location the zapfdingbatslist in a pdf document using iTextSharp

调整 Bruno 对 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);