PDF exporter using IText. This class is provided more as an example than as a "production ready" class: users
probably will need to write a custom export class with a specific layout.
| Method from org.displaytag.export.PdfView Detail: |
public void doExport(OutputStream out) throws JspException {
try
{
// Initialize the table with the appropriate number of columns
initTable();
// Initialize the Document and register it with PdfWriter listener and the OutputStream
Document document = new Document(PageSize.A4.rotate(), 60, 60, 40, 40);
document.addCreationDate();
HeaderFooter footer = new HeaderFooter(new Phrase(TagConstants.EMPTY_STRING, smallFont), true);
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_CENTER);
PdfWriter.getInstance(document, out);
// Fill the virtual PDF table with the necessary data
generatePDFTable();
document.open();
document.setFooter(footer);
document.add(this.tablePDF);
document.close();
}
catch (Exception e)
{
throw new PdfGenerationException(e);
}
}
|
protected void generateHeaders() throws BadElementException {
Iterator iterator = this.model.getHeaderCellList().iterator();
while (iterator.hasNext())
{
HeaderCell headerCell = (HeaderCell) iterator.next();
String columnHeader = headerCell.getTitle();
if (columnHeader == null)
{
columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName());
}
Cell hdrCell = getCell(columnHeader);
hdrCell.setGrayFill(0.9f);
hdrCell.setHeader(true);
tablePDF.addCell(hdrCell);
}
}
Generates the header cells, which persist on every page of the PDF document. |
protected void generatePDFTable() throws JspException, BadElementException {
if (this.header)
{
generateHeaders();
}
tablePDF.endHeaders();
generateRows();
}
The overall PDF table generator. |
protected void generateRows() throws JspException, BadElementException {
// get the correct iterator (full or partial list according to the exportFull field)
RowIterator rowIterator = this.model.getRowIterator(this.exportFull);
// iterator on rows
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
// iterator on columns
ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList());
while (columnIterator.hasNext())
{
Column column = columnIterator.nextColumn();
// Get the value to be displayed for the column
Object value = column.getValue(this.decorated);
Cell cell = getCell(ObjectUtils.toString(value));
tablePDF.addCell(cell);
}
}
}
Generates all the row cells. |
public String getMimeType() {
return "application/pdf"; //$NON-NLS-1$
}
|
protected void initTable() throws BadElementException {
tablePDF = new Table(this.model.getNumberOfColumns());
tablePDF.setDefaultVerticalAlignment(Element.ALIGN_TOP);
tablePDF.setCellsFitPage(true);
tablePDF.setWidth(100);
tablePDF.setPadding(2);
tablePDF.setSpacing(0);
smallFont = FontFactory.getFont(FontFactory.HELVETICA, 7, Font.NORMAL, new Color(0, 0, 0));
}
Initialize the main info holder table. |
public void setParameters(TableModel tableModel,
boolean exportFullList,
boolean includeHeader,
boolean decorateValues) {
this.model = tableModel;
this.exportFull = exportFullList;
this.header = includeHeader;
this.decorated = decorateValues;
}
|