itext设置默认NO_BORDER表格

去除iText表格边框
本文介绍如何在iText中创建无边框的PdfPTable。通过设置getDefaultCell().setBorder(PdfPCell.NO_BORDER),可以移除单元格边框。示例代码展示了使用PdfPCell与直接使用Paragraph作为单元格的区别。

读到itext in action第6章6.1.3,有个函数getDefaultCell(),查看该函数的API

 

 

 

PdfPCell com.lowagie.text.pdf. PdfPTable .getDefaultCell()

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

@Override public boolean printLabel(String id) { BaseFont baseFont = null; try { baseFont = BaseFont.createFont("SimSun", "Identity-H", BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } Font chineseFont = new Font(baseFont); // 获取物料信息 WmsMaterielManage info = this.getById(id); String materialId = info.getMaterialId(); String name = info.getName(); String model = info.getModel(); String unit = info.getUnit(); // 合并规格和单位 String specWithUnit = StringUtils.isNotBlank(unit) ? model + " " + unit : model; try { // 1. 生成二维码图片 String qrContent = String.format("ID:%s\n%s\n%s", materialId, name, model); ByteArrayOutputStream qrImageStream = generateQRCode(qrContent, 120, 120); // 2. 创建PDF文档 Document document = new Document(new Rectangle(170f, 113f)); PdfWriter.getInstance(document, new FileOutputStream("material_label_60x40.pdf")); document.setMargins(3, 3, 3, 3); document.open(); // 3. 创建主表格 PdfPTable mainTable = new PdfPTable(1); mainTable.setWidthPercentage(100); // 4. 标题行 Font titleFont = new Font(chineseFont.getBaseFont(), 10, Font.BOLD); PdfPCell titleCell = new PdfPCell(new Paragraph("物料标识卡", titleFont)); titleCell.setBorder(Rectangle.NO_BORDER); titleCell.setHorizontalAlignment(Element.ALIGN_CENTER); titleCell.setVerticalAlignment(Element.ALIGN_MIDDLE); titleCell.setPaddingBottom(2f); mainTable.addCell(titleCell); // 5. 内容区表格 PdfPTable contentTable = new PdfPTable(2); contentTable.setWidthPercentage(100); contentTable.setWidths(new float[]{1, 1.4f}); // 6. 二维码单元格(包含下方的编码) PdfPCell qrCell = new PdfPCell(); qrCell.setBorder(Rectangle.NO_BORDER); qrCell.setHorizontalAlignment(Element.ALIGN_CENTER); qrCell.setVerticalAlignment(Element.ALIGN_MIDDLE); // 添加二维码图片 Image qrImage = Image.getInstance(qrImageStream.toByteArray()); qrImage.scaleAbsolute(70, 70); qrCell.addElement(qrImage); // 在二维码下方添加编码(小字体) Font codeFont = new Font(chineseFont.getBaseFont(), 6); Paragraph codeParagraph = new Paragraph(materialId, codeFont); codeParagraph.setAlignment(Element.ALIGN_CENTER); qrCell.addElement(codeParagraph); contentTable.addCell(qrCell); // 7. 信息单元格 Font labelFont = new Font(chineseFont.getBaseFont(), 7, Font.BOLD); Font valueFont = new Font(chineseFont.getBaseFont(), 7); Paragraph infoParagraph = new Paragraph(); // 只保留名称和规格(规格已包含单位) infoParagraph.add(new Chunk("物料名称: ", labelFont)); infoParagraph.add(new Chunk(name + "\n", valueFont)); infoParagraph.add(new Chunk("物料规格: ", labelFont)); infoParagraph.add(new Chunk(specWithUnit, valueFont)); PdfPCell infoCell = new PdfPCell(infoParagraph); infoCell.setBorder(Rectangle.NO_BORDER); infoCell.setVerticalAlignment(Element.ALIGN_MIDDLE); contentTable.addCell(infoCell); // 8. 添加内容到主表格 mainTable.addCell(contentTable); // 9. 生成PDF document.add(mainTable); document.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } 2025-08-27 18:33:00.213 [http-nio-12575-exec-1] ERROR c.d.data.business.exception.WebExceptionHandler - 未知异常 url /wmsMaterielManage/printLabel/1.002.002.0007,异常信息:com.itextpdf.text.DocumentException: Font 'SimSun' with 'Identity-H' is not recognized. java.lang.RuntimeException: com.itextpdf.text.DocumentException: Font 'SimSun' with 'Identity-H' is not recognized.
最新发布
08-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值