我正在使用 iTextSharp 创建“标签”、文本框和复选框,但我尝试从该代码派生以创建按钮尚未成功。
这是我创建文本框的方式:
PdfPCell cellRequesterNameTextBox = new PdfPCell()
{
CellEvent = new DynamicTextbox("textBoxRequesterName")
};
tblFirstRow.AddCell(cellRequesterNameTextBox);
. . .
public class DynamicTextbox : IPdfPCellEvent
{
private string fieldname;
public DynamicTextbox(string name)
{
fieldname = name;
}
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].PdfWriter;
float textboxheight = 11f;
Rectangle rect = rectangle;
rect.Bottom = rect.Top - textboxheight;
TextField text = new TextField(writer, rect, fieldname);
PdfFormField field = text.GetTextField();
writer.AddAnnotation(field);
}
}
这是我创建复选框的方式:
PdfPCell cell204Submitted = new PdfPCell()
{
CellEvent = new DynamicCheckbox("checkbox204Submitted")
};
tblLastRow.AddCell(cell204Submitted);
. . .
public class DynamicCheckbox : IPdfPCellEvent
{
private string fieldname;
public DynamicCheckbox(string name)
{
fieldname = name;
}
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].PdfWriter;
RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes");
ckbx.CheckType = RadioCheckField.TYPE_CHECK;
ckbx.BackgroundColor = new BaseColor(255, 197, 0);
ckbx.FontSize = 6;
ckbx.TextColor = BaseColor.WHITE;
PdfFormField field = ckbx.CheckField;
writer.AddAnnotation(field);
}
}
这是我尝试创建按钮的方式:
PdfPCell cellButton1 = new PdfPCell()
{
CellEvent = new DynamicButton("button1")
};
tblButtonRow.AddCell(cellButton1);
. . .
public class DynamicButton : IPdfPCellEvent
{
private string fieldname;
public DynamicButton(string name)
{
fieldname = name;
}
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].PdfWriter;
float buttonheight = 24f;
Rectangle rect = rectangle;
PushbuttonField btn = new PushbuttonField(writer, rect, fieldname);
PdfFormField field = btn.Field; // <= what should this be?
writer.AddAnnotation(field);
}
}
最后一个(按钮)代码有什么遗漏或错误?我认为我在 DynamicButton.CellLayout() 中对“字段”的分配一定是错误的,但它应该是什么?
以下是“按钮”的外观(底部的细长线,复选框及其相关文本/标签下方):
我怎样才能使那些纤细的“按钮”丰满/滋润?