I have a number of PdfPTable
s that is following each other like this:
Is it possible to add a title on all of the page(not just the first page)? I believe I cant use the PdfPageEventHelper
because the table that is first on each page needs to be moved down(to not overlap the title).
Any ideas or tips how this can be solved? I cant figure out how to know if there has been a new page or not for each of the tables.
public class TableDemo {
/** The resulting PDF file. */
public static final String RESULT = "first_table.pdf";
/**
* Main method.
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args)
throws IOException, DocumentException {
new TableDemo().createPdf(RESULT);
}
/**
* Creates a PDF with tables
* @param filename the name of the PDF file that will be created.
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename)
throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.open();
document.add(addTitle());
for(int i=0; i<10; i++)
document.add(createTable());
document.close();
}
/**
* Creates our table
* @return our table
*/
public static PdfPTable createTable() {
PdfPTable table = new PdfPTable(4);
int rows = (int)(Math.random()*30+1);
for(int row=0; row<rows; row++){
for(int cell=0; cell<4; cell++){
table.addCell("row "+row+"; cell "+cell);
}
}
table.setKeepTogether(true);
table.setSpacingAfter(30);
return table;
}
public static Paragraph addTitle(){
Font fontbold = FontFactory.getFont("Times-Roman", 40, Font.BOLD);
Paragraph p = new Paragraph("TITLE ON EACH PAGE", fontbold);
p.setSpacingAfter(20);
p.setAlignment(1); // Center
return p;
}
}