java - iText 7 - 设置单元格宽度

标签 java itext7

我正在使用 iText 7 生成一个包含表格的 PDF。

我想定义单元格的大小。

例如我有这个: enter image description here

我想减少带有数字的列(07、08 ...)并增加其他列。

这是我创建表格的方法:

int nbColumns = 7 + planning.size() *4;
Table table = new Table(nbColumns);
table.setWidthPercent(100);
table.addCell(createCell(""));
table.addCell(createCell("Col1"));
table.addCell(createCell("Col2"));
DateFormat hourFormat = new SimpleDateFormat("HH", Locale.FRENCH);
for(Date hourDate : planning){
    table.addCell(new Cell(1,4).setTextAlignment(TextAlignment.CENTER).add(hourFormat.format(hourDate)).setFont(regular).setFontSize(10));
}

table.addCell(createCell("Long"));
table.addCell(createCell("A"));
table.addCell(createCell("B"));
table.addCell(createCell(""));

我尝试使用 Cell 的方法 setWidth 但它没有执行任何操作。

我尝试在创建表时定义列的宽度:

float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2,2};
Table table = new Table(colWidths);

但是它造成了标题单元格一行多行的困惑。

你能帮我吗?

最佳答案

如果 float[] colWidths = {1,1,1,1,1,1,1,1,2,1,1,1,1,1,5,1,1,2,2 ,2}; 是一个数组,其中包含您想要的每列的相对宽度,将它们转换为 UnitValue 百分比数组,并使用它构造您的表格:

Table table = new Table(UnitValue.createPercentArray(colWidths));

如果您希望表格的绝对宽度高于为列传递的宽度,请使用 Table#setFixedLayout()

运行和/或试验以下代码可能有助于查看每种不同类型声明的效果:

public void createPdf(String dest) throws IOException, FileNotFoundException{
    PdfWriter writer = new PdfWriter(dest);
    PdfDocument pdfDoc = new PdfDocument(writer);
    Document doc = new Document(pdfDoc);
    float tableWidth = 100f;
    //Using defaults
    doc.add(new Paragraph("default/auto-layout"));
    doc.add(new Paragraph("Using a float[]"));
    float[] colWidths = {1,2,3,4};
    Table table = new Table(colWidths);
    table.setWidthPercent(tableWidth);
    fillTable(table);
    doc.add(table);

    doc.add(new Paragraph("Using a UnitValue[] point array"));
    colWidths=new float[]{20f,40f,80f,160f};
    table = new Table(UnitValue.createPointArray(colWidths));
    table.setWidth(tableWidth);
    fillTable(table);
    doc.add(table);

    doc.add(new Paragraph("Using a UnitValue[] percent array"));
    colWidths=new float[]{1,2,3,4};
    table = new Table(UnitValue.createPercentArray(colWidths));
    table.setWidthPercent(tableWidth);
    fillTable(table);
    doc.add(table);

    doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    //Using fixedLayout
    doc.add(new Paragraph("Using fixedLayout"));
    doc.add(new Paragraph("Using a float[]"));
    colWidths =new float[]{1,2,3,4};
    table = new Table(colWidths);
    table.setFixedLayout();
    table.setWidthPercent(tableWidth);
    fillTable(table);
    doc.add(table);

    doc.add(new Paragraph("Using a UnitValue[] point array"));
    colWidths=new float[]{20f,40f,80f,160f};
    table = new Table(UnitValue.createPointArray(colWidths));
    table.setWidthPercent(tableWidth);
    table.setFixedLayout();
    fillTable(table);
    doc.add(table);

    doc.add(new Paragraph("Using a UnitValue[] percent array"));
    colWidths=new float[]{1,2,3,4};
    table = new Table(UnitValue.createPercentArray(colWidths));
    table.setWidthPercent(tableWidth);
    table.setFixedLayout();
    fillTable(table);
    doc.add(table);

    doc.close();
}

public void fillTable(Table table){
    table.addCell("Water");
    table.addCell("Fire");
    table.addCell("Heaven");
    table.addCell("As I'm grounded here on");
    table.addCell("It's waving oceans are calling");
    table.addCell("is burning me deep inside");
    table.addCell("can wait for me");
    table.addCell("Earth");
}

关于java - iText 7 - 设置单元格宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46928562/

相关文章:

java - 如何过滤包含列表的对象列表?

java - 在 Hibernate 中批量删除时查询语法错误

java - 使用iText 7和Java生成pdf无法换行长英文单词

c# - 如何从 PDF 中提取所有值?

java - 使用 iText Java 循环创建新的 pdf

java - iText 7 itextpdf.kernel.PdfException

java - 使用单独的方法在 JFrame 中绘制线条

java - 如何在用户计算机上启动本地端口(编辑问题)

java - libGDX 和 Eclipse - 找不到 tools.jar

java - 如何获取特定 pdf 表单字段的页码和位置,并在 iText7 中的相应位置插入表格?