简体   繁体   English

如何隐藏和显示带有可选内容的表格

[英]How to hide and show Table with Optional Content

I am using iText Library for creating sort-able table. 我正在使用iText库创建可排序的表。 For this I am trying to hide/show the tables created at the same position. 为此,我试图隐藏/显示在同一位置创建的表。 I read this can be achieved by using Optional Content . 我读到这可以通过使用Optional Content来实现。 Can anyone please help me out to show/hide a Table with Optional Content? 谁能帮我显示/隐藏带有可选内容的表格吗?

Take a look at the SortingTable example. 看一下SortingTable示例。 In this example, we add three overlapping tables to a document at the same location, but as each table belongs to a different layer of a radio group, only one table is visible at the same time. 在此示例中,我们将三个重叠的表添加到同一位置的文档中,但是由于每个表都属于无线电组的不同层,因此在同一时间只能看到一个表。 You can switch to another table by clicking on the column header. 您可以通过单击列标题切换到另一个表。

Take a look at optionaltables.pdf . 看一看optionaltables.pdf The default view looks like this: 默认视图如下所示:

在此处输入图片说明

But if you click on the words "Column 2", it looks like this: 但是,如果您单击“列2”一词,它看起来像这样:

在此处输入图片说明

How is this done? 怎么做?

First we create the OCGs: 首先,我们创建OCG:

ArrayList<PdfLayer> options = new ArrayList<PdfLayer>();
PdfLayer radiogroup = PdfLayer.createTitle("Table", writer);
PdfLayer radio1 = new PdfLayer("column1", writer);
radio1.setOn(true);
options.add(radio1);
radiogroup.addChild(radio1);
PdfLayer radio2 = new PdfLayer("column2", writer);
radio2.setOn(false);
options.add(radio2);
radiogroup.addChild(radio2);
PdfLayer radio3 = new PdfLayer("column3", writer);
radio3.setOn(false);
options.add(radio3);
radiogroup.addChild(radio3);
writer.addOCGRadioGroup(options);

Then we add 3 tables at the same location using ColumnText : 然后,我们使用ColumnText在同一位置添加3个表:

PdfContentByte canvas = writer.getDirectContent();
ColumnText ct = new ColumnText(canvas);
for (int i = 1; i < 4; i++) {
    canvas.beginLayer(options.get(i - 1));
    ct.setSimpleColumn(new Rectangle(36, 36, 559, 806));
    ct.addElement(createTable(i, options));
    ct.go();
    canvas.endLayer();
}

The table is created like this: 该表是这样创建的:

public PdfPTable createTable(int c, List<PdfLayer> options) {
    PdfPTable table = new PdfPTable(3);
    for (int j = 1; j < 4; j++) {
        table.addCell(createCell(j, options));
    }
    for (int i = 1; i < 4; i++) {
        for (int j = 1; j < 4; j++) {
            table.addCell(createCell(i, j, c));
        }
    }
    return table;
}

We want the words in the header to be clickable: 我们希望标题中的单词是可点击的:

public PdfPCell createCell(int c, List<PdfLayer> options) {
    Chunk chunk = new Chunk("Column " + c);
    ArrayList<Object> list = new ArrayList<Object>();
    list.add("ON");
    list.add(options.get(c - 1));
    PdfAction action = PdfAction.setOCGstate(list, true);
    chunk.setAction(action);
    return new PdfPCell(new Phrase(chunk));
}

In this POC, the difference between the tables is different from what you want. 在此POC中,表之间的差异与您想要的不同。 You want the content to be sorted differently. 您希望对内容进行不同的排序。 For this simple example, I introduced a different background color: 对于这个简单的示例,我介绍了一种不同的背景色:

public PdfPCell createCell(int i, int j, int c) {
    PdfPCell cell = new PdfPCell();
    cell.addElement(new Paragraph(String.format("row %s; column %s", i, j)));
    if (j == c) {
        cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
    }
    return cell;
}

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

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