2

how can I draw checked box (with X) with iTextSharp. I don't want it to be image. More like symbol I tried this, but it didn't work:

RadioCheckField fCell = new RadioCheckField(writer, new Rectangle(20,20), "NoDeletion", "Yes");
fCell.CheckType = RadioCheckField.TYPE_CROSS;
PdfFormField footerCheck = null;
footerCheck = fCell.CheckField;
writer.AddAnnotation(footerCheck);

Thanks,

PowerPuffGirl
  • 81
  • 1
  • 2
  • 9
  • 1
    The code you are using isn't about drawing a checked box. It's about drawing an interactive check box. Do you want the check box to be interactive? Or do you just want to draw a symbol of a check box? See the extra answer to [this question](http://stackoverflow.com/a/37991842/1622493). Do you have a font with the checked box character you want to display? – Bruno Lowagie Aug 17 '16 at 10:44

2 Answers2

5

I assume that you don't need an interactive check box. You just want to use a check box character. The first thing you need, is a font that has such a character. When I work on Windows, I have access to a file named WINGDING.TTF. This is a font program that contains all kinds of symbols, among others some check boxes:

enter image description here

I created the PDF shown in the screen shot like this:

public static final String DEST = "results/fonts/checkbox_character.pdf";
public static final String FONT = "c:/windows/fonts/WINGDING.TTF";
public static final String TEXT = "o x \u00fd \u00fe";

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.GetInstance(document, new FileOutputStream(dest));
    document.Open();
    BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font f = new Font(bf, 12);
    Paragraph p = new Paragraph(TEXT, f);
    document.Add(p);
    document.Close();
}

As you can see, the empty check box corresponds with the letter o, there are three variations of a checked check box.

J.C
  • 632
  • 1
  • 10
  • 41
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
0

Thank you, but I found a solution. Here it is:

var checkBox = new Phrase();
checkBox.Add(new Chunk(" ☒ ", new Font(BaseFont.CreateFont(PrintCommonConstants.UnicodeFontNormal, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 18)));
PowerPuffGirl
  • 81
  • 1
  • 2
  • 9
  • It's a bad idea to put the check box character in your code as-is. This can lead to all kinds of encoding problems. It's better to use a Unicode notations. See my answer for more info. – Bruno Lowagie Aug 17 '16 at 11:26