首页 文章

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

提问于
浏览
0

我需要使用 Java 的 iText 库创建 PDF 文档。我还需要包含一些复选框,这些复选框是 on/off,具体取决于某些类变量的值。我已经找到了一些关于交互式表单的例子,但是我不需要这种复杂程度:只有一些复选框被添加到基本文档中,如下所示:

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

有帮助吗?

1 回答

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

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

相关问题