PDFTableView

Align data as Table / Data Grid

Show you data as a table/data grid.

Each table consist of header(which is practically a PDFTableRowView) and another compulsory first row.

You can add new row by calling tableView.addRow(tableRowView);

Set Custom column width: You can set custom column width to entire table, you can also set custom width to individual row. *whichever is set last will take preference.

// As this table has 4 columns, each will get width as per their index in array.
// Sum should be equal to 100%
int[] widthPercent = {20, 20, 20, 40};
tableView.setColumnWidth(widthPercent);

Example:

String[] textInTable = {"1", "2", "3", "4"};

// Create table column headers
PDFTableView.PDFTableRowView tableHeader = new PDFTableView.PDFTableRowView(getApplicationContext());
for (String s : textInTable) {
    PDFTextView pdfTextView = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.P);
    pdfTextView.setText("Header Title: " + s);
    tableHeader.addToRow(pdfTextView);
}
// Create first row
PDFTableView.PDFTableRowView tableRowView1 = new PDFTableView.PDFTableRowView(getApplicationContext());
for (String s : textInTable) {
    PDFTextView pdfTextView = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.P);
    pdfTextView.setText("Row 1 : " + s);
    tableRowView1.addToRow(pdfTextView);
}

// PDFTableView takes table header and first row at once because if page ends after adding header then first row will be on next page. To avoid confusion to user, table header and first row is printed together.
PDFTableView tableView = new PDFTableView(getApplicationContext(), tableHeader, tableRowView1);
for (int i = 0; i < 10; i++) {
    // Create 10 rows and add to table.
    PDFTableView.PDFTableRowView tableRowView = new PDFTableView.PDFTableRowView(getApplicationContext());
    for (String s : textInTable) {
	PDFTextView pdfTextView = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.P);
	pdfTextView.setText("Row " + (i + 1) + ": " + s);
	tableRowView.addToRow(pdfTextView);
    }
    tableView.addRow(tableRowView);
}

// As this table has 4 columns, each will get width as per their index in array.
// Sum should be equal to 100%
int[] widthPercent = {20, 20, 20, 40};
tableView.setColumnWidth(widthPercent);

Last updated