You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iText表格买卖方列间竖线未贴合边框的解决求助

解决买卖方表格竖线无法贴合边框的问题

问题根源

你的代码里有两个关键问题导致竖线无法贴合边框:

  1. 多余添加了空单元格table.addCell(new Cell().setBorder(Border.NO_BORDER));,让表格多出一行空行,直接截断了中间的竖线
  2. 单元格边框设置逻辑混乱,重复设置Border.NO_BORDER,且单独设置左右边框时未和表格整体边框衔接

修改后的代码

// Create table for seller and buyer information
Table table = new Table(UnitValue.createPercentArray(new float[]{50, 50}));
table.setWidth(UnitValue.createPercentValue(100));
// 设置表格整体边框与列间竖线
table.setBorder(new SolidBorder(1));
table.setVerticalBorder(new SolidBorder(1));

// Add seller and buyer information
// 卖家标题单元格:仅保留右边框
table.addCell(new Cell().add(new Paragraph("Seller:"))
        .setBorderLeft(Border.NO_BORDER)
        .setBorderTop(Border.NO_BORDER)
        .setBorderBottom(Border.NO_BORDER)
        .setPadding(5));
// 买家标题单元格:仅保留左边框
table.addCell(new Cell().add(new Paragraph("Buyer:"))
        .setBorderRight(Border.NO_BORDER)
        .setBorderTop(Border.NO_BORDER)
        .setBorderBottom(Border.NO_BORDER)
        .setPadding(5));

// 卖家详情单元格:仅保留右边框
table.addCell(new Cell().add(new Paragraph("XYZ Pvt. Ltd.\nNew Delhi, India\nGSTIN: 29AABBCCDD121ZD"))
        .setBorderLeft(Border.NO_BORDER)
        .setBorderTop(Border.NO_BORDER)
        .setBorderBottom(Border.NO_BORDER)
        .setPadding(5));
// 买家详情单元格:仅保留左边框
table.addCell(new Cell().add(new Paragraph("Vedant Computers\nNew Delhi, India\nGSTIN: 29AABBCCDD131ZD"))
        .setBorderRight(Border.NO_BORDER)
        .setBorderTop(Border.NO_BORDER)
        .setBorderBottom(Border.NO_BORDER)
        .setPadding(5));

// Add the seller and buyer information table to the document
document.add(table);

// Add items information
Table itemsTable = new Table(UnitValue.createPercentArray(new float[]{25, 25, 25, 25}));
itemsTable.setWidth(UnitValue.createPercentValue(100));
itemsTable.setBorder(new SolidBorder(1));

// Add table headers
itemsTable.addCell(new Paragraph("Item").setTextAlignment(TextAlignment.CENTER));
itemsTable.addCell(new Paragraph("Quantity").setTextAlignment(TextAlignment.CENTER));
itemsTable.addCell(new Paragraph("Rate").setTextAlignment(TextAlignment.CENTER));
itemsTable.addCell(new Paragraph("Amount").setTextAlignment(TextAlignment.CENTER));

// Add table data
itemsTable.addCell(new Paragraph("Product 1").setTextAlignment(TextAlignment.CENTER));
itemsTable.addCell(new Paragraph("12 Nos").setTextAlignment(TextAlignment.CENTER));
itemsTable.addCell(new Paragraph("123.00").setTextAlignment(TextAlignment.CENTER));
itemsTable.addCell(new Paragraph("1476.00").setTextAlignment(TextAlignment.CENTER));

document.add(itemsTable);

关键修改说明

  1. 移除多余空单元格:删掉无效的空单元格代码,让表格保持2行2列的正确结构,竖线可贯穿整个表格高度
  2. 统一边框控制:通过table.setVerticalBorder直接设置列间竖线,或通过单元格边框取舍实现,避免重复设置导致边框断裂
  3. 优化单元格边框:仅隐藏不需要的边框,让中间竖线与表格上下边框完美衔接
  4. 修正文本对齐:给每个数据单元格单独设置对齐样式,避免全局设置导致的样式混乱

内容的提问来源于stack exchange,提问作者shrimaan0205

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 15:58:15