itext设置表格无边框怎么设置

2025-01-20 15:43:57
推荐回答(2个)
回答1:

在创建单元格的时候设置 cell.disableBorderSide属性可以控制表格的单元格边框
效果如下所示

//cell.disableBorderSide(8);//右边没了
// cell.disableBorderSide(1);//上没了
// cell.disableBorderSide(2);//下没了
//cell.disableBorderSide(3);//上下都没了
//cell.disableBorderSide(4);//左
//cell.disableBorderSide(5);//左上都没了
//cell.disableBorderSide(6);//左下都没了
//cell.disableBorderSide(7);//只剩右边
// cell.disableBorderSide(9);//右上没了
//cell.disableBorderSide(10);//右下没了
//cell.disableBorderSide(11);//只剩左
//cell.disableBorderSide(12);//左右没了
//cell.disableBorderSide(13);//只剩下
//cell.disableBorderSide(14);//只剩上

cell.disableBorderSide(15);//全没了

回答2:

代码说话,

package com.witwall.example.itextpdf;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class TableCellBorderColor {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("TableCellBorder.pdf"));
document.open();

PdfPTable table = new PdfPTable(3);
PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));
cell1.setUseBorderPadding(true);
//
// Setting cell's border width and color
//
cell1.setBorderWidth(5f);
cell1.setBorderColor(BaseColor.BLUE);
table.addCell(cell1);

PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));
cell2.setUseBorderPadding(true);
//
// Setting cell's background color
//
cell2.setBackgroundColor(BaseColor.GRAY);
//
// Setting cell's individual border color
//
cell2.setBorderWidthTop(1f);
cell2.setBorderColorTop(BaseColor.RED);
cell2.setBorderColorRight(BaseColor.GREEN);
cell2.setBorderColorBottom(BaseColor.BLUE);
cell2.setBorderColorLeft(BaseColor.BLACK);
table.addCell(cell2);

PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3"));
cell3.setUseBorderPadding(true);
//
// Setting cell's individual border width
//
cell3.setBorderWidthTop(2f);
cell3.setBorderWidthRight(1f);
cell3.setBorderWidthBottom(2f);
cell3.setBorderWidthLeft(1f);
table.addCell(cell3);
table.completeRow();

document.add(table);
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
} finally {
document.close();
}
}
}