Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to create this layout in PDF
The page layout is single table on the top with 100% width (marked with red color - table 1)
And side by side tables (each brown box is a table table 2 to table 7)
brown tables have constant sizes and constant positions
I was able to successfully create the title and table1 using:
table.useAllAvailableWidth();
My problem is with the rest of the tables (marked in the image as brown)
I tried to lay them side by side using:
table2.setHorizontalAlignment(HorizontalAlignment.RIGHT);
table2.setMaxWidth(UnitValue.createPercentValue(50f));
table3.setHorizontalAlignment(HorizontalAlignment.LEFT);
table3.setMaxWidth(UnitValue.createPercentValue(50f));
But they wont align together on the same line but one below the other
My question is what is the correct way to achieve this pdf layout?
–
–
* 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)
You get approximately your sketched layout like this:
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
)
The result:
(As you did not give exact coordinates, the above can only be an approximation. You'll have to adapt the values accordingly.)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.