读到itext in action第6章6.1.3,有个函数getDefaultCell(),查看该函数的API
Gets the default PdfPCell
that will be used as reference for all the addCell
methods except addCell(PdfPCell)
.
那么就是说你使用new PdfPCell就有border
那再查看PdfPCell的构造函数。以PdfPCell()和PdfPCell(Phrase)为例,发现的确有默认的border。
参照第6章的代码PdfPTableWithoutBorders做小小的改动
/* chapter06/PdfPTableWithoutBorders.java */
package org.study.itext.table;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
/**
* @blog http://reymont.iteye.com/
* @author reymont.li
* @version create time:2011-7-18 下午04:13:47
*/
public class PdfPTableWithoutBorders {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(
document,
new FileOutputStream("resource/pdfptable_without_borders.pdf"));
document.open();
PdfPTable table = new PdfPTable(3);
table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
//cell.setColspan(3);
table.addCell(cell);
table.addCell(new Paragraph("header with colspan 3"));
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
document.add(table);
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
}
可得到。
请注意addCell(new PdfPCell())和addCell(new Paragraph())的区别
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
table.addCell(cell);
table.addCell(new Paragraph("header with colspan 3"));
参考资料:
- itext in action 2006版
- itext-2.0.8