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

如题所述

1. 通过设置表格的边框,可以实现!
public static PdfPCell createCellTL(String value,
int align, Font font,int colspan) { //上左有边框的表格:1.表格内容;2.字体;3.对齐方式;4.横跨列数
PdfPCell cell = new PdfPCell();

//cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align); //水平对齐
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value,textfont)); //设置表格的短语
cell.setPadding(5.0f); //设置内间距

cell.disableBorderSide(2); //隐藏下右边框
cell.disableBorderSide(8);

return cell;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-03-17
代码说话,

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();
}
}
}
第2个回答  2018-03-07
在创建单元格的时候设置 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);//全没了
相似回答