2017-03-15 77 views
0

我想知道如何將自定義邊框附加到iTextSharp pdfdcell。iTextsharp自定義邊框

enter image description here

的問題是,我不能找到一種方法,用2行添加邊框。

我只能通過

PdfPCell tmp = new PdfPCell(new Phrase(c)); 
tmp.HorizontalAlignment = Element.ALIGN_RIGHT; 
tmp.Border = c.borderPosition; 
tmp.BorderWidth = c.borderWidth; 
pt.AddCell(tmp); 

創建於pdfdcell正常的下邊框所以結果是這樣的

enter image description here

,但我需要添加邊框下多了一個線。

+0

http://stackoverflow.com/questions/21939280/itextsharp-multiple-lines-in-pdfpcell-one-under-another –

+0

@VinothRaj工作的代碼我認爲OP並不意味着文本行,而是邊界線。 – mkl

+1

[http://stackoverflow.com/questions/31588087](http://stackoverflow.com/questions/31588087) – kuujinbo

回答

0

由於我使用的是C#和iTextSharp,並且評論僅顯示了Java上的解決方案。

我已經實施了類似的事情來解決問題。

基本事實是iTextSharp不支持自定義邊框,但它允許您在PDF上繪製東西。因此我們的目標是在單元格底部畫一條雙線。

  1. 隱藏現有邊界
  2. 找出細胞的準確位置
  3. 畫線

的訣竅是,實現對細胞的CellEvent,在cellevent內它給了我們確切細胞的位置,因此我們很容易畫東西。

下面

是在我的C#項目

public function void DrawACell_With_DOUBLELINE_BOTTOM_BORDER(Document doc, PdfWriter writer){ 
    PdfPTable pt = new PdfPTable(new float[]{1}); 
    Chunk c = new Chunk("A Cell with doubleline bottom border"); 
    int padding = 3; 
    PdfPCell_DoubleLine cell = new PdfPCell_DoubleLine(PdfPTable pt,new Phrase(c), writer, padding); 
    pt.AddCell(cell); 
    doc.Add(pt); 
} 

public class PdfPCell_DoubleLine : PdfPCell 
{ 
    public PdfPCell_DoubleLine(Phrase phrase, PdfWriter writer, int padding) : base(phrase) 
    { 
     this.HorizontalAlignment = Element.ALIGN_RIGHT; 
     //1. hide existing border 
     this.Border = Rectangle.NO_BORDER; 
     //2. find out the exact position of the cell 
     this.CellEvent = new DLineCell(writer, padding); 
    } 
    public class DLineCell : IPdfPCellEvent 
    { 
     public PdfWriter writer { get; set; } 
     public int padding { get; set; } 
     public DLineCell(PdfWriter writer, int padding) 
     { 
      this.writer = writer; 
      this.padding = padding; 
     } 

     public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvases) 
     { 
      //draw line 1 
      PdfContentByte cb = writer.DirectContent; 
      cb.MoveTo(rect.GetLeft(0), rect.GetBottom(0) - padding); 
      cb.LineTo(rect.GetRight(0), rect.GetBottom(0) - padding); 
      //draw line 2 
      cb.MoveTo(rect.GetLeft(0), rect.GetBottom(0) - padding - 2); 
      cb.LineTo(rect.GetRight(0), rect.GetBottom(0) - padding - 2); 
      cb.Stroke(); 
     } 
    } 
}