简体   繁体   English

iText7 和 C#,在表格单元格中放置一个填充形状

[英]iText7 and C#, put a filled shape in a table cell

I am trying to add a rectangle with rounded borders in a cell of a table, created with iText7 and C#.我正在尝试在使用 iText7 和 C# 创建的表格单元格中添加一个带有圆角边框的矩形。

I tried using我尝试使用

table.AddCell(new Cell().Add(rect)

where i created rect with我在哪里创建了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);

suggested from a friend of mine, but I think this is the wrong way to do this, and I'm not even quite sure of what this code does.我的一个朋友建议,但我认为这是错误的方法,我什至不太确定这段代码的作用。 Plus, the rectangle is transparent, has big margins, and the font in the cells is now green too (it was black before inserting the rectangle).另外,矩形是透明的,有很大的边距,并且单元格中的字体现在也是绿色的(在插入矩形之前是黑色的)。

Here is how it looks like (i purposedly put the square a little bit higher to show the transparency):这是它的样子(我故意将正方形放高一点以显示透明度):

在此处输入图片说明

What I want to do is to create a green shape rectangle, round the borders, then put it in the cell.我想要做的是创建一个绿色形状的矩形,环绕边框,然后将其放入单元格中。

It should look like this:它应该是这样的:

在此处输入图片说明

Is there a good way to do that?有没有好的方法可以做到这一点?

You can create a block-level layout object ( Div ) and set all the necessary visual properties to it.您可以创建一个块级布局对象 ( Div ) 并为其设置所有必要的视觉属性。 It is not necessary to perform custom drawing operations.没有必要执行自定义绘图操作。 Here is the code sample (in Java, but converting to C# boils down to capitalizing method names):这是代码示例(在 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));

Visually the result looks like this:视觉上的结果是这样的: 结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM