使用iText将复选框添加到PDF文档 [英] Adding a Checkbox to a PDF Document using iText

查看:1323
本文介绍了使用iText将复选框添加到PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to create a PDF Document using Java's iText libraries. I need to include as well some checkboxes, which are on/off depending on the value of some class variables. I've found some examples about interactive forms but I don't need this level of complexity: just some checkboxes which are added to a basic document like this:

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

任何帮助?

推荐答案

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

Here is how you can do it using Windings font:

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

If you want to add any extra character, just reference the Windings character set: http://www.alanwood.net/demos/wingdings.html

这篇关于使用iText将复选框添加到PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆