我正在使用itextsharp库,我希望选中或取消选中文本旁边的复选框。但是我无法使其正常工作。
这是我的类构造函数。
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();
}
这是我的方法。
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);
}
我希望复选框旁边的文本是蓝色,但我旁边。如何在复选框旁边显示文本。非常感谢您的帮助。
答案 0 :(得分:1)
您应该使用PdfAppearance
来设置复选框(或radioButton,按钮等)
这是一个完整的例子
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 我唯一的问题是由于某种原因我无法使用您的字体,我变得胡言乱语
修改
您甚至可以更改您的复选框填充颜色(并且您可以做更多更疯狂的事情,例如在选中复选框时创建十字)
checkBoxes[1].SetRGBColorFill(255, 128, 128); //change fill color