2014-05-14 32 views
1

我有以下情况:我必须使用iTextSharp在我的PDF的页脚中创建一个圆角表,但我发现要做到这一点很困难。为什么我无法在此iText iTextSharp页脚表中获得圆角表格?

首先我必须创建一个名为PdfHeaderFooter,它扩展了PdfPageEventHelper iTextSharp的接口类。

在这个类我已经实现了的OnEndPage()方法是创建在所有页面的末尾页脚,这是我的代码:

// Write on end of each page 
    public override void OnEndPage(PdfWriter writer, Document document) 
    { 
     base.OnEndPage(writer, document); 
     PdfPTable tabFot = new PdfPTable(new float[] { 1F }); 
     tabFot.TotalWidth = 300F; 

     tabFot.DefaultCell.Border = PdfPCell.NO_BORDER; 
     tabFot.DefaultCell.CellEvent = new RoundedBorder(); 

     PdfPCell cell; 
     cell = new PdfPCell(new Phrase("Footer")); 
     tabFot.AddCell(cell); 

     tabFot.WriteSelectedRows(0, -1, 150, document.Bottom, writer.DirectContent); 
    } 

正如你在代码中看到我创建表名为TabFoot即300px wisth并包含单个列。我还将此表单元的单元格事件处理程序设置为RoundBorder对象。

这是我RoundBorder类的代码:

class RoundedBorder : IPdfPCellEvent 
{ 
    public void CellLayout(PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvas) 
    { 
     PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS]; 
     cb.RoundRectangle(
      rect.Left + 1.5f, 
      rect.Bottom + 1.5f, 
      rect.Width - 3, 
      rect.Height - 3, 4 
     ); 
     cb.Stroke(); 
    } 
} 

的问题是,我的工作方案和PDF生成,但在页脚中的表没有圆角但经典的方角落,我得到这样的结果:

enter image description here

为什么?我错过了什么?我能做些什么来解决?

TNX

回答

2

为什么它不工作的原因很简单。您正在定义值和DefaultCell的单元格事件。如文档所述,默认单元格的属性在您自己添加单元格而不创建单元格时使用。例如:

table.AddCell("Test 1"); 

在你的情况,你不使用默认的单元格,你正在创建自己PdfPCell例如:

PdfPCell cell = new PdfPCell(new Phrase("Footer")); 

cell实例都有它自己的它的属性。它没有考虑你为DefaultCell定义了什么(否则就没有办法引入与默认值不同的属性)。因此,你需要:

cell.Border = PdfPCell.NO_BORDER; 
cell.CellEvent = new RoundedBorder(); 

现在特定小区cell只会有一个圆角的边框。

+0

当创建自己的PdfPCell实例时,是否有任何方法可以使表格而不是每个单元格的边框四舍五入? –

+0

是的,这是使用表事件而不是单元事件完成的。 –

相关问题