iText 7列宽为什么这些不适合? [英] iText 7 column widths why do these not fit?

查看:544
本文介绍了iText 7列宽为什么这些不适合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iText的新手并直接跳入iText 7,但发现在我的桌子上设置列宽是非常具有挑战性的。显然有一些基本的我没有得到。

I am brand new to iText and leaped straight into iText 7, but find setting column widths on my table to be ridiculously challenging. Clearly there is something basic I'm not getting.

在梳理网络寻找答案时,我发现一些基于itext 5的答案似乎与itext 7(硬编码固定宽度)。 iText 7的东西似乎假设我没有的知识! 相对列宽 - 这是什么意思?数字加起来应该是100吗?有2或4列的简单示例是有意义的,但我很难将其转换为7列表。

In combing the web for answers, I found some itext 5-based answers that don't seem to be relevant to itext 7 (hard-coding a fixed width). The iText 7 stuff seems to assume knowledge I don't have! "relative column widths" -- what does that mean? should the numbers add up to 100? Simple examples with 2 or 4 columns make sense, but I'm having difficulty translating that into a 7-column table.

我有一个包含7列的表。第1列,第3列和第5列应该相对较窄,它们只是标签。第7列可以占用任何可用空间。例如:

I have a table with 7 columns. The 1st, 3rd, and 5th columns should be relatively narrow, they are simply labels. the 7th column can take up whatever space is available. For instance:

Col1  Col2         Col3  Col4         Col5      Col6        Col7 
CMS#: C34827284    Date: 12/5/16      End Date: 12/6/16     approved

我以为我拥有它,但我的最新尝试产生了元素不适合当前区域错误。以下是我如何定义它

I thought I had it, but my latest attempt produces "Element does not fit current area" errors. Here is how I defined it

Table htable = new Table(new float[] {3, 8, 5, 10, 5, 10, 30});

这些数字加起来为71,那为什么它们不适合?他们是否需要添加最多100个或者我完全在错误的轨道上添加最多100个的想法?

These numbers add up to 71 so why do they not fit? Do they need to add exactly up to 100 or am I completely on the wrong track with the "adds up to 100" idea?

推荐答案

你是否为整个桌子定义了宽度?

Did you define a width for the table as a whole?

这就是我尝试过的:

public void createPdf(String dest) throws IOException {
    //Initialize PDF writer
    PdfWriter writer = new PdfWriter(dest);

    //Initialize PDF document
    PdfDocument pdf = new PdfDocument(writer);

    // Initialize document
    Document document = new Document(pdf, PageSize.A4);

    // Create table
    Table htable = new Table(new float[] {3, 8, 5, 10, 5, 10, 30});
    htable.setFontSize(8);
    htable.setWidthPercent(100);
    htable.addHeaderCell(new Cell().add("Col1"));
    htable.addHeaderCell(new Cell().add("Col2"));
    htable.addHeaderCell(new Cell().add("Col3"));
    htable.addHeaderCell(new Cell().add("Col4"));
    htable.addHeaderCell(new Cell().add("Col5"));
    htable.addHeaderCell(new Cell().add("Col6"));
    htable.addHeaderCell(new Cell().add("Col7"));
    htable.addCell(new Cell().add("CMS#"));
    htable.addCell(new Cell().add("C34827284"));
    htable.addCell(new Cell().add("Date:"));
    htable.addCell(new Cell().add("12/5/16"));
    htable.addCell(new Cell().add("EndDate:"));
    htable.addCell(new Cell().add("12/6/16"));
    htable.addCell(new Cell().add("Approved"));
    // Add the table
    document.add(htable);
    // Close the document
    document.close();
}

结果如下所示:

请注意,我缩小了字体大小以避免更多文本被分割为 CMS#被拆分。

Note that I reduced the font size to avoid that more text is split the way CMS# is split.

尝试添加 htable.setWidthPercent(100); 如果您还没有这样做,请使用您的代码。问题应该消失。

Try adding htable.setWidthPercent(100); to your code if you didn't already do so. The problem should disappear.

这篇关于iText 7列宽为什么这些不适合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆