c# - 如何圆化 iTextSharp 表格边框的角?

标签 c# itext

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

enter image description here

这是我处理输出的代码:

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[Sharp] 没有开箱即用的功能。这是一种迂回的做事方式,但首先你必须实现 IPdfPCellEvent接口(interface),然后将事件处理程序附加到您添加到表中的每个 单元格。首先是一个简单的实现:

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

关于c# - 如何圆化 iTextSharp 表格边框的角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10271592/

相关文章:

c# - 通过 ItextSharp 合并 PDF

java - Itext 中的 Bouncy CasSTLe 错误

c# - 使用 xs :extension and xs:restriction in a XSD

c# - 为什么这些随机事件遵循特定的模式?

javascript - 所有面向对象的语言都会在内存中创建大量重复信息吗?

java - 与 TJ 运算符(operator)合作

java - 获取已通过 iText 读入的 PDF 的方向

java - iText7 使用 Java 进行缩进列表格式化

c# - AutoResetEvent 过程?

c# - 用子类创建泛型