使用iText7形成此布局的最佳方法是什么?

时间:2018-05-21 01:33:00

标签: java layout itext7

我正在尝试在PDF中创建此布局 页面布局是顶部的单个表格,宽度为100%(标有红色 - 表格1) 并排表格(每个棕色方框是表格表2至表格7)

棕色表具有恒定的大小和不变的位置 我能够使用:

成功创建标题和table1
    table2.setHorizontalAlignment(HorizontalAlignment.RIGHT);
    table2.setMaxWidth(UnitValue.createPercentValue(50f));

    table3.setHorizontalAlignment(HorizontalAlignment.LEFT);
    table3.setMaxWidth(UnitValue.createPercentValue(50f));

我的问题在于其余的表格(在图像中标记为棕色)
我试图将它们并排放置:

if (!change.after.val()) {
  return console.log('Una notificacion se elimino de la base de datos : ', notification_id);
}

但是他们不会在同一条线上对齐,而是在另一条线下面对齐

我的问题是实现这种pdf布局的正确方法是什么?

desired pdf layout

1 个答案:

答案 0 :(得分:2)

由于表具有恒定的大小和恒定位置,您可以简单地使用iText 7功能设置固定位置,宽度和高度,特别是表格:

/**
 * Sets values for a absolute repositioning of the Element.
 * The coordinates specified correspond to the
 * bottom-left corner of the element and it grows upwards.
 * Also has as a side effect that the Element's {@link Property#POSITION} is changed to
 * {@link LayoutPosition#FIXED fixed}.
 *
 * @param left   horizontal position of the bottom-left corner on the page
 * @param bottom vertical position of the bottom-left corner on the page
 * @param width  a floating point value measured in points.
 * @return this Element.
 */
public T setFixedPosition(float left, float bottom, float width)

/**
 * Sets the height property a block element as a point-value.
 *
 * @param height a floating point value for the new height
 * @return the block element itself.
 */
public T setHeight(float height)

你大致得到了这样的草图布局:

try (   PdfDocument pdfDocument = new PdfDocument(new PdfWriter(...));
        Document document = new Document(pdfDocument)) {
    PageSize pageSize = pdfDocument.getDefaultPageSize();

    Table table = new Table(1);
    table.addCell("table 1 - 1");
    table.addCell("table 1 - 2");
    table.setFixedPosition(pageSize.getLeft() + 30, pageSize.getTop() - 75, pageSize.getWidth() - 60);
    table.setHeight(45);
    document.add(table);

    table = new Table(UnitValue.createPercentArray(new float[] {40, 60}));
    table.addCell("table 2 - 1");
    table.addCell("table 2 - 2");
    table.setFixedPosition(pageSize.getLeft() + 30, pageSize.getTop() - 265, (pageSize.getWidth() - 70) / 2);
    table.setHeight(185);
    document.add(table);

    table = new Table(UnitValue.createPercentArray(new float[] {20, 50, 30}));
    table.addCell("table 4 - 1");
    table.addCell("table 4 - 2");
    table.addCell("table 4 - 3");
    table.setFixedPosition(pageSize.getLeft() + 30, pageSize.getTop() - 720, (pageSize.getWidth() - 70) / 2);
    table.setHeight(450);
    document.add(table);

    table = new Table(1);
    table.addCell("table 6");
    table.setFixedPosition(pageSize.getLeft() + 30, pageSize.getTop() - 810, (pageSize.getWidth() - 70) / 2);
    table.setHeight(85);
    document.add(table);

    table = new Table(UnitValue.createPercentArray(new float[] {20, 40, 20, 20}));
    table.addCell("table 3 - 1");
    table.addCell("table 3 - 2");
    table.addCell("table 3 - 3");
    table.addCell("table 3 - 4");
    table.setFixedPosition(pageSize.getRight() - (pageSize.getWidth() - 10) / 2, pageSize.getTop() - 345, (pageSize.getWidth() - 70) / 2);
    table.setHeight(265);
    document.add(table);

    table = new Table(1);
    table.addCell("table 5 - 1");
    table.addCell("table 5 - 2");
    table.setFixedPosition(pageSize.getRight() - (pageSize.getWidth() - 10) / 2, pageSize.getTop() - 640, (pageSize.getWidth() - 70) / 2);
    table.setHeight(290);
    document.add(table);

    table = new Table(UnitValue.createPercentArray(new float[] {20, 50, 30}));
    table.addCell("table 7 - 1");
    table.addCell("table 7 - 2");
    table.addCell("table 7 - 3");
    table.setFixedPosition(pageSize.getRight() - (pageSize.getWidth() - 10) / 2, pageSize.getTop() - 810, (pageSize.getWidth() - 70) / 2);
    table.setHeight(165);
    document.add(table);
}

FixedPositionLayouts test testFixedTablePositions

结果:

screen shot

(由于你没有给出精确的坐标,上面只能是近似值。你必须相应地调整值。)