6

我想在 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);

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

4

1 回答 1

10

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 文档- 基本上上面的所有代码都是根据您的需要绘制一个带有圆角的单元格边框。

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

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);
}
于 2012-04-22T21:00:41.050 回答