2010-03-31 125 views

回答

13

如果你有iTextSharp的源代码,添加以下PdfContentByte类:

/// <summary> 
     /// Enumuration for defining corners you want rounded. 
     /// </summary> 
     [Flags()] 
     public enum Corners {None = 0,All=15,Top=3,Bottom=12, TopLeft = 1, TopRight=2, BottomLeft=4, BottomRight=8}; 

     /// <summary> 
     /// Adds a round rectangle to the current path, with rounded conrners as specified by roundCorners. 
     /// </summary> 
     /// <param name="x"></param> 
     /// <param name="y"></param> 
     /// <param name="w"></param> 
     /// <param name="h"></param> 
     /// <param name="r"></param> 
     /// <param name="roundCorners"></param> 
     public void RoundRectangle(float x, float y, float w, float h, float r,Corners roundCorners) 
     { 
      if (w < 0) 
      { 
       x += w; 
       w = -w; 
      } 
      if (h < 0) 
      { 
       y += h; 
       h = -h; 
      } 
      if (r < 0) 
       r = -r; 
      float b = 0.4477f; 
      if((roundCorners & Corners.BottomLeft) == Corners.BottomLeft) 
       MoveTo(x + r, y); 
      else 
       MoveTo(x, y); 

      if ((roundCorners & Corners.BottomRight) == Corners.BottomRight) 
      { 
       LineTo(x + w - r, y); 
       CurveTo(x + w - r * b, y, x + w, y + r * b, x + w, y + r); 
      } 
      else 
       LineTo(x + w, y); 

      if ((roundCorners & Corners.TopRight) == Corners.TopRight) 
      { 
       LineTo(x + w, y + h - r); 
       CurveTo(x + w, y + h - r * b, x + w - r * b, y + h, x + w - r, y + h); 
      } 
      else 
       LineTo(x + w, y + h); 

      if ((roundCorners & Corners.TopLeft) == Corners.TopLeft) 
      { 
       LineTo(x + r, y + h); 
       CurveTo(x + r * b, y + h, x, y + h - r * b, x, y + h - r); 
      } 
      else 
       LineTo(x , y + h); 

      if ((roundCorners & Corners.BottomLeft) == Corners.BottomLeft) 
      { 
       LineTo(x, y + r); 
       CurveTo(x, y + r * b, x + r * b, y, x + r, y); 
      }else 
       LineTo(x, y); 
     }   

下一步是implment IPdfPCellEvent接口:

public class myCellEvent : IPdfPCellEvent 
    { 
     #region members 
     PdfContentByte.Corners _roundedCorners; 
     float _roundness; 
     #endregion 

     #region ctor 
     public myCellEvent() 
      :this(PdfContentByte.Corners.All,2f) 
     { 

     } 

     public myCellEvent(PdfContentByte.Corners roundedCorners) 
      : this(roundedCorners, 2f) 
     { 

     } 

     public myCellEvent(PdfContentByte.Corners roundedCorners,float roundness) 
     { 
      _roundedCorners = roundedCorners; 
      _roundness = roundness; 

     } 
     #endregion 

     #region IPdfPCellEvent Members 
     public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) 
     { 
      PdfContentByte cb = canvases[PdfPTable.LINECANVAS]; 
      cb.RoundRectangle(position.Left, position.Bottom, position.Right - position.Left, position.Top - position.Bottom, this._roundness, this._roundedCorners); 
      cb.SetColorStroke(new BaseColor(System.Drawing.Color.Black)); 
      cb.SetColorFill(new BaseColor(System.Drawing.Color.Beige));    
      cb.FillStroke(); 
      cb.Stroke(); 
     } 
     #endregion 
    } 

然后你周围,你想生产什么都角落圆形表的选择:

PdfPTable table = new PdfPTable(1); 


       PdfPCell cell = new PdfPCell(new Phrase(10, atext, f2)); 
       cell.Border = 0; 
       cell.Padding = 5f; 
       cell.CellEvent = new myCellEvent(PdfContentByte.Corners.Top,2f); 
       table.AddCell(cell); 

如果你喜欢答案...给它一个投票:)

+0

这对单个细胞来说很好,但整个桌子呢?如何实现TableCell事件来实现这一点。 – 2010-04-29 17:53:12

+1

添加另一个表格,其中一个单元格包含我的目标表格。再次感谢您的回答MK。 – 2010-04-29 18:10:46

+1

我很高兴你找到了答案,另一种方法是将每个桌角单元的一角(TopLeft,TopRight,BottomLeft和BottomRight)四舍五入。 – 2010-05-02 06:56:24

2

我假设如果你想要一个圆角的桌子,你正在寻找一些华丽的(或至少漂亮的)格式在你的桌子上。如果是这种情况,我建议关闭iTextSharp表中的所有边界,并在图像上使用图形图像作为背景。然后,您可以使图形背景像您想要的那样复杂和富有想象力(阴影,渐变,圆角等),而且很麻烦。然后,将您的桌子与背景图片对齐。

如果你的数据格式会经常改变,你不会想要使用这种方法。

5

无法创建带圆角的iTextSharp PdfPTable,但是您可以使用PdfContentByte.RoundRectangle()方法绘制带圆角的表格边框。

以下是一个演示这一个片段:

Document doc = new Document(); 
using (FileStream fs = new FileStream("RoundRectangle Demo.pdf", FileMode.Create)) { 
PdfWriter writer = PdfWriter.GetInstance(doc, fs); 
    doc.Open(); 
    PdfContentByte cb = writer.DirectContent; 

    // Bottom left coordinates x & y, followed by width, height and radius of corners. 
    cb.RoundRectangle(100f, 500f, 200f, 200f, 2f); 
    cb.Stroke(); 

    doc.Close(); 
} 

这个片段是代码的精简版我就www.mikesdotnetting.com文章iTextSharp - Drawing shapes and Graphics中找到。如果你还没有看过他的其他文章,你可以仔细看看。

+1

这很好。你知道我怎么能找到桌子的位置,以便我可以准确地绘制圆角。 – 2010-04-03 21:04:21

+0

尽可能简单的回答 – hdoghmen 2015-06-17 19:34:11