2014-07-02 122 views
3

我通过在iTextSharp的与PDFPTable数据库建立一个表,并要求是没有行/细胞在表中有一个顶部或底部的底部边框,但左边框并且每个单元格的右侧都有一个黑色边框(换句话说,每列都有一个左边框和一个右边框),桌子的底部也需要用黑色边框关闭,这也是我的问题所在。iTextSharp的PDFPTable如何使周围整个表

我在做的是将边框设置为0,然后手动分配边框,以便仅在每个单元格上获得左右边框,如下面所示,作为生成的“数量”列的示例:

cell = new PdfPCell(new Phrase(Qty.value, subheaderFont)); 
    cell.HorizontalAlignment = Element.ALIGN_CENTER; 
    cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
    cell.BackgroundColor = new iTextSharp.text.BaseColor(220, 220, 220); 
    cell.Border = 0; 
    cell.BorderColorLeft = BaseColor.BLACK; 
    cell.BorderWidthLeft = .5f; 
    cell.BorderColorRight = BaseColor.BLACK; 
    cell.BorderWidthRight = .5f; 
    table.AddCell(cell); 

这个问题很明显,我没有办法检测最后一行添加border-bottom,但我想必须有一种方法来控制“表”本身的边界,或者我正在这是错误的方法?

回答

1

当你发现,PdfPTable没有国界,可能是因为PDF的没有摆在首位表。将边界直接放在PdfPCell上可能更有意义(尽管PDF也不支持这些)。无论如何,表格只是一个单元的集合,所以让他们处理它。

无论如何,解决方法是在您的PdfPTable类的实例上设置TableEvent。要做到这一点,您需要定制实现IPdfPTableEvent界面。下面的代码一般应为你做这个(见底部的笔记“一般”)

class TopBottomTableBorderMaker : IPdfPTableEvent { 
    private BaseColor _borderColor; 
    private float _borderWidth; 

    /// <summary> 
    /// Add a top and bottom border to the table. 
    /// </summary> 
    /// <param name="borderColor">The color of the border.</param> 
    /// <param name="borderWidth">The width of the border</param> 
    public TopBottomTableBorderMaker(BaseColor borderColor, float borderWidth) { 
     this._borderColor = borderColor; 
     this._borderWidth = borderWidth; 
    } 
    public void TableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { 
     //widths (should be thought of as x's) is an array of arrays, first index is for each row, second index is for each column 
     //The below uses first and last to calculate where each X should start and end 
     var firstRowWidths = widths[0]; 
     var lastRowWidths = widths[widths.Length - 1]; 

     var firstRowXStart = firstRowWidths[0]; 
     var firstRowXEnd = firstRowWidths[firstRowWidths.Length - 1] - firstRowXStart; 

     var lastRowXStart = lastRowWidths[0]; 
     var lastRowXEnd = lastRowWidths[lastRowWidths.Length - 1] - lastRowXStart; 

     //heights (should be thought of as y's) is the y for each row's top plus one extra for the last row's bottom 
     //The below uses first and last to calculate where each Y should start and end 
     var firstRowYStart = heights[0]; 
     var firstRowYEnd = heights[1] - firstRowYStart; 

     var lastRowYStart = heights[heights.Length - 1]; 
     var lastRowYEnd = heights[heights.Length - 2] - lastRowYStart; 

     //Where we're going to draw our lines 
     PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; 

     //I always try to save the previous state before changinge anything 
     canvas.SaveState(); 

     //Set our line properties 
     canvas.SetLineWidth(this._borderWidth); 
     canvas.SetColorStroke(this._borderColor); 

     //Draw some rectangles 
     canvas.Rectangle(
         firstRowXStart, 
         firstRowYStart, 
         firstRowXEnd, 
         firstRowYEnd 
         ); 
     //They aren't actually drawn until you stroke them! 
     canvas.Stroke(); 

     canvas.Rectangle(
         lastRowXStart, 
         lastRowYStart, 
         lastRowXEnd, 
         lastRowYEnd 
         ); 
     canvas.Stroke(); 

     //Restore any previous settings 
     canvas.RestoreState(); 

    } 
} 

使用它是很容易的,只是一个实例绑定到该属性:

//Create your name as you normally do 
var table = new PdfPTable(3); 

//Bind and instance with properties set 
table.TableEvent = new TopBottomTableBorderMaker(BaseColor.BLACK, 0.5f); 

//The rest is the same 
for (var i = 0; i < 6; i++) { 
    var cell = new PdfPCell(new Phrase(i.ToString())); 
    cell.HorizontalAlignment = Element.ALIGN_CENTER; 
    cell.VerticalAlignment = Element.ALIGN_MIDDLE; 
    cell.BackgroundColor = new iTextSharp.text.BaseColor(220, 220, 220); 
    cell.Border = 0; 
    cell.BorderColorLeft = BaseColor.BLACK; 
    cell.BorderWidthLeft = .5f; 
    cell.BorderColorRight = BaseColor.BLACK; 
    cell.BorderWidthRight = .5f; 
    table.AddCell(cell); 
} 

以上我说“一般”它应该工作。但是,如果您有表格标题和/或页脚,那么您也需要考虑这些问题。这应该不是太难,但你需要调整y值占table.HeaderRowstable.FooterRows

1

我经历过同样的问题,发现了以下的解决方案。 从“的iText在行动第二版” PdfPCell扩展矩形,在继承的方法来改变边界绘制和背景都画的方式登塔..

的方法“DisableBorderSide(INT矩形)”是如何去删除与其他尺寸相关的边界。

PdfPCell cell = new PdfPCell(new Phrase("Some Text", FontFactory.GetFont("Arial", BaseFont.WINANSI, BaseFont.EMBEDDED, 13, Font.NORMAL, BaseColor.BLACK))); 
cell.BackgroundColor = new BaseColor(255, 255, 255); 
cell.HorizontalAlignment = Element.ALIGN_CENTER; 
cell.BorderColor = BaseColor.BLACK; 
cell.Border = Rectangle.BOX; 
cell.BorderWidth = 1; 
cell.DisableBorderSide(Rectangle.TOP_BORDER); 
cell.DisableBorderSide(Rectangle.BOTTOM_BORDER); 
cell.Padding = 3; 
table.AddCell(cell); 
2

我解决了这个使用嵌套表

'CREATE TWO PDFPTABLES 
Dim tblNested1 As New PdfPTable(1) 
Dim tblNested2 As New PdfPTable(3) 

'CREATE CELLS WITH NO BORDER AND ADD THEM TO TABLE2 

Dim cellNested1 = New PdfPCell(New Phrase("CELL1")) 
cellNested1.Border = 0 
tblNested2.AddCell(cellNested1) 
Dim cellNested2 = New PdfPCell(New Phrase("CELL2")) 
cellNested2.Border = 0 
tblNested2.AddCell(cellNested2) 
Dim cellNested3 = New PdfPCell(New Phrase("CELL3")) 
cellNested3.Border = 0 
tblNested2.AddCell(cellNested3) 

'APPEND TABLE2 TO A CELL WITH DEFAULT BORDER 

    Dim cell1 As New PdfPCell 
    cell1.AddElement(tblNested2) 

    tblNested1.AddCell(cell1) 

    document.Add(tblNested1) 
+0

玛丽亚的回答是最适合这种情况。对Chris Hass而言,他认为他的解决方案对于需要维护和更新的大型项目来说更好。他的解决方案是干的,而且这种方法更好。但如果快速和肮脏是好的,他们玛丽亚的方法是完美的。 – BillRuhl

相关问题