2012-04-19 359 views
6

是否可以在iTextSharp中的表格(PdfPTable)中有单元格间距?我无法看到任何可能的地方。我确实看到了一个使用iTextSharp.text.Table的建议,但在我的iTextSharp(5.2.1)版本中似乎没有。iTextSharp表格单元格间距可能?

回答

1

Table类已从5.x开始从iText中删除,以支持PdfPTable。

至于间距,你要找的是setPadding方法。

看一看的iText的API的更多信息:

http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfPCell.html

(这对Java版本,但C#端口维护的方法的名称)

+4

感谢您的但是这是用于添加细胞填充(在细胞内)。我需要的是单元格间距(单元格之间)。 – 2012-04-20 11:49:32

13

如果你正在寻找对于真正的单元格间距,如HTML,则不是,PdfPTable本身不支持。然而,PdfPCell支持一个属性,该属性需要IPdfPCellEvent的自定义实现,只要单元布局发生,该属性就会被调用。下面是一个简单的实现,你可能想调整它以满足你的需求。

public class CellSpacingEvent : IPdfPCellEvent { 
    private int cellSpacing; 
    public CellSpacingEvent(int cellSpacing) { 
     this.cellSpacing = cellSpacing; 
    } 
    void IPdfPCellEvent.CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { 
     //Grab the line canvas for drawing lines on 
     PdfContentByte cb = canvases[PdfPTable.LINECANVAS]; 
     //Create a new rectangle using our previously supplied spacing 
     cb.Rectangle(
      position.Left + this.cellSpacing, 
      position.Bottom + this.cellSpacing, 
      (position.Right - this.cellSpacing) - (position.Left + this.cellSpacing), 
      (position.Top - this.cellSpacing) - (position.Bottom + this.cellSpacing) 
      ); 
     //Set a color 
     cb.SetColorStroke(BaseColor.RED); 
     //Draw the rectangle 
     cb.Stroke(); 
    } 
} 

要使用它:

//Create a two column table 
PdfPTable table = new PdfPTable(2); 
//Don't let the system draw the border, we'll do that 
table.DefaultCell.Border = 0; 
//Bind our custom event to the default cell 
table.DefaultCell.CellEvent = new CellSpacingEvent(2); 
//We're not changing actual layout so we're going to cheat and padd the cells a little 
table.DefaultCell.Padding = 4; 
//Add some cells 
table.AddCell("Test"); 
table.AddCell("Test"); 
table.AddCell("Test"); 
table.AddCell("Test"); 

doc.Add(table); 
-4

尝试

 PdfPTable table = new PdfPTable(2); 
     table.getDefaultCell().setBorder(0); 
     table.getDefaultCell().setPadding(8); 
     table.addCell("Employee ID"); 
     table.addCell(""); 
     table.addCell("Employee Name"); 
     table.addCell(""); 
     table.addCell("Department"); 
     table.addCell(""); 
     table.addCell("Place"); 
     table.addCell(""); 
     table.addCell("Contact Number"); 
     table.addCell(""); 
     document.add(table);