我使用iTextSharp創建「標籤」和文本框和複選框,但是我試圖從該代碼派生以創建按鈕尚未成功。如何創建使用iTextSharp添加到PDF文件的按鈕?
這裏是我是如何創建文本框:
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()中的「field」賦值必須是錯誤的,但它應該是什麼?
這裏是如何的「按鈕」的外觀(長瘦的線在底部,複選框之下,與其相關的文本/標籤):
我怎麼能胖起來/滋潤那些twiggified 「鈕釦」?