import java.io.*;
import java.util.*;
import com.pdfjet.*;
/**
* Example_08.java
*/
public class Example_08 {
public Example_08() throws Exception {
PDF pdf = new PDF(new FileOutputStream("Example_08.pdf"));
Page page = new Page(pdf, Letter.PORTRAIT);
Font f1 = new Font(pdf, CoreFont.HELVETICA_BOLD);
Font f2 = new Font(pdf, CoreFont.HELVETICA);
Font f3 = new Font(pdf, CoreFont.HELVETICA_BOLD_OBLIQUE);
f1.setSize(7.0f);
f2.setSize(7.0f);
f3.setSize(7.0f);
Table table = new Table();
List<List<Cell>> tableData = getData(
"data/world-communications.txt",
"|", Table.DATA_HAS_2_HEADER_ROWS, f1, f2);
table.setData(tableData, Table.DATA_HAS_2_HEADER_ROWS);
table.setLineWidth(0.2f);
table.setPosition(70.0f, 30.0f);
table.setTextColorInRow(6, Color.blue);
table.setTextColorInRow(39, Color.red);
table.setFontInRow(26, f3);
table.removeLineBetweenRows(0, 1);
table.autoAdjustColumnWidths();
table.setColumnWidth(0, 120.0f);
table.rightAlignNumbers();
int numOfPages = table.getNumberOfPages(page);
while (true) {
Point point = table.drawOn(page);
// TO DO: Draw "Page 1 of N" here
if (!table.hasMoreData()) {
// Allow the table to be drawn again later:
table.resetRenderedPagesCount();
break;
}
page = new Page(pdf, Letter.PORTRAIT);
}
pdf.complete();
}
public List<List<Cell>> getData(
String fileName,
String delimiter,
int numOfHeaderRows,
Font f1,
Font f2) throws Exception {
List<List<Cell>> tableData = new ArrayList<List<Cell>>();
int currentRow = 0;
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = reader.readLine()) != null) {
List<Cell> row = new ArrayList<Cell>();
String[] cols = null;
if (delimiter.equals("|")) {
cols = line.split("\\|", -1);
}
else if (delimiter.equals("\t")) {
cols = line.split("\t", -1);
}
else {
throw new Exception(
"Only pipes and tabs can be used as delimiters");
}
for (int i = 0; i < cols.length; i++) {
String text = cols[i].trim();
if (currentRow < numOfHeaderRows) {
row.add(new Cell(f1, text));
}
else {
row.add(new Cell(f2, text));
}
}
tableData.add(row);
currentRow++;
}
reader.close();
appendMissingCells(tableData, f2);
return tableData;
}
private void appendMissingCells(List<List<Cell>> tableData, Font f2) {
List<Cell> firstRow = tableData.get(0);
int numOfColumns = firstRow.size();
for (int i = 0; i < tableData.size(); i++) {
List<Cell> dataRow = tableData.get(i);
int dataRowColumns = dataRow.size();
if (dataRowColumns < numOfColumns) {
for (int j = 0; j < (numOfColumns - dataRowColumns); j++) {
dataRow.add(new Cell(f2));
}
dataRow.get(dataRowColumns - 1).setColSpan((numOfColumns - dataRowColumns) + 1);
}
}
}
public static void main(String[] args) throws Exception {
new Example_08();
}
} // End of Example_08.java
© 2023 Innovatics Inc.