Primefaces DataExporter设置表格列宽度

时间:2015-09-23 15:05:43

标签: primefaces pdf-generation data-export

我有一个Primefaces DataGrid,我导出了Primefaces DataExporter,但我无法弄清楚如何调整列的大小。

我添加了一个预先准备者

<p:dataExporter type="pdf" target="tbl" fileName="cars" preProcessor="#{customizedDocumentsView.preProcessPDF}" />

这是我的bean中的代码

public void preProcessPDF(Object document) {
        Document pdf = (Document) document;
        pdf.open();
        pdf.setPageSize(PageSize.A4);

        //I need to do something like that
        //PdfPTable table = new PdfPTable(4);
        //float[] columnWidths = new float[] {10f, 20f, 30f, 10f};
        //table.setWidths(columnWidths);
    } 

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

最近,我继承了许多已存在的导出器的项目,并且已经设置了选项标签,因此我需要找到能够保持所有 标记完整的解决方案。

我从这个被接受的answer获得了一个想法。

我的解决方案通过 p:dataExporter options 标记设置列宽,因此无需进行前/后处理。我使用Primefaces ver 4.x及以上测试了它。

一步一步的程序:

  1. 创建新包 org.primefaces.component.export 你的项目内部。

  2. 从Primefaces git repository完全复制以下类 ExporterOptionsPDFOptionsExportedfactoryPDFExporter 进入新创建的包。

  3. ExporterOptions 中添加 public float[] getColumnWidths();

  4. PDFOptions 中添加 float[] columnWidths; 并添加getter和setter。

  5. ExporterFactory 修改行中 exporter = new PdfExporter();exporter = new CustomPdfExporter();

  6. PDFExporter 类重命名为 CustomPDFExporter 并完全遵循 export 方法

    public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, String encodingType,MethodExpression preProcessor, MethodExpression postProcessor, ExporterOptions options) throws IOException
    
  7. 从其他导出方法中删除代码,但保留声明。

    1. CustomPDFExporter 内部 ExportPdfTable 方法中添加2行

      protected PdfPTable exportPDFTable(FacesContext context, DataTable table, boolean pageOnly, boolean selectionOnly, String encoding) throws DocumentException {
         int columnsCount = getColumnsCount(table);
         PdfPTable pdfTable = new PdfPTable(columnsCount);
         this.cellFont = FontFactory.getFont(FontFactory.TIMES, encoding);
         this.facetFont = FontFactory.getFont(FontFactory.TIMES, encoding, Font.DEFAULTSIZE, Font.BOLD);
      
          if (this.expOptions != null) {
              applyFacetOptions(this.expOptions);
              applyCellOptions(this.expOptions);
              //line 1
              //sets columns column relative widths to iText PdfTable object
              pdfTable.setWidths(this.expOptions.getColumnWidths());
              //line 2
              //decreases page margins comparing to original 'Primefaces' layout
              pdfTable.setWidthPercentage(100);
          }
      
          //....
      

      }

    2. 现在您已准备好进入托管bean并添加pdf选项。例如

      pdfOpt = new PDFOptions(); //add getter and setter too
      pdfOpt.setFacetBgColor("#F88017");
      ...
      //if, for example, your PDF table has 4 columns
      //1st column will occupy 10% of table's horizontal width,...3rd - 20%, 4th - 60% 
      float[] columnWidths = new float[]{0.1f, 0.1f, 0.2f, 0.6f};
      pdfOpt.setColumnWidths(columnWidths);
      ...
      
    3. 最后,您的p:dataExporter组件应如下所示

      <p:dataExporter type="pdf" target="tbl" fileName="cars" options="#{customizedDocumentsView.pdfOpt}"/>
      
    4. 使用PF showcase的此解决方案产生以下结果 enter image description here

      建议扩展此解决方案:

      Primefaces导出器使用iText ver 2.1.7。旧的但仍然强大的API。 例如,在步骤1中的 ExporterOptions 中,您可以添加

      public int[] getColumnWidths();
      

      设置绝对列宽 或者您可以设置由项目要求驱动的任何其他选项。

相关问题