删除iText表格中的嵌套表格边框。

huangapple go评论70阅读模式
英文:

Remove nested table border from itext table

问题

String dest = FileSystems.getDefault().getPath("").toAbsolutePath().toString() + "/test.pdf";
log.info(dest);
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(writer);
pdfDoc.addNewPage();

Document document = new Document(pdfDoc);

float[] pointColumnWidths = {150F, 150F};
Table outerTable = new Table(pointColumnWidths);

for (int i = 0; i < 5; i++) {
    outerTable.addCell("col1-" + i);
    outerTable.addCell("col2-" + i);
}

float[] innerTablePointColumnWidths = {150F, 150F, 150F, 150F};
Table nestedTable = new Table(innerTablePointColumnWidths);

nestedTable.addCell("col11");
nestedTable.addCell("col11_val");

nestedTable.addCell("col12");
nestedTable.addCell("col12_val");

nestedTable.addCell("col22");
nestedTable.addCell("col22_val");

nestedTable.addCell("col23");
nestedTable.addCell("col23_val");

nestedTable.addCell("col31");
nestedTable.addCell("col31_val");

nestedTable.addCell("col32");
nestedTable.addCell("col32_val");

outerTable.addCell("nestedParent");

outerTable.setBorder(Border.NO_BORDER);
/* Cell cell = new Cell();
cell.add(nestedTable);*/
// cell.setBorder(Border.NO_BORDER);
// outerTable.addCell(cell);

outerTable.addCell(nestedTable);

document.add(outerTable);

document.close();

itext相关库版本 7.1.12

英文:

I was trying to create nested table in itext by java. I can create it but unexpectedly, the nested table holds its own border along with the original table's cell border.
Is there any way to remove the outer border only of the nested table, in nestedParent's right column?
删除iText表格中的嵌套表格边框。
My code:

String dest= FileSystems.getDefault().getPath(&quot;&quot;).toAbsolutePath().toString()+&quot;/test.pdf&quot;;
log.info(dest);
PdfWriter writer= new PdfWriter(dest);
PdfDocument pdfDoc = new PdfDocument(writer);
pdfDoc.addNewPage();
Document document = new Document(pdfDoc);
float [] pointColumnWidths = {150F, 150F};
Table outerTable = new Table(pointColumnWidths);
for (int i = 0; i &lt; 5; i++) {
outerTable.addCell(&quot;col1-&quot; + i);
outerTable.addCell(&quot;col2-&quot; + i);
}
float [] innerTablePointColumnWidths = {150F, 150F, 150F, 150F};
Table nestedTable = new Table(innerTablePointColumnWidths);
nestedTable.addCell(&quot;col11&quot;);
nestedTable.addCell(&quot;col11_val&quot;);
nestedTable.addCell(&quot;col12&quot;);
nestedTable.addCell(&quot;col12_val&quot;);
nestedTable.addCell(&quot;col22&quot;);
nestedTable.addCell(&quot;col22_val&quot;);
nestedTable.addCell(&quot;col23&quot;);
nestedTable.addCell(&quot;col23_val&quot;);
nestedTable.addCell(&quot;col31&quot;);
nestedTable.addCell(&quot;col31_val&quot;);
nestedTable.addCell(&quot;col32&quot;);
nestedTable.addCell(&quot;col32_val&quot;);
outerTable.addCell(&quot;nestedParent&quot;);
outerTable.setBorder(Border.NO_BORDER);
/* Cell cell = new Cell();
cell.add(nestedTable);*/
// cell.setBorder(Border.NO_BORDER);
//outerTable.addCell(cell);
outerTable.addCell(nestedTable);
document.add(outerTable);
document.close();

itext related lib version 7.1.12

答案1

得分: 0

你需要从内部表格单元中移除每个边框(即"col11"单元格应将其上边和左边的边框设置为null或Border#NO_BORDER,并将内部表格单元格也设置为null)。

此外,您还应将内部表格的填充设置为0以消除间隙。

Cell cell = new Cell();
cell.setPadding(0);
cell.add(nestedTable);
outerTable.addCell(cell);
英文:

You need to remove each border from the inner table cells (i.e "col11" cell should set his upper and left border to null or Border#NO_BORDER AND set the inner table cell to null as well).

Furthermore, You should set the padding of the inner table to 0 in order to remove the gap.

Cell cell = new Cell();
cell.setPadding(0);
cell.add(nestedTable);
outerTable.addCell(cell);

答案2

得分: 0

我无法使用内部表格解决这个问题。所以,为了解决这个问题,我不得不定义一个包含5列的表格,然后在必要时为该特定条目定义所需的单元格定义,使用 rowcol span,就像下面这样:

Cell cell = new Cell(1, 1);
Cell cell = new Cell(1, 4);

对于行内的五列:

Cell cell = new Cell(4, 1);

这样就解决了!

英文:

I couldn't solve the problem using inner table. So, to solve that, I have to define a table of 5 columns and then when necessary I have defined the necessary cell definition for that particular entry, using row and col span, like

Cell cell= new Cell(1,1);
Cell cell=new Cell(1,4);

For five columns inside a row:

Cell cell= new Cell(4,1)

this way it is solved!

huangapple
  • 本文由 发表于 2020年9月1日 16:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63684210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定