【问题标题】:iTextsharp custom borderiTextsharp 自定义边框
【发布时间】:2017-03-16 11:28:53
【问题描述】:

我想知道如何将自定义边框附加到 iTextSharp pdfdcell。

喜欢

问题是我找不到用 2 行添加边框的方法。

我只能在pdfdcell中创建一个正常的下边框

PdfPCell tmp = new PdfPCell(new Phrase(c));
tmp.HorizontalAlignment = Element.ALIGN_RIGHT;
tmp.Border = c.borderPosition;
tmp.BorderWidth = c.borderWidth;
pt.AddCell(tmp);

所以结果是这样的

但我需要在边框下再添加一条线。

【问题讨论】:

标签: c# itext


【解决方案1】:

由于我使用的是 C# 和 iTextSharp,并且评论仅显示 Java 上的解决方案。

我已经实现了类似的东西来解决这个问题。

基本事实是 iTextSharp 不支持自定义边框,但它允许您在 PDF 上绘制东西。因此,目标是在单元格底部绘制一条双线。

  1. 隐藏现有边框
  2. 找出单元格的确切位置
  3. 画线

诀窍是在单元格上实现 CellEvent,在单元格事件中它为我们提供了单元格的确切位置,因此我们可以轻松绘制。

下面是在我的 C# 项目中工作的代码

public function void DrawACell_With_DOUBLELINE_BOTTOM_BORDER(Document doc, PdfWriter writer){
    PdfPTable pt = new PdfPTable(new float[]{1});   
    Chunk c = new Chunk("A Cell with doubleline bottom border");
    int padding = 3;
    PdfPCell_DoubleLine cell = new PdfPCell_DoubleLine(PdfPTable pt,new Phrase(c), writer, padding);
    pt.AddCell(cell);
    doc.Add(pt);
}

public class PdfPCell_DoubleLine : PdfPCell
{
    public PdfPCell_DoubleLine(Phrase phrase, PdfWriter writer, int padding) : base(phrase)
    {
        this.HorizontalAlignment = Element.ALIGN_RIGHT;
        //1. hide existing border
        this.Border = Rectangle.NO_BORDER;
        //2. find out the exact position of the cell
        this.CellEvent = new DLineCell(writer, padding);
    }
    public class DLineCell : IPdfPCellEvent
    {
        public PdfWriter writer { get; set; }
        public int padding { get; set; }
        public DLineCell(PdfWriter writer, int padding)
        {
            this.writer = writer;
            this.padding = padding;
        }

        public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvases)
        {
            //draw line 1
            PdfContentByte cb = writer.DirectContent;
            cb.MoveTo(rect.GetLeft(0), rect.GetBottom(0) - padding);
            cb.LineTo(rect.GetRight(0), rect.GetBottom(0) - padding);
            //draw line 2
            cb.MoveTo(rect.GetLeft(0), rect.GetBottom(0) - padding - 2);
            cb.LineTo(rect.GetRight(0), rect.GetBottom(0) - padding - 2);
            cb.Stroke();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多