簡體   English   中英

如何圓化 iTextSharp 表格邊框的角?

[英]How do I round the corners of an iTextSharp table border?

我想在 itextsharp 中制作一個圓角矩形。 這是我現在沒有四舍五入的 output:

在此處輸入圖像描述

這是我處理 output 的代碼:

pdftbl = new PdfPTable(3);
pdftbl.WidthPercentage = 100;
width = new float[3];
width[0] = 0.6F;
width[1] = 0.2F;
width[2] = 0.6F;
pdftbl.SetWidths(width);
pdfcel = new PdfPCell(new Phrase(Insuredaddress, docFont9));
pdfcel.BorderColor = Color.BLACK;
pdftbl.AddCell(pdfcel);
pdfcel = new PdfPCell(new Phrase("", docWhiteFont9));
pdfcel.Border = 0;
pdftbl.AddCell(pdfcel);
pdfcel = new PdfPCell(new Phrase(BkrAddrss, docFont9));
pdfcel.BorderColor = Color.BLACK;
pdftbl.AddCell(pdfcel);
objDocument.Add(pdftbl);

我可以更改/添加什么以達到預期的結果?

iText [夏普]沒有開箱即用的功能。 這是一種迂回的處理方式,但首先必須實現IPdfPCellEvent接口,然后將事件處理程序附加到添加到表中的每個單元格。 首先是一個簡單的實現

public class RoundRectangle : IPdfPCellEvent {
  public void CellLayout(
    PdfPCell cell, 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();
  }
}

請參閱PdfContentByte文檔 - 基本上上面的所有代碼都是繪制帶有圓角的單元格邊框。

然后像這樣分配上面創建的事件處理程序:

RoundRectangle rr = new RoundRectangle();    
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(document, STREAM);
  document.Open();
  PdfPTable table = new PdfPTable(1);
  PdfPCell cell = new PdfPCell() {
    CellEvent = rr, Border = PdfPCell.NO_BORDER,
    Padding = 4, Phrase = new Phrase("test")
  };
  table.AddCell(cell);  
  document.Add(table);
}

鑒於至少在 Itext 7 中只能通過在單元格上設置來在表格上繪制邊框,您可以按如下方式配置其半徑:

var cell = new Cell();
cell.SetBorderBottomLeftRadius(new BorderRadius(4f));

在表格上設置邊框半徑無效:

var table = new Table(2);
table.SetBorderBottomLeftRadius(new BorderRadius(4f)); // No border is drawn

暫無
暫無

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

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