2013-10-16 120 views
2

我試圖創建基本上是表格格式的用戶列表的PDF文檔。我需要桌子上有複選框。到目前爲止,我有表格和複選框分開,但我找不到方法來獲取表格中的複選框。我想我需要使用AddCell()方法添加它們。使用iTextSharp將表格元素添加到表格

這裏是我的代碼:

static void Main(string[] args) 
    { 
     // create the document, filestream, writer 
     Document doc = new Document(PageSize.LETTER); 
     FileStream file = new FileStream(@"C:\Users\test\User List.pdf", FileMode.OpenOrCreate); 
     PdfWriter writer = PdfWriter.GetInstance(doc, file); 

     try 
     { 
      // metadata 
      doc.AddAuthor("Test User"); 
      doc.AddCreator("Test Document"); 
      doc.AddKeywords("Test Document"); 
      doc.AddSubject("Test Document"); 
      doc.AddTitle("Test Document - Test User"); 

      // open up the PDF document 
      doc.Open(); 

      PdfContentByte cb = writer.DirectContent; 
      Font _bf = new Font(Font.FontFamily.HELVETICA, 9); 

      // create a table 
      PdfPTable table = new PdfPTable(3); 
      PdfPCell cell = new PdfPCell(new Phrase("User List")); 

      PdfFormField field = PdfFormField.CreateCheckBox(writer); 
      field.SetWidget(new Rectangle(40, 500, 60, 530), PdfAnnotation.HIGHLIGHT_INVERT); 
      field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT); 
      field.FieldName = "Delete"; 
      writer.AddAnnotation(field); 

      cell.Colspan = 3; 
      cell.HorizontalAlignment = 0; //0 = left, 1 = center, 2 = right 
      cell.VerticalAlignment = 1; 
      table.AddCell("No."); 
      table.AddCell("Users"); 
      table.AddCell("Delete"); 

      // create form fields 
      PdfAppearance[] onOff = new PdfAppearance[2]; 
      Rectangle rect; 
      PdfFormField checkbox; 
      RadioCheckField _checkbox; 

      onOff[0] = cb.CreateAppearance(10, 10); 
      onOff[0].Rectangle(1, 1, 8, 8); 
      onOff[0].Stroke(); 

      onOff[1] = cb.CreateAppearance(10, 10); 
      onOff[1].SetRGBColorFill(255, 128, 128); 
      onOff[1].Rectangle(1, 1, 8, 8); 
      onOff[1].FillStroke(); 
      onOff[1].MoveTo(1, 1); 
      onOff[1].LineTo(9, 9); 
      onOff[1].MoveTo(1, 9); 
      onOff[1].LineTo(9, 1); 
      onOff[1].Stroke(); 

      for (int i = 0; i < 5; i++) 
      { 
       table.AddCell(i.ToString()); 
       table.AddCell("User " + i); 
       //rect = new Rectangle(400, 675 - i*40, 415, 663 - i*40); 
       rect = new Rectangle(400, 735 - i * 40, 415, 723 - i * 40); 
       _checkbox = new RadioCheckField(writer, rect, "User "+(i+1), "Yes"); 
       checkbox = _checkbox.CheckField; 
       checkbox.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]); 
       checkbox.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1]); 
       writer.AddAnnotation(checkbox); 

       // this is where I am trying to add the checkbox to the table 
       // what can I add to AddCell() to make the checkbox show up? 
       table.AddCell(); 
       //ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase("User " + (i + 1), _bf), 50, 665 - i * 40, 0); 
      } 

      cb = writer.DirectContent; 
      doc.Add(table); 

     } 
     catch(DocumentException dex) 
     { 
      throw (dex); 
     } 
     finally 
     { 
      // close 
      doc.Close(); 
      writer.Close(); 
      file.Close(); 
     } 
    } 

有沒有辦法做到這一點使用iTextSharp的?也許有更好的圖書館使用?

+0

我想我可以做的是圍繞表單元素繪製線條,如下所示:數字|名稱|複選框然後在上面或下面畫線條 – user

回答

4

我將參考iText website,其中顯示如何將字段傳遞給自定義IPdfPCellEvent實現和how to create that that custom class,然後在C#中將其重新創建。

首先,創建一個類實現了IPdfPCellEvent

public class ChildFieldEvent : IPdfPCellEvent { 
    /** A parent field to which a child field has to be added. */ 
    protected PdfFormField parent; 
    /** The child field that has to be added */ 
    protected PdfFormField kid; 
    /** The padding of the field inside the cell */ 
    protected float padding; 

    /** 
    * Creates a ChildFieldEvent. 
    * @param parent the parent field 
    * @param kid the child field 
    * @param padding a padding 
    */ 
    public ChildFieldEvent(PdfFormField parent, PdfFormField kid, float padding) { 
     this.parent = parent; 
     this.kid = kid; 
     this.padding = padding; 
    } 

    /** 
    * Add the child field to the parent, and sets the coordinates of the child field. 
    */ 
    public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] cb) { 
     parent.AddKid(kid); 
     kid.SetWidget(new iTextSharp.text.Rectangle(
                rect.GetLeft(padding), 
                rect.GetBottom(padding), 
                rect.GetRight(padding), 
                rect.GetTop(padding) 
                ), 
                PdfAnnotation.HIGHLIGHT_INVERT 
                ); 
    } 
} 

然後,只需設置上應該對他們有任何領域細胞CellEvent

//File to create 
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf"); 

//Standard PDF creation, nothing special here 
using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) { 
    using (var doc = new Document()) { 
     using (var writer = PdfWriter.GetInstance(doc, fs)) { 
      doc.Open(); 

      //Create a root form object that we'll add "children" to. This way we don't have to add each "child" annotation to the writer 
      var root = PdfFormField.CreateEmpty(writer); 
      root.FieldName = "root"; 

      //Create a two column table 
      var t = new PdfPTable(2); 

      //Add a basic cell 
      t.AddCell("First Name"); 

      //Create our textfield, the rectangle that we're passing in is ignored and doesn't matter 
      var tf = new TextField(writer, new iTextSharp.text.Rectangle(0, 0), "first-name"); 

      //Create a new cell 
      var cell = new PdfPCell(); 

      //Set the cell event to our custom IPdfPCellEvent implementation 
      cell.CellEvent = new ChildFieldEvent(root, tf.GetTextField(), 1); 

      //Add the cell to our table 
      t.AddCell(cell); 

      //Add the table to the document 
      doc.Add(t); 

      //IMPORTANT! Add the root annotation to the writer which also adds all of the child annotations 
      writer.AddAnnotation(root); 


      //Clean up 
      doc.Close(); 
     } 
    } 
} 
+0

謝謝,這工作。我沒有想到這一點。我確實看過那個網站,但我想我應該看起來更難看,當我找不到東西時我總是感到恐慌,我忘了冷靜下來並深入研究這些問題。 – user

+0

每當您搜索iTextSharp相關解決方案並且找不到任何東西時,也嘗試搜索iText,因爲這是父項目,並且已經存在了更長的時間。 –

+0

我還有一個問題,你知道checkbox字段在檢查完後有一個圓圈嗎?我如何改變它以顯示「X」?當用戶點擊它時,我設置了一個外觀。當用戶點擊該字段時,他們看到一個「X」,但之後,這只是一個黑點。 – user