Hi,
I am creating a pdf containing a table. I need to size the pdf such
that:
- the pdf uses a single page only, regardless of the height of the
table
- no extraneous white space appears at the bottom
I want the pdf to contain the table, the whole table, and nothing but
the table, on *a single page*. (I'm actually creating an image of the
resulting pdf, but that's likely irrelevant in this context.)
It usually works. However, when there is *line-wrapping* of wide text in
a cell(s), then then it sometimes succeeds, and sometimes fails. It
seems to fail after the amount of wrapping exceeds some unknown amount.
It fails in the sense that the pdf is spread over 2 pages, not 1.
The pertinent sections of my Shakespearean-yet-oh-so-faulty code look
like this:
private byte[] buildPdfBytesAndPdfTable() {
ByteArrayOutputStream result = null;
try {
result = new ByteArrayOutputStream();
Document document = new Document(PageSize.LETTER); //this size
gets overwritten below
PdfWriter writer = PdfWriter.getInstance(document, result); //
NEEDED! has a side effect
document.addTitle("Preview Table");
document.addSubject("Preview Table");
fPdfPTable = buildTableContent(fTableConfig); //has side effect
regarding height/width - see below
document.setPageSize(computePageSizeFrom(fPdfPTable));
document.open();
document.add(fPdfPTable);
document.close();
}
catch (DocumentException ex) {
throw new RuntimeException("Unable to create PDF", ex);
}
return result.toByteArray();
}
private PdfPTable buildTableContent(TableConfig/*This holds my table
data*/ aConfig) {
float[] RELATIVE_WIDTHS = getRelativeWidths(aConfig);
PdfPTable result = new PdfPTable(RELATIVE_WIDTHS);
//elided...
result.setTotalWidth(PageSize.LETTER.width());
result.calculateHeightsFast();
return result;
}
private Rectangle computePageSizeFrom(PdfPTable aTable) {
float width = PageSize.LETTER.width();
float height = aTable.getTotalHeight() + 72.0f;
return new Rectangle(width, height);
}
Do you know where I'm going wrong? I've whacked away at it in the
obvious ways, but the problem persists...
Any help appreciated,
- John
------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
The must-attend event for mobile developers. Connect with experts.
Get tools for creating Super Apps. See the latest technologies.
Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php