Pdf Component - PdfGrid

30 Sep 202624 minutes to read

Represents a table structure used to display tabular data in a PDF document.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the data source and column mapping
let dataSource: object[] = [{ id: '1001', name: 'John' }, { id: '1002', name: 'David' }];
let columns: PdfColumnInformation[] = [{ field: 'id', headerText: 'Employee ID', width: 80 }, { field: 'name', headerText: 'Employee Name', width: 120 }];
// Create a new PDF grid
let grid: PdfGrid = new PdfGrid(dataSource, columns);
// Draw the grid on the page
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 240, height: 160 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Properties

_nestedContinuationPage PdfPage|null

Final physical page produced by a paginating nested grid during the current row render.

_nestedContinuationY number

Y coordinate immediately below the final nested-grid fragment on its physical page.

Get columnWidths Map

Gets the column widths map for the grid.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the explicit row and column structure with custom widths
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120], [2, 150]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'ID' }, { value: 'Name' }, { value: 'Department' }] }];
let rows: PdfGridRow[] = [{ cells: [{ value: 'E01' }, { value: 'John' }, { value: 'Sales' }] }];
// Create a new PDF grid with specified column widths
let grid: PdfGrid = new PdfGrid(3, widths, rows, headers);
// Retrieve the column widths map
let columnWidthsMap: Map<number, number> = grid.columnWidths;
// Draw the grid on the page
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 350, height: 160 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Get dataSource object[]

Gets the current source records for a data-source grid.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the data source and column mapping
let dataSource: object[] = [{ id: '1001', name: 'John' }, { id: '1002', name: 'David' }];
let columns: PdfColumnInformation[] = [{ field: 'id', headerText: 'Employee ID', width: 80 }, { field: 'name', headerText: 'Employee Name', width: 120 }];
// Create a new PDF grid from the data source
let grid: PdfGrid = new PdfGrid(dataSource, columns);
// Retrieve the current data source
let currentData: object[] = grid.dataSource;
// Draw the grid on the page
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 240, height: 160 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Get headersCount number

Gets the number of header rows currently available in the grid.

Get horizontalOverflow PdfGridHorizontalOverflowType

Gets or sets how the grid handles horizontal width overflow.
The default is PdfGridHorizontalOverflowType.onePage.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the explicit row and column structure
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120], [2, 80], [3, 120]]);
let rows: PdfGridRow[] = [{ cells: [{ value: 'A' }, { value: 'B' }, { value: 'C' }, { value: 'D' }] }];
// Create a new PDF grid
let grid: PdfGrid = new PdfGrid(4, widths, rows);
// Set horizontal overflow mode
grid.horizontalOverflow = PdfGridHorizontalOverflowType.nextPage;
// Draw the grid on the page
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 150, height: 400 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Set horizontalOverflow void

Sets how the grid handles horizontal width overflow.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the explicit row and column structure
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120], [2, 80], [3, 120]]);
let rows: PdfGridRow[] = [{ cells: [{ value: 'A' }, { value: 'B' }, { value: 'C' }, { value: 'D' }] }];
// Create a new PDF grid
let grid: PdfGrid = new PdfGrid(4, widths, rows);
// Set horizontal overflow mode
grid.horizontalOverflow = PdfGridHorizontalOverflowType.nextPage;
// Draw the grid on the page
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 150, height: 400 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
value PdfGridHorizontalOverflowType The horizontal overflow mode to set.

Get numberOfColumns number

Gets the column count for the grid.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the explicit row and column structure
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'ID' }, { value: 'Name' }] }];
let rows: PdfGridRow[] = [{ cells: [{ value: 'E01' }, { value: 'John' }] }];
// Create a new PDF grid with 2 columns
let grid: PdfGrid = new PdfGrid(2, widths, rows, headers);
// Retrieve the column count
let columnCount: number = grid.numberOfColumns;
// Draw the grid on the page with updated data
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 240, height: 160 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Get recordsCount number

Gets the number of record rows currently available in the grid.
Header rows are not included in this count.

Get repeatHeader boolean

Gets the flag controlling whether header rows repeat on continuation pages.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the explicit row and column structure
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'Employee ID' }, { value: 'Employee Name' }] }];
let rows: PdfGridRow[] = [{ cells: [{ value: 'E01' }, { value: 'John' }] }];
// Create a new PDF grid
let grid: PdfGrid = new PdfGrid(2, widths, rows, headers);
// Check the current repeat header setting (default is true)
let repeatsHeaders: boolean = grid.repeatHeader;
// Draw the grid on the page
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 240, height: 160 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Set repeatHeader void

Sets the flag controlling whether header rows repeat on continuation pages.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the explicit row and column structure
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'Employee ID' }, { value: 'Employee Name' }] }];
let rows: PdfGridRow[] = [{ cells: [{ value: 'E01' }, { value: 'John' }] }, { cells: [{ value: 'E02' }, { value: 'Alice' }] }];
// Create a new PDF grid
let grid: PdfGrid = new PdfGrid(2, widths, rows, headers);
// Disable header repetition on continuation pages
grid.repeatHeader = false;
// Draw the grid with pagination (headers only appear on first page)
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 240, height: 160 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
value boolean True to repeat headers on all pages; false to show headers only on the first page.

Get style PdfGridStyle

Gets the grid-level style.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the explicit row and column structure
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'Employee ID' }, { value: 'Employee Name' }] }];
let rows: PdfGridRow[] = [{ cells: [{ value: 'E01' }, { value: 'John' }] }];
// Create a new PDF grid with grid-level style defaults
let gridStyle: PdfGridStyle = { backgroundColor: { r: 240, g: 240, b: 240 }, textColor: { r: 0, g: 0, b: 0 } };
let grid: PdfGrid = new PdfGrid(2, widths, rows, headers, {style: gridStyle});
// Retrieve the grid-level style defaults
let currentStyle: PdfGridStyle = grid.style;
// Draw the grid on the page with applied styles
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 240, height: 160 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Set style void

Sets the grid-level style.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create the explicit row and column structure
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'Employee ID' }, { value: 'Employee Name' }] }];
let rows: PdfGridRow[] = [{ cells: [{ value: 'E01' }, { value: 'John' }] }, { cells: [{ value: 'E02' }, { value: 'Alice' }] }];
// Create a new PDF grid
let grid: PdfGrid = new PdfGrid(2, widths, rows, headers);
// Define grid-level style with light gray background and black text
let newStyle: PdfGridStyle = {
    backgroundColor: { r: 240, g: 240, b: 240 },
    textColor: { r: 0, g: 0, b: 0 },
    font: { size: 10 }
};
// Set the grid-level style defaults
grid.style = newStyle;
// Draw the grid on the page with applied style defaults
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 240, height: 160 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
value PdfGridStyle A style object containing grid-level default styling properties.

Methods

addHeader

Adds a header row to the grid.

// Create a new PDF document.
const document = new PdfDocument();
// Add a new page to the document.
const page = document.pages.add();
// Define column widths for the grid.
const columnWidths = new Map<number, number>([[0, 100], [1, 100]]);
// Create a new PDF grid with 2 columns.
const grid = new PdfGrid(2, columnWidths, []);
// Add header row to the grid.
grid.addHeader({ cells: [{ value: 'ID' }, { value: 'Name' }] });
// Add the first data row using addRow method.
grid.addRow({ cells: [{ value: '1001' }, { value: 'John' }] });
// Add the second data row using addRow method.
grid.addRow({ cells: [{ value: '1002' }, { value: 'Jane' }] });
// Add the third data row using addRow method.
grid.addRow({ cells: [{ value: '1003' }, { value: 'Michael' }] });
// Draw the grid on the page within the specified bounds.
grid.draw(page, { x: 50, y: 50, width: 200, height: 400 });
// Save the document.
document.save('grid.pdf');
// Destroy the document.
document.destroy();
Parameter Type Description
gridRow PdfGridRow Header row to add.

Returns void

addRow

Validates and appends one record row to the grid.

// Create a new PDF document.
const document = new PdfDocument();
// Add a new page to the document.
const page = document.pages.add();
// Define column widths for the grid.
const columnWidths = new Map<number, number>([[0, 100], [1, 100]]);
// Create a new PDF grid with 2 columns.
const grid = new PdfGrid(2, columnWidths, []);
// Add header row to the grid.
grid.addHeader({ cells: [{ value: 'ID' }, { value: 'Name' }] });
// Add the first data row using addRow method.
grid.addRow({ cells: [{ value: '1001' }, { value: 'John' }] });
// Add the second data row using addRow method.
grid.addRow({ cells: [{ value: '1002' }, { value: 'Jane' }] });
// Add the third data row using addRow method.
grid.addRow({ cells: [{ value: '1003' }, { value: 'Michael' }] });
// Draw the grid on the page within the specified bounds.
grid.draw(page, { x: 50, y: 50, width: 200, height: 400 });
// Save the document.
document.save('grid.pdf');
// Destroy the document.
document.destroy();
Parameter Type Description
gridRow PdfGridRow The record row to append.

Returns void

draw

Draws the grid on the specified page within the specified bounds.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create a grid with multiple rows
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'Employee ID' }, { value: 'Employee Name' }] }];
let rows: PdfGridRow[] = [
    { cells: [{ value: 'E01' }, { value: 'John' }] },
    { cells: [{ value: 'E02' }, { value: 'Alice' }] },
    { cells: [{ value: 'E03' }, { value: 'Bob' }] }
];
let grid: PdfGrid = new PdfGrid(2, widths, rows, headers);
// Draw the grid within specified bounds with pagination support
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10, width: 240, height: 300 });
console.log('Grid rendered on page:', result.page.pageIndex, 'Occupied bounds:', result.bounds);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
page PdfPage The PDF page on which to draw the grid.
bounds Rectangle The rectangular bounds.
layoutFormat (optional) PdfLayoutFormat Optional layout format.

Returns PdfGridLayoutResult

draw

Draws the grid using the specified graphics context within the specified bounds.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Get the graphics context from the page
let graphics: PdfGraphics = page.graphics;
// Create a small grid
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'ID' }, { value: 'Name' }] }];
let rows: PdfGridRow[] = [{ cells: [{ value: 'E01' }, { value: 'John' }] }];
let grid: PdfGrid = new PdfGrid(2, widths, rows, headers);
// Draw the grid directly on graphics within bounds (no pagination)
grid.draw(graphics, { x: 10, y: 10, width: 240, height: 100 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
graphics PdfGraphics The graphics context.
bounds Rectangle The rectangular bounds.

Returns void

draw

Draws the grid on the specified page starting at the given location.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Create a grid with multiple rows
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'Employee ID' }, { value: 'Employee Name' }] }];
let rows: PdfGridRow[] = [
    { cells: [{ value: 'E01' }, { value: 'John' }] },
    { cells: [{ value: 'E02' }, { value: 'Alice' }] },
    { cells: [{ value: 'E03' }, { value: 'Bob' }] }
];
let grid: PdfGrid = new PdfGrid(2, widths, rows, headers);
// Draw the grid starting at location (10, 10) with automatic pagination
let result: PdfGridLayoutResult = grid.draw(page, { x: 10, y: 10 });
console.log('Grid rendered on page:', result.page.pageIndex, 'Occupied bounds:', result.bounds);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
page PdfPage The PDF page on which to draw the grid.
location Point The (x, y) coordinates (in points) where the grid drawing will start.
layoutFormat (optional) PdfLayoutFormat Optional layout format.

Returns PdfGridLayoutResult

draw

Draws the grid using the specified graphics context starting at the given location.

// Load an existing PDF document
let document: PdfDocument = new PdfDocument(data, password);
// Access the first page
let page: PdfPage = document.getPage(0);
// Get the graphics context from the page
let graphics: PdfGraphics = page.graphics;
// Create a small grid
let widths: Map<number, number> = new Map<number, number>([[0, 80], [1, 120]]);
let headers: PdfGridRow[] = [{ cells: [{ value: 'ID' }, { value: 'Name' }] }];
let rows: PdfGridRow[] = [{ cells: [{ value: 'E01' }, { value: 'John' }] }];
let grid: PdfGrid = new PdfGrid(2, widths, rows, headers);
// Draw the grid directly on graphics starting at location (10, 10) (no pagination)
grid.draw(graphics, { x: 10, y: 10 });
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
graphics PdfGraphics The graphics context (typically from a page) on which to draw the grid.
location Point The (x, y) coordinates (in points) where the grid drawing will start.

Returns void

getHeader

Gets the header row at the specified zero-based index.

Parameter Type Description
index number The zero-based header-row index.

Returns PdfGridRow

getRow

Gets the record row at the specified zero-based index.

Parameter Type Description
index number The zero-based record-row index.

Returns PdfGridRow