I want to add two checboxes inside an itexsharp cell on a table. But I don't know how to do it. The check box must NOT be editable. They need to be generated from code, based on user data on DB to check the yes option or the no option.
I try this:
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("YES" checkBox "\n" "NO" uncheckBox, f);
cell = new PdfPCell(p);
table.AddCell(cell);
It works good without the YES and NO text, if I only put both textboxes it workd good. The problem is when I write "YES" checkbox, because it generate an extrange icon. Any solution?
CodePudding user response:
Since you indicated that the checkboxes shouldn't be modified, one possibility is to use images. Create an empty checkbox and a checked checkbox. Then add the appropriate image.
Pre-requisites:
- Download / install NuGet package:
iTextSharp
(v. 5.5.13.3)
Add the following using directives
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
Use your favorite image editor to create two checkboxes (ex: Paint)
Here are some examples which can be used for testing:
- Checkbox-Unchecked.png:
- Checkbox-CheckedBlue1.png:
- Checkbox-CheckedBlue2.png:
- Checkbox-CheckedRed.png:
- Checkbox-CheckedBlack.png:
In Visual Studio (Solution Explorer), create a folder (name: Images). Add each of your images to the folder. In the Properties Window (for each filename), set "Copy to Output Directory" to "Copy Always".
Code:
Note: Modify the image filenames in the code below to your desired filenames ("imgChecked" and "imgUnchecked").
public void CreatePdf(string filename, string checkBoxFilename)
{
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite))
{
//create new instance - specify page size and margins
using (Document doc = new Document(PageSize.LETTER, 1.0F, 1.0F, 1.0F, 1.0F))
{
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
//open
doc.Open();
//add new page
doc.NewPage();
//create table
PdfPTable table = CreatePdfTable();
//add table to Document
doc.Add(table);
//flush the FileStream
fs.Flush();
}
}
}
private PdfPTable CreatePdfTable()
{
PdfPTable table = new PdfPTable(new float[] { 40f, 18f, 50f }) { WidthPercentage = 100f };
table.TotalWidth = 555f;
//create font
var fuente_cabecera = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12f);
//column 1
PdfPCell cell = new PdfPCell(new Phrase("¿Fuma o ha fumado alguna vez?", fuente_cabecera));
cell.Rowspan = 2;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);
//column 2
Paragraph p = new Paragraph();
p = GetFilledInCheckBoxes("Yes", fuente_cabecera); //check "Yes"
//p = GetFilledInCheckBoxes("No", fuente_cabecera); //check "No"
//p = GetFilledInCheckBoxes("None", fuente_cabecera); //check "None"
cell = new PdfPCell(p);
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Rowspan = 2;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("This is text line 1", fuente_cabecera));
table.AddCell(cell);
cell = new PdfPCell(new Phrase("This is text line 2", fuente_cabecera));
table.AddCell(cell);
return table;
}
private Paragraph GetFilledInCheckBoxes(string whichCheckBoxIsChecked, Font font)
{
//create new instance
Paragraph p = new Paragraph();
//get images
Image imgChecked = Image.GetInstance(@".\Images\Checkbox-CheckedBlue1.png");
Image imgUnchecked = Image.GetInstance(@".\Images\Checkbox-Unchecked.png");
Image imgYes = null;
Image imgNo = null;
if (whichCheckBoxIsChecked == "Yes")
{
//set values
imgYes = imgChecked;
imgNo = imgUnchecked;
}
else if (whichCheckBoxIsChecked == "No")
{
//set values
imgYes = imgUnchecked;
imgNo = imgChecked;
}
else
{
//set values
imgYes = imgUnchecked;
imgNo = imgUnchecked;
}
//add appropriate checkbox
Chunk chunkCheckBoxYes = new Chunk(imgYes, 0, 0, true);
p.Add(chunkCheckBoxYes); //add to paragraph
//add text - 'Yes'
p.Add(new Chunk("Yes", font)); //add to paragraph
//add spaces
p.Add(new Chunk(" ", font)); //add to paragraph
//add appropriate checkbox
Chunk chunkCheckBoxNo = new Chunk(imgNo, 0, 0, true);
p.Add(chunkCheckBoxNo); //add to paragraph
//add text - 'No'
p.Add(new Chunk("No", font));
return p;
}
Resources: