簡體   English   中英

表嵌套在itextsharp中的PDFPCELL中

[英]Table nested into PDFPCELL in itextsharp

我想給一個桌子提供圓形邊框,但經過研究后我發現它無法完成,但我們可以給一個單元格提供圓形邊框。

所以我做了這樣的事情

PdfPCell cell = new PdfPCell()
{
     CellEvent = rr, // rr is RoundRectangle object
     Border = PdfPCell.NO_BORDER,
     Padding = 4,
     Phrase = new Phrase("test")
};
table.AddCell(cell);
document.Add(table);

現在我可以給一個單元格邊框,所以我想要做的是我想將完整的嵌套表放入這個pdfpcell中,以便我可以間接地在該表上實現邊界...

你可以幫忙嗎? 如果你不理解我的方法..問題......我將在評論部分更清楚地解釋......

使用kuujinbo的代碼逐字表示RoundRectangle類:

public class RoundRectangle : IPdfPCellEvent {
    public void CellLayout(
      PdfPCell cell, iTextSharp.text.Rectangle rect, PdfContentByte[] canvas
    ) {
        PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
        cb.RoundRectangle(
          rect.Left,
          rect.Bottom,
          rect.Width,
          rect.Height,
          4 // change to adjust how "round" corner is displayed
        );
        cb.SetLineWidth(1f);
        cb.SetCMYKColorStrokeF(0f, 0f, 0f, 1f);
        cb.Stroke();
    }
}

然后,您只需要一個“外部”和“內部”表,只將CellEvent放在外部表上。

//Create a one column table
var outerTable = new PdfPTable(1);

//Create a single cell for that outer table
var outerCell = new PdfPCell();

//Turn the border off for that cell because we're manually going to draw one
outerCell.Border = iTextSharp.text.Rectangle.NO_BORDER;

//Bind our custom class for drawing the borders
outerCell.CellEvent = new RoundRectangle();


//Do whatever we want with the inner table
var innerTable = new PdfPTable(3);
for (var i = 0; i < 9; i++) {
    innerTable.AddCell("Hello");
}

//When done, add the inner table to the outer cell
outerCell.AddElement(innerTable);

//Add the outer cell to the outer table
outerTable.AddCell(outerCell);

//Add the outer table to the document
doc.Add(outerTable);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM