Reputation: 752
I am using itextsharp library i want to have a checkbox checked or uncheck next to text.But I am unable to make it work.
Here is my class constructor.
public pdfCreator(string fileName)
{
//Create document
pdfDoc = new Document(PageSize.A4, 25, 25, 25, 15);
//Create a PDF file in specific path
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, new FileStream(System.Web.HttpContext.Current.Server.MapPath(fileName+".pdf"), FileMode.Create));
pdfDoc.Open();
}
Here is my method.
public void chkBoxesCreator()
{
string FONT = "c:/windows/fonts/WINGDING.TTF";
string checkBox = "\u00fe";
string uncheckBox = "o";
BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
Paragraph p = new Paragraph(checkBox, f);
Paragraph p2 = new Paragraph(uncheckBox, f);
pdfDoc.Add(p);
pdfDoc.Add(p2);
}
I want text next to checkboxes that i but in blue. How can i get text next to my checkboxes. Thank you very much for any help.
Upvotes: 0
Views: 782
Reputation: 1915
You should use PdfAppearance
to set the checkbox(or radioButton, button, etc. )
Here's a full example
public static void chkBoxesCreator()
{
String[] texts = { "one", "two", "three" };
using (var pdfDoc = new Document(PageSize.A4))
using (var output = new FileStream(fileLoc, FileMode.Create))
using (var writer = PdfWriter.GetInstance(pdfDoc, output))
{
{
pdfDoc.Open();
PdfContentByte cb = writer.DirectContent;
Rectangle _rect;
PdfFormField _Field1;
PdfAppearance[] checkBoxes = new PdfAppearance[2];
//set first checkBox style
checkBoxes[0] = cb.CreateAppearance(20, 20);
checkBoxes[0].Rectangle(1, 1, 18, 18);
checkBoxes[0].Stroke();
//set second checkBox style
checkBoxes[1] = cb.CreateAppearance(20, 20);
checkBoxes[1].Rectangle(1, 1, 18, 18);
checkBoxes[1].FillStroke();
RadioCheckField _checkbox1;
for (int i = 0; i < texts.Length; i++)
{
_rect = new Rectangle(180, 806 - i * 40, 200, 788 - i * 40); //can be any location
_checkbox1 = new RadioCheckField(writer, _rect, texts[i], "on");
_Field1 = _checkbox1.CheckField;
_Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", checkBoxes[0]);
_Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", checkBoxes[1]);
writer.AddAnnotation(_Field1);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(texts[i], new Font(Font.FontFamily.HELVETICA, 18)), 210, 790 - i * 40, 0);
}
cb = writer.DirectContent;
pdfDoc.Close();
}
}
}
PS The only problem i have that i couldn't use your font for some reason i was getting gibberish
Edit
You could even change you checkBox fill Color with(and you could make any more crazy thing like create a cross when the checkbox is checked)
checkBoxes[1].SetRGBColorFill(255, 128, 128); //change fill color
And the output is(with SetRGBColorFill
)
Upvotes: 1