itextsharp - 带圆角的 table

标签 itextsharp

我想知道使用 iTextSharp 5.x+ 库在具有圆角的 pdf 中创建表格的最佳方法是什么。

最佳答案

如果您有 iTextSharp 源代码,请将以下内容添加到 PdfContentByte类(class):

/// <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
    }

然后,您将想要生成圆 table 的任何角落都圆滑:
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 - 带圆角的 table ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2557217/

相关文章:

.net - ItextSharp 5.4.5 是否支持 256 位加密?

c# - 如何在 C# 中逐行读取 PDF 文件?

c# - 如何在 iTextSharp 中找到当前 (X, Y) 位置?

c# - 代码未在我的 PDF 中绘制水平线

vb.net - iTextSharp 从 Table 到 pdfPTable 的转换

pdf - 在 iTextSharp 中隐藏部分和章节的数量

pdf - 如何使用 iTextsharp 突出显示 pdf 文件中的文本或单词?

c# - iTextSharp 居中对齐 Document 对象中的对象

C#:iTextSharp,如何编辑 pdf 文档的标题属性?

itextsharp 5.4.4 CopyAcroForm 不再存在