3

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

Any help ?

user2824073
  • 2,407
  • 10
  • 39
  • 73
  • 2
    You don't want it to be interactive? (i.e. can't check/uncheck it? Then why not just create a bitmap of a checkbox and insert the image into your pdf? – Rick S Oct 20 '14 at 17:39
  • No I don't need to be interactive. So I can just use images, there is no checkbox component which can be added to the document ? – user2824073 Oct 20 '14 at 17:51
  • 2
    Perhaps you could use a font like WingDings. http://www.techrepublic.com/blog/microsoft-office/use-wingdings-to-display-special-check-box-controls-in-an-access-report/ – Rick S Oct 20 '14 at 18:04

2 Answers2

8

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

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

Francesco Marchioni
  • 4,091
  • 1
  • 25
  • 40
  • When I'm using wingding.ttf checked is an 'o', unchecked is an 'x'. Can it be wingding_0.ttf is a different font? – Qohelet Dec 02 '16 at 17:15
0

PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);

    // Check symbol (Unicode character: U+2713)
    String checkSymbol = "\u2713";
    Paragraph checkParagraph = new Paragraph(checkSymbol + " Check Symbol").setFont(font);
    document.add(checkParagraph);

    // Uncheck symbol (Unicode character: U+2610)
    String uncheckSymbol = "\u2610";
    Paragraph uncheckParagraph = new Paragraph(uncheckSymbol + " Uncheck Symbol").setFont(font);
    document.add(uncheckParagraph);
nagaraja T
  • 29
  • 4