我需要使用Java的iText库创建PDF文档。我还需要包含一些复选框,这些复选框是开/关的,具体取决于某些类变量的值。我已经找到了一些关于交互式表单的例子,但我不需要这种复杂程度:只有一些复选框被添加到这样的基本文档中:
public class SamplePDF {
public static final String RESULT = "hello.pdf";
public static void main(String[] args)
throws DocumentException, IOException {
new SamplePDF().createPdf(RESULT);
}
public void createPdf(String filename)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
document.add(new Paragraph("Document Heading"));
//
// Add Checkboxes here
//
document.close();
}
}
任何帮助?
答案 0 :(得分:6)
以下是使用Wingdings字体的方法:
BaseFont base = BaseFont.createFont("C:\\Winodws\\fonts\\wingding_0.ttf", BaseFont.IDENTITY_H, false);
Font font = new Font(base, 16f, Font.BOLD);
char checked='\u00FE';
char unchecked='\u00A8';
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
// Here is how to add a checked checkbox
document.add(new Paragraph(String.valueOf(checked),font));
Here is an unchecked checkbox
document.add(new Paragraph(String.valueOf(unchecked),font));
document.close();
如果您想添加任何额外字符,只需引用Wingdings字符集:http://www.alanwood.net/demos/wingdings.html