使用iText将复选框添加到PDF文档

use*_*073 0 itext

我需要使用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();
    }
}
Run Code Online (Sandbox Code Playgroud)

有帮助吗?

Fra*_*oni 6

以下是使用Windings字体的方法:

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();
Run Code Online (Sandbox Code Playgroud)

如果你想添加任何额外的字符,只需引用Windings字符集:http://www.alanwood.net/demos/wingdings.html