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

问题描述:

我正在使用iText库创建可排序的表格。为此我试图隐藏/显示在相同位置创建的表格。我读过这个可以通过使用可选内容来实现。任何人都可以请帮助我显示/隐藏一个带有可选内容的表格吗?如何隐藏和显示带有可选内容的表格

看看SortingTable的例子。在本例中,我们将三个重叠的表添加到同一位置的文档中,但由于每个表属于一个无线电组的不同层,因此同一时间只能看到一个表。您可以通过单击列标题切换到另一个表格。

看看optionaltables.pdf。默认视图是这样的:

enter image description here

但是,如果你点击的话 “列2”,它看起来像这样:

enter image description here

这是如何完成的?

首先,我们创建了有组织犯罪集团:

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

然后我们在同一位置添加3个表使用ColumnText

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

表创建这样的:

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

我们希望标题中的文字可以点击:

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

在这个POC中,表格之间的区别与你想要的不同。您希望对内容进行不同的排序。对于这个简单的例子,我介绍了不同的背景颜色:

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