【问题标题】:How do I round the corners of an iTextSharp table border?如何使 iTextSharp 表格边框的角变圆?
【发布时间】:2022-04-19 15:54:22
【问题描述】:

我想在 itextsharp 中制作一个圆角矩形。这是我现在没有四舍五入的输出:

这是我处理该输出的代码:

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);

我可以更改/添加什么来达到预期的效果?

【问题讨论】:

    标签: c# itext


    【解决方案1】:

    iText[Sharp] 没有开箱即用的此功能。这是一种迂回的做事方式,但首先您必须实现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 documentation - 基本上上面的所有代码都是根据您的需要绘制一个带有圆角的单元格边框。

    然后像这样分配上面创建的事件处理程序:

    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);
    }
    

    【讨论】:

    • 这不是这个问题的确切答案,因为 OP 要求的是表格舍入而不是单元格。
    【解决方案2】:

    鉴于至少在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-28
      • 2010-09-24
      • 2021-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多