我想知道使用iTextSharp 5.x +库在pdf中创建具有圆角的表的最佳方法是什么。
答案 0 :(得分: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);
}
下一步是实施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);
如果您喜欢这个答案......请投票:)
答案 1 :(得分:6)
无法创建带圆角的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中找到的代码的精简版本。如果你还没有,你会想仔细看看他的其他文章。
答案 2 :(得分:2)
我认为如果你想要一个带圆角的桌子,你会在你的桌子上寻找一些华丽(或至少漂亮)的格式。如果是这种情况,我建议关闭iTextSharp表中的所有边框,并使用图形图像作为页面上的背景。然后,您可以根据需要将图形背景设置为复杂和花哨(阴影,渐变,圆角等),而不会遇到任何麻烦。然后,将表格与背景图像对齐。
如果您的数据格式经常发生变化,您将不希望使用此方法。
答案 3 :(得分:0)
私有函数pdfDIF()作为Byte() 昏暗的celevent作为新的PdfPCellEvent
Dim tableImportes As New PdfPTable(1)
tableImportes.SetTotalWidth({10 * dpixcm})
Dim cellImportes = New PdfPCell(New Phrase("IMPORTE", COURIER9Bold))
cellImportes.BorderWidth = 0
cellImportes.HorizontalAlignment = Element.ALIGN_CENTER
cellImportes.CellEvent = celevent
tableImportes.AddCell(cellImportes)
结束功能
公共类PdfPCellEvent 实现IPdfPCellEvent
Public Sub CellLayout(cell As iTextSharp.text.pdf.PdfPCell, position As iTextSharp.text.Rectangle, canvases() As iTextSharp.text.pdf.PdfContentByte) Implements iTextSharp.text.pdf.IPdfPCellEve`enter code here`nt.CellLayout
Dim cb As PdfContentByte = canvases(PdfPTable.LINECANVAS)
cb.RoundRectangle(position.Left,position.Bottom,position.Width,position.Height,10)
cb.SetLineWidth(1)
cb.SetCMYKColorStrokeF(0.0F, 0.0F, 0.0F, 1.0F)
cb.Stroke()
End Sub
结束班级