How to center a grid ?
2 Replies
1 reply marked as answer
AP
Anand Panchamoorthi
Syncfusion Team
January 6, 2021 12:51 PM UTC
Hi Mohamed Chaaben,
Thanks for contacting Syncfusion support.
We can draw grid into the center of the PDF document by calculating the width and height of the grid before drawing into the PDF page. Please find the below code examples to draw PDF grid into center of the PDF document.
|
With specifying width and height of the PDF grid |
//Create a new PDF document
final PdfDocument document = PdfDocument();
//Add a new page
final PdfPage page = document.pages.add();
//Get page size
final Size pageSize = page.getClientSize();
//Create a new PDF grid
final PdfGrid grid = PdfGrid();
//Add columns
grid.columns.add(count: 3);
//Set column widths
grid.columns[0].width = 150;
grid.columns[1].width = 100;
grid.columns[2].width = 150;
//Add headers
grid.headers.add(1);
final PdfGridRow header = grid.headers[0];
//Set header row height
header.height = 60;
header.cells[0].value = 'Header - 1 Cell - 1';
header.cells[1].value = 'Header - 1 Cell - 2';
header.cells[2].value = 'Header - 1 Cell - 3';
//Add rows
final PdfGridRow row1 = grid.rows.add();
//Set height of the first row
row1.height = 100;
row1.cells[0].value = 'Row - 1 Cell - 1';
row1.cells[1].value = 'Row - 1 Cell - 2';
row1.cells[2].value = 'Row - 1 Cell - 3';
final PdfGridRow row2 = grid.rows.add();
//Set height of the second row
row2.height = 150;
row2.cells[0].value = 'Row - 2 Cell - 1';
row2.cells[1].value = 'Row - 2 Cell - 2';
row2.cells[2].value = 'Row - 2 Cell - 3';
//Calculate height of the grid before draw
double height = 0;
for (int i = 0; i < grid.headers.count; i++) {
height += grid.headers[i].height;
}
for (int i = 0; i < grid.rows.count; i++) {
height += grid.rows[i].height;
}
//Calculate width of the grid before draw
double width = 0;
for (int i = 0; i < grid.columns.count; i++) {
width += grid.columns[i].width;
}
//Check whether the calculated height is less than the page height
if (height <= pageSize.height) {
//We can adjust the grid horizontally by set the x and width values based on the page size and width calculated
//We can adjust the grid vertically by set the y and height based on the height calculated. If the grid height greater less than page size, then we can use the below bounds to center.
grid.draw(
page: page,
bounds: Rect.fromLTWH(
((pageSize.width - width) / 2), ((pageSize.height - height) / 2), width, 0));
} else {
//We can ajust the x and width based on the page size to locate grid in center
grid.draw(
page: page,
bounds: Rect.fromLTWH(((pageSize.width - width) / 2), 0, width, 0));
//We can use PdfLayoutFormat to set grid pagination bounds
}
//Save PDF document
final List<int> bytes = document.save();
Please find the android studio sample from https://www.syncfusion.com/downloads/support/directtrac/general/ze/create_pdf_file1067002468 . |
|
Without specifying width and height of the PDF grid |
//Create a PDF document
final PdfDocument document = PdfDocument();
//Add a new page
final PdfPage page = document.pages.add();
//Get page size
final Size pageSize = page.getClientSize();
//Create a new PDF grid
final PdfGrid grid = PdfGrid();
//Add Columns
grid.columns.add(count: 3);
//Add headers
grid.headers.add(1);
final PdfGridRow header = grid.headers[0];
header.cells[0].value = 'Header - 1 Cell - 1';
header.cells[1].value = 'Header - 1 Cell - 2';
header.cells[2].value = 'Header - 1 Cell - 3';
//Add rows
final PdfGridRow row1 = grid.rows.add();
row1.cells[0].value = 'Row - 1 Cell - 1';
row1.cells[1].value = 'Row - 1 Cell - 2';
row1.cells[2].value = 'Row - 1 Cell - 3';
final PdfGridRow row2 = grid.rows.add();
row2.cells[0].value = 'Row - 2 Cell - 1';
row2.cells[1].value = 'Row - 2 Cell - 2';
row2.cells[2].value = 'Row - 2 Cell - 3';
//Calculate height of the grid before draw
double height = 0;
for (int i = 0; i < grid.headers.count; i++) {
height += grid.headers[i].height;
}
for (int i = 0; i < grid.rows.count; i++) {
height += grid.rows[i].height;
}
//Check whether the calculated height is less than the page height
if (height <= pageSize.height) {
//We can adjust the grid horizontally by set the x and width values based on the page size
//We can adjust the grid vertically by set the y and height based on the height calculated. If the grid height greater less than page size, then we can use the below bounds to center.
grid.draw(
page: page,
bounds: Rect.fromLTWH(
20, ((pageSize.height - height) / 2), pageSize.width - 40, 0));
} else {
//We can ajust the x and width based on the page size to locate grid in center
grid.draw(
page: page,
bounds: Rect.fromLTWH(20, 0, pageSize.width - 40, 0));
//We can use PdfLayoutFormat to set grid pagination bounds
}
//Save PDF document
final List<int> bytes = document.save();
Please find the android studio sample from https://www.syncfusion.com/downloads/support/directtrac/general/ze/create_pdf_file750172005 . |
Please let us know if you need any further assistance in this.
With Regards,
Anand Panchamoorthi
Marked as answer
MC
mohamed chaaben
January 6, 2021 05:55 PM UTC
Thank you very much.
SIGN IN To post a reply.
- 2 Replies
- 2 Participants
- Marked answer
-
MC mohamed chaaben
- Jan 5, 2021 11:35 PM UTC
- Jan 6, 2021 05:55 PM UTC