作为
表的大小和位置是固定的
,您只需使用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
测试
testFixedTablePositions
)
结果是:
(因为你没有给出精确的坐标,所以上面只能是一个近似值。您必须相应地调整这些值。)