2017-03-15 9 views
0

iTextSharp pdfdcellにカスタムボーダーを追加するにはどうすればいいですか?iTextSharpカスタムボーダー

enter image description here

のような問題は、私は2行との国境を追加する方法を見つける傾けることです。

私はだけなので、結果はこの

enter image description here

のようなものですが、私は国境の下に1行を追加する必要が

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

によってpdfdcellに通常の下の境界線を作成することができます。

+0

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

+0

@VinothRaj私のC#プロジェクトで動作するコードです私はOPがテキスト行を意味するのではなく、境界線を意味すると思う。 – mkl

+1

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

答えて

0

私はC#とiTextSharpを使用しているので、コメントはJavaのソリューションを示しています。

私はこの問題を解決するために同様のことを実装しました。

基本的な事実は、iTextSharpがカスタムボーダーをサポートしていないことですが、PDF上に描画することができます。従って、目的は細胞の底に二本鎖を描くことである。

  1. 国境
  2. を既存の非表示は、トリックは、セルにCellEventを実施し、cellevent以内にそれが私たちに正確なを与えたことである
  3. ドローライン

セルの正確な位置を見つけますしたがって、我々は簡単に物を描きます。以下

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(); 
     } 
    } 
}