iText7 和 C#,将填充形状放入 table 单元格
iText7 and C#, put a filled shape in a table cell
我正在尝试在使用 iText7 和 C# 创建的 table 的单元格中添加一个带有圆角边框的矩形。
我试过使用
table.AddCell(new Cell().Add(rect)
我用
创建rect
的地方
Rectangle boundingBox = new Rectangle(20, 470, 30, 30);
PdfFormXObject xObject = new PdfFormXObject(boundingBox);
xObject.MakeIndirect(pdfDoc); //Make sure the XObject gets added to the document
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
Color greenColor = new DeviceCmyk(100, 30, 100, 0);
canvas.SetFillColor(greenColor);
canvas.Rectangle(294, 780, 50, 35);
canvas.FillStroke();
Image rect = new Image(xObject);
我的一个朋友建议的,但我认为这是错误的做法,我什至不太确定这段代码的作用。另外,矩形是透明的,边距很大,单元格中的字体现在也是绿色的(插入矩形之前是黑色的)。
这是它的样子(我故意把正方形放高一点以显示透明度):
我想做的是创建一个绿色的长方形,把边框弄圆,然后把它放在单元格中。
它应该是这样的:
有什么好的方法吗?
您可以创建块级布局对象 (Div
) 并为其设置所有必要的视觉属性。无需执行自定义绘图操作。这是代码示例(在 Java 中,但转换为 C# 归结为大写方法名称):
Div rectangle = new Div()
.setHeight(30)
.setWidth(30)
.setBackgroundColor(ColorConstants.GREEN)
.setBorderRadius(new BorderRadius(5))
.setBorder(new SolidBorder(ColorConstants.GREEN, 1));
table.addCell(new Cell().add(rectangle));
视觉上结果如下所示:
我正在尝试在使用 iText7 和 C# 创建的 table 的单元格中添加一个带有圆角边框的矩形。
我试过使用
table.AddCell(new Cell().Add(rect)
我用
创建rect
的地方
Rectangle boundingBox = new Rectangle(20, 470, 30, 30);
PdfFormXObject xObject = new PdfFormXObject(boundingBox);
xObject.MakeIndirect(pdfDoc); //Make sure the XObject gets added to the document
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
Color greenColor = new DeviceCmyk(100, 30, 100, 0);
canvas.SetFillColor(greenColor);
canvas.Rectangle(294, 780, 50, 35);
canvas.FillStroke();
Image rect = new Image(xObject);
我的一个朋友建议的,但我认为这是错误的做法,我什至不太确定这段代码的作用。另外,矩形是透明的,边距很大,单元格中的字体现在也是绿色的(插入矩形之前是黑色的)。
这是它的样子(我故意把正方形放高一点以显示透明度):
我想做的是创建一个绿色的长方形,把边框弄圆,然后把它放在单元格中。
它应该是这样的:
有什么好的方法吗?
您可以创建块级布局对象 (Div
) 并为其设置所有必要的视觉属性。无需执行自定义绘图操作。这是代码示例(在 Java 中,但转换为 C# 归结为大写方法名称):
Div rectangle = new Div()
.setHeight(30)
.setWidth(30)
.setBackgroundColor(ColorConstants.GREEN)
.setBorderRadius(new BorderRadius(5))
.setBorder(new SolidBorder(ColorConstants.GREEN, 1));
table.addCell(new Cell().add(rectangle));
视觉上结果如下所示: