我想知道使用iTextSharp 5.x +库创建具有圆角的pdf表格的最佳方法。带圆角的表格
带圆角的表格
回答
如果你有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);
如果你喜欢答案...给它一个投票:)
我假设如果你想要一个圆角的桌子,你正在寻找一些华丽的(或至少漂亮的)格式在你的桌子上。如果是这种情况,我建议关闭iTextSharp表中的所有边界,并在图像上使用图形图像作为背景。然后,您可以使图形背景像您想要的那样复杂和富有想象力(阴影,渐变,圆角等),而且很麻烦。然后,将您的桌子与背景图片对齐。
如果你的数据格式会经常改变,你不会想要使用这种方法。
无法创建带圆角的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中找到。如果你还没有看过他的其他文章,你可以仔细看看。
这很好。你知道我怎么能找到桌子的位置,以便我可以准确地绘制圆角。 – 2010-04-03 21:04:21
尽可能简单的回答 – hdoghmen 2015-06-17 19:34:11
- 1. 带风格的CSS3圆角文本框
- 2. 带圆角的UIView?
- 3. 带圆角的NSTextField?
- 4. 带圆角的UIToolbar
- 5. 带圆角的Bezierpath
- 6. 带圆角的ListView
- 7. 带有圆角和渐变背景的HTML表格
- 8. 带有圆角的UITextView
- 9. 带圆角矩形的SKScene
- 10. Xamarin Forms:带圆角的StackLayout
- 11. 带圆角的进度条?
- 12. 带圆角的翻转UIView
- 13. 带圆角的WPF treeview
- 14. 带回形针的圆角
- 15. 带菜单的JQuery圆角
- 16. 创建带圆角的ImageView
- 17. 带圆角的EaselJS三角形?
- 18. 创建带圆角的三角形
- 19. 带圆角的绘制三角形
- 20. 在带圆角的div内滚动iframe叶圆角的痕迹
- 21. UIImage带透明圆角
- 22. HTML5帆布带圆角
- 23. 表中的圆角
- 24. 表中的圆角
- 25. Silverlight中网格的圆角
- 26. 分组表视图 - 圆角单元格?
- 27. 使用圆角创建表格
- 28. 带边框半径的多线圆角
- 29. 处理:带圆角的图像
- 30. 带有透明圆角矩形的UIView?
这对单个细胞来说很好,但整个桌子呢?如何实现TableCell事件来实现这一点。 – 2010-04-29 17:53:12
添加另一个表格,其中一个单元格包含我的目标表格。再次感谢您的回答MK。 – 2010-04-29 18:10:46
我很高兴你找到了答案,另一种方法是将每个桌角单元的一角(TopLeft,TopRight,BottomLeft和BottomRight)四舍五入。 – 2010-05-02 06:56:24