[英]How to hide and show Table with Optional Content
我正在使用iText庫創建可排序的表。 為此,我試圖隱藏/顯示在同一位置創建的表。 我讀到這可以通過使用Optional Content來實現。 誰能幫我顯示/隱藏帶有可選內容的表格嗎?
看一下SortingTable示例。 在此示例中,我們將三個重疊的表添加到同一位置的文檔中,但是由於每個表都屬於無線電組的不同層,因此在同一時間只能看到一個表。 您可以通過單擊列標題切換到另一個表。
看一看optionaltables.pdf 。 默認視圖如下所示:
但是,如果您單擊“列2”一詞,它看起來像這樣:
怎么做?
首先,我們創建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);
然后,我們使用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();
}
該表是這樣創建的:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.