2013-05-15 1 views
1

Я использую dataexporter для создания pdf таблицы данных. В моей таблице данных заголовок столбцов централизован, однако версия pdf тех же столбцов выравнивается слева. как я могу сделать столбцы PDF централизованы, как таблица данных.Dataexporter позиция заголовка pdf

ответ

2

Я использую решение для настройки PDFExporter, она работает очень хорошо, спасибо за внимание. Ниже, как я сделал:

Мой пользовательский класс:

public class CustomPDFExporter extends PDFExporter { 

@Override 
protected void addColumnFacets(DataTable table, PdfPTable pdfTable, ColumnType columnType) { 
    for(UIColumn col : table.getColumns()) { 
     if(!col.isRendered()) { 
      continue; 
     } 

     if(col instanceof DynamicColumn) { 
      ((DynamicColumn) col).applyModel(); 
     } 

     if(col.isExportable()) { 
      addHeaderValue(pdfTable, col.getFacet(columnType.facet()), FontFactory.getFont(FontFactory.TIMES, "iso-8859-1", Font.DEFAULTSIZE, Font.BOLD)); 
     } 
    } 
} 

protected void addHeaderValue(PdfPTable pdfTable, UIComponent component, Font font) { 
    String value = component == null ? "" : exportValue(FacesContext.getCurrentInstance(), component); 

    PdfPCell cell = new PdfPCell(new Paragraph(value, font)); 
    cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
    pdfTable.addCell(cell); 
} 

}

боб:

public void exportPDF(DataTable table, String filename) throws IOException { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    Exporter exporter = new CustomPDFExporter(); 
    exporter.export(context, table, filename, false, false, "iso-8859-1", null, null); 
    context.responseComplete(); 
} 

В моей странице я добавил:

<h:commandLink action="#{boxBean.exportPDF(boxTable, 'relatorio_caixas')}" > 
    <p:graphicImage value="/resources/img/pdf.png"/> 
</h:commandLink> 
Смежные вопросы