【问题标题】:How to arrange table border in iTextSharp如何在 iTextSharp 中排列表格边框
【发布时间】:2016-03-19 14:01:10
【问题描述】:

我正在尝试制作试算表。我想要如下所示的格式。我怎样才能制作这样的表格

我试过了,

table.DefaultCell.Border = Rectangle.RIGHT_BORDER | Rectangle.RIGHT_BORDER;

但代码只给出垂直线。我怎样才能制作像上面附加格式的表格?

【问题讨论】:

    标签: asp.net itextsharp


    【解决方案1】:

    看来你没看懂bitwise | operator的意思,因为这没有意义:

    table.DefaultCell.Border = Rectangle.RIGHT_BORDER | Rectangle.RIGHT_BORDER;
    

    上面的行相当于:

    table.DefaultCell.Border = Rectangle.RIGHT_BORDER;
    

    另外:如果你使用table.DefaultCell.Border,你不应该期望不同的单元格有不同的边框。您要求 iTextSharp 以这样一种方式绘制所有隐式创建的单元格,即它们只有右侧的边框。当 iTextSharp 服从该命令时,您为什么会感到惊讶?

    您需要显式地创建每个PdfPCell 并为每个PdfPCell 定义一个边框:

    PdfPCell left_cell = new PdfPCell();
    left_cell.Border = Rectangle.RIGHT_BORDER | Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER;
    // add stuff to left_cell
    table.AddCell(left_cell);
    PdfPCell middle_cell = new PdfPCell();
    middle_cell.Border = Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER;
    // add stuff to middle_cell
    table.AddCell(middle_cell);
    PdfPCell right_cell = new PdfPCell();
    right_cell.Border = Rectangle.LEFT_BORDER | Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER;
    // add stuff to right_cell
    table.AddCell(right_cell);
    

    在您的代码中,您可以在每次调用 AddCell() 之前为 DefaultCell 应用不同的边框,但这并没有多大意义。最好创建一个自定义的 getCell() 方法,以完全按照您喜欢的方式创建 PdfPCell 实例。

    【讨论】:

    • 是的,其实我不知道 |操作员。让我按你说的试试
    • 我添加了bitwise | operator 的链接,这是编程语言中非常常见的运算符。你不知道的事实告诉我你从来没有学过计算机科学。
    • 哪个版本将支持上述代码。对我来说,它显示错误,例如“iTextSharp.text.Rectangle 不包含'RIGHT'的定义”
    • 我不太了解 iTextSharp,但我所指的常量一直存在于 Rectangle 类中:developers.itextpdf.com/reference/com.itextpdf.text.Rectangle 也许 iTextSharp 中的名称不同,也许没有。跨度>
    • iTextSharp Rectangle.RIGHT_BORDERRectangle.RIGHT 的 iText 等价物。即两者都是int,值为8
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 2016-01-12
    • 1970-01-01
    • 2011-09-08
    • 2017-08-10
    相关资源
    最近更新 更多