|
//Create a new PDF document
final PdfDocument document = PdfDocument();
//Add a new page
final PdfPage page = document.pages.add();
//Gets page size
final Size pageSize = page.getClientSize();
//Font used to draw text in PDF grid
final PdfFont font = PdfStandardFont(PdfFontFamily.timesRoman, 25);
//Create a new PDF grid
final PdfGrid grid = PdfGrid();
//Set font of the text in PDF grid cells
grid.style.font = font;
//Add 3 columns
grid.columns.add(count: 2);
//Add headers in PDF grid
grid.headers.add(1);
//Header row with 90/270 degree rotated text
final PdfGridRow header = grid.headers[0];
header.cells[0].value = '90 Degree Rotated';
header.cells[1].value = '270 Degree Rotated';
//Calculate height of 90/270 degree rotate text
header.height = calculateHeightInRotation(header, font);
//Add rows in PDF grid
final PdfGridRow row1 = grid.rows.add();
row1.cells[0].value = 'Row 1 - Cell 1';
row1.cells[1].value = 'Row 1 - Cell 2';
final PdfGridRow row2 = grid.rows.add();
row2.cells[0].value = 'Row 2 - Cell 1';
row2.cells[1].value = 'Row 2 - Cell 2';
final PdfGridRow row3 = grid.rows.add();
row3.cells[0].value = 'Row 3 - Cell 1';
row3.cells[1].value = 'Row 3 - Cell 2';
//Call back function to rotate text in PDF grid cells
grid.beginCellLayout = onBeginCellLayout;
//Draw grid into PDF page
grid.draw(
page: page,
bounds: Rect.fromLTWH(0, 0, pageSize.width, pageSize.height));
//Save PDF document
final List<int> bytes = document.save();
//Call back function to rotate text in header cells
void onBeginCellLayout(Object sender, PdfGridBeginCellLayoutArgs args) {
if (args.isHeaderRow) {
//Accessing grid instance
PdfGrid grid = sender as PdfGrid;
//Getting the value of corresponding cell
PdfGridCell cell = grid.headers[args.rowIndex].cells[args.cellIndex];
dynamic value = cell.value;
if (value != null && value is String && value.isNotEmpty) {
//Save current graphics state
PdfGraphicsState state = args.graphics.save();
if (args.cellIndex == 0) {//90 degree rotation
//Move current position to top-right of the cell bounds
args.graphics.translateTransform(args.bounds.right, args.bounds.top);
//Rotate graphics to 90 degree
args.graphics.rotateTransform(90);
//Draw string in rotated graphics at (0, 0) offset
//We can set X position of the offset as per the text alighments
args.graphics.drawString(
value, PdfStandardFont(PdfFontFamily.timesRoman, 25),
brush: PdfBrushes.black,
bounds: Rect.fromLTWH(0, 0, args.bounds.width, args.bounds.height));
} else {//270 degree rotation
//Move current position to left-bottom of the cell bounds
args.graphics.translateTransform(args.bounds.left, args.bounds.bottom);
//Rotate graphics to 270 degree
args.graphics.rotateTransform(270);
//Draw string in rotated graphics at (0, 0) offset
//We can set X position of the offset as per the text alighments
args.graphics.drawString(
value, PdfStandardFont(PdfFontFamily.timesRoman, 25),
brush: PdfBrushes.black,
bounds: Rect.fromLTWH(0, 0, args.bounds.width, args.bounds.height));
}
//Restore graphics
args.graphics.restore(state);
//Set empty seting for the value of grid cell
grid.headers[args.rowIndex].cells[args.cellIndex].value = '';
}
}
}
//Calculate height of the grid row for 90 or 270 degree text rotation
double calculateHeightInRotation(PdfGridRow row, PdfFont font) {
double height = 0;
for (int i = 0; i < row.cells.count; i++) {
dynamic value = row.cells[i].value;
if (value != null && value is String && value.isNotEmpty) {
Size size = font.measureString(value);
if (size.width > height) {
height = size.width;
}
}
}
return height;
} |
|
int currentRowIndex = -1;
//Call back function to rotate text in header cells
void onBeginCellLayout(Object sender, PdfGridBeginCellLayoutArgs args) {
if (args.isHeaderRow) {
if (args.cellIndex) {
currentRowIndex++;
}
}
//We can use currentRowIndex instead of args.rowIndex for all header rows.
<<<Your code>>>
} |
|
//Call back function to rotate text in header cells
void _onBeginCellLayout(Object sender, PdfGridBeginCellLayoutArgs args) {
if (args.isHeaderRow) {
//Accessing grid instance
final PdfGrid grid = sender as PdfGrid;
//Getting the value of corresponding cell
final PdfGridCell cell = grid.headers[args.rowIndex].cells[args.cellIndex];
//Draw rectangle with background brush on the graphics before text rotation applied
if (cell.style.backgroundBrush != null) {
args.graphics.drawRectangle(
brush: cell.style.backgroundBrush, bounds: args.bounds);
cell.style.backgroundBrush = null;
}
final dynamic value = cell.value;
if (value != null && value is String && value.isNotEmpty) {
//Save current graphics state
final PdfGraphicsState state = args.graphics.save();
if (args.rowIndex == 0) {
//90 degree rotation
//Move current position to top-right of the cell bounds
args.graphics.translateTransform(args.bounds.right, args.bounds.top);
//Rotate graphics to 90 degree
args.graphics.rotateTransform(90);
//Draw string in rotated graphics at (0, 0) offset
//We can set X position of the offset as per the text alighments
args.graphics.drawString(
value, PdfStandardFont(PdfFontFamily.timesRoman, 25),
brush: PdfBrushes.black,
bounds: Rect.fromLTWH(0, 0, args.bounds.width, args.bounds.height));
} else {
//270 degree rotation
//Move current position to left-bottom of the cell bounds
args.graphics.translateTransform(args.bounds.left, args.bounds.bottom);
//Rotate graphics to 270 degree
args.graphics.rotateTransform(270);
//Draw string in rotated graphics at (0, 0) offset
//We can set X position of the offset as per the text alighments
args.graphics.drawString(
value, PdfStandardFont(PdfFontFamily.timesRoman, 25),
brush: PdfBrushes.black,
bounds: Rect.fromLTWH(0, 0, args.bounds.width, args.bounds.height));
}
//Restore graphics
args.graphics.restore(state);
//Set empty seting for the value of grid cell
grid.headers[args.rowIndex].cells[args.cellIndex].value = '';
}
}
}
//Calculate height of the grid row for 90 or 270 degree text rotation
double _calculateHeightInRotation(PdfGridRow row, PdfFont font) {
double height = 0;
for (int i = 0; i < row.cells.count; i++) {
final dynamic value = row.cells[i].value;
if (value != null && value is String && value.isNotEmpty) {
final Size size = font.measureString(value);
if (size.width > height) {
height = size.width;
}
}
}
return height;
}
//Create a new PDF document
final PdfDocument document = PdfDocument();
//Add a new page
final PdfPage page = document.pages.add();
//Gets page size
final Size pageSize = page.getClientSize();
//Font used to draw text in PDF grid
final PdfFont font = PdfStandardFont(PdfFontFamily.timesRoman, 25);
//Create a new PDF grid
final PdfGrid grid = PdfGrid();
//Set font of the text in PDF grid cells
grid.style.font = font;
//Add 3 columns
grid.columns.add(count: 2);
//Add headers in PDF grid
grid.headers.add(2);
//Header row with 90/270 degree rotated text
final PdfGridRow header1 = grid.headers[0];
header1.cells[0].value = '90 Degree Rotated';
//Assign background brush
header1.cells[0].style.backgroundBrush = PdfBrushes.blue;
header1.cells[1].value = '90 Degree Rotated';
//Assign background brush
header1.cells[1].style.backgroundBrush = PdfBrushes.orange;
//Calculate height of 90 degree rotate text
header1.height = _calculateHeightInRotation(header1, font);
final PdfGridRow header2 = grid.headers[1];
header2.cells[0].value = '270 Degree Rotated';
//Assign background brush
Header2.cells[0].style.backgroundBrush = PdfBrushes.red;
header2.cells[1].value = '270 Degree Rotated';
//Calculate height of 270 degree rotate text
header2.height = _calculateHeightInRotation(header2, font);
//Add rows in PDF grid
final PdfGridRow row1 = grid.rows.add();
row1.cells[0].value = 'Row 1 - Cell 1';
row1.cells[1].value = 'Row 1 - Cell 2';
final PdfGridRow row2 = grid.rows.add();
row2.cells[0].value = 'Row 2 - Cell 1';
row2.cells[1].value = 'Row 2 - Cell 2';
final PdfGridRow row3 = grid.rows.add();
row3.cells[0].value = 'Row 3 - Cell 1';
row3.cells[1].value = 'Row 3 - Cell 2';
//Call back function to rotate text in PDF grid cells
grid.beginCellLayout = _onBeginCellLayout;
//Draw grid into PDF page
grid.draw(
page: page,
bounds: Rect.fromLTWH(0, 0, pageSize.width, pageSize.height));
//Save PDF document
final List<int> bytes = document.save();
|