[英]iTextSharp: Add checkbox inside table cell with text
我想在表格的 itexsharp 单元格内添加两个复选框。 但我不知道该怎么做。 复选框不得编辑。 它们需要根据 DB 上的用户数据从代码生成,以检查是选项还是否选项。
我试试这个:
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);
没有 YES 和 NO 文本也能很好地工作,如果我只放置两个文本框,它就可以很好地工作。 问题是当我写“是”+复选框时,因为它会生成一个 extrange 图标。 任何解决方案?
由于您表示不应修改复选框,因此一种可能性是使用图像。 创建一个空复选框和一个选中的复选框。 然后添加适当的图像。
先决条件:
iTextSharp
(v. 5.5.13.3)添加以下使用指令
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
使用您最喜欢的图像编辑器创建两个复选框(例如:画图)
以下是一些可用于测试的示例:
在 Visual Studio(解决方案资源管理器)中,创建一个文件夹(名称:Images)。 将每个图像添加到文件夹中。 在属性 Window(对于每个文件名)中,将“复制到 Output 目录”设置为“始终复制”。
代码:
注意:将下面代码中的图像文件名修改为您想要的文件名(“imgChecked”和“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;
}
资源:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.