我有许多PdfPTable
s跟随彼此:
是否可以在所有页面(不仅仅是第一页)上添加标题?我相信我不能使用PdfPageEventHelper
因为每页首页的表格需要向下移动(不与标题重叠)。
任何想法或提示如何解决这个问题?我无法弄清楚如何知道每个表是否有新页面。
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;
}
}