【问题标题】:iText7 and C#, put a filled shape in a table celliText7 和 C#,在表格单元格中放置填充形状
【发布时间】:2020-03-20 14:19:46
【问题描述】:

我正在尝试在使用 iText7 和 C# 创建的表格单元格中添加一个带有圆角边框的矩形。

我尝试过使用

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

我的一个朋友建议的,但我认为这是错误的做法,我什至不太确定这段代码的作用。另外,矩形是透明的,有很大的边距,单元格中的字体现在也是绿色的(在插入矩形之前它是黑色的)。

这是它的样子(我故意把正方形放高一点以显示透明度):

我要做的是创建一个绿色形状的矩形,围绕边框,然后将其放入单元格中。

应该是这样的:

有什么好的方法吗?

【问题讨论】:

    标签: c# .net itext7


    【解决方案1】:

    您可以创建块级布局对象 (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));
    

    结果看起来像这样:

    【讨论】:

    • 非常感谢!当您回答时,我尝试使用 new Cell ,但我认为 new Div 更有意义!
    • 只是一个小问题:如果我不设置.setBorder(new SolidBorder(ColorConstants.GREEN, 1));,你知道为什么div会有那些“透明”的边框吗?我认为只使用与单元格相同的setHeight 会占用所有空间,但没有发生。
    • 我认为这是因为单元格默认有填充
    • 同样,有没有一种简单的方法可以在单元格中添加圆形/椭圆?
    猜你喜欢
    • 2016-11-08
    • 2013-12-11
    • 2010-09-25
    • 2020-11-23
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多