圆角桌子 [英] Tables with rounded corners

查看:132
本文介绍了圆角桌子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering what would be the best approach for creating tables in a pdf that have rounded corners using the iTextSharp 5.x+ library.

推荐答案

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

If you have iTextSharp source code, add the following to 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 interface:

Next step is to implment IPdfPCellEvent interface:

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
    }

然后你绕过什么你想要产生圆形表的角落:

Then you round what ever corners you want to produce the rounded table of choice:

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

如果您喜欢答案......请投票:)

If you like the answer ... give it a vote :)

这篇关于圆角桌子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆