11

iTextSharpのテーブル(PdfPTable)内にセル間隔を設定することは可能ですか? それが可能であることはどこにも見えません。代わりに iTextSharp.text.Table を使用するという 1 つの提案を見ましたが、私のバージョンの iTextSharp (5.2.1) では利用できないようです。

4

2 に答える 2

15

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);
于 2012-04-19T16:45:09.313 に答える
0

Table クラスは、5.x 以降の iText から削除され、PdfPTable が優先されます。

間隔に関しては、探しているのは setPadding メソッドです。

詳細については、iText の API をご覧ください。

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

(これは Java バージョン用ですが、C# ポートはメソッドの名前を維持します)

于 2012-04-19T16:19:17.090 に答える