繁体   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