2015-04-16 84 views
0

我使用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」賦值必須是錯誤的,但它應該是什麼?

這裏是如何的「按鈕」的外觀(長瘦的線在底部,複選框之下,與其相關的文本/標籤):

enter image description here

我怎麼能胖起來/滋潤那些twiggified 「鈕釦」?

回答

1

您沒有顯示代碼中最重要的部分:您沒有展示如何創建爲其定義按鈕的單元格。如果您要檢查rect(更具體地說它的高度)的值,您會發現您可能正在創建高度爲0的單元格。爲什麼高度爲零?因爲他們可能不包含任何東西。

單元格是否需要包含任何東西?當然不是,但如果你想避免零高度的行,你必須定義一個高度。這在我的書的Chapter 4中有解釋,例如在CellHeights的例子中。我爲這個相當神祕的名字道歉,但這個例子是關於細胞的高度。

您可以定義一個固定的高度:

cell.FixedHeight = 72f; 

這是一個危險的特性:如果添加了不適合的固定高度(例如72個用戶單位)裏面的內容,這些內容都將丟失。

您還可以定義一個最小高度:

cell.MinimumHeight = 36f; 

在這種情況下,高度肯定會有36個用戶單位(在這種情況下),但它可能是更多,如果有比配合更多的內容最小高度。

相關問題