【问题标题】:Primefaces DataExporter Set Table Columns WidthPrimefaces DataExporter 设置表格列宽
【发布时间】:2015-09-23 15:05:42
【问题描述】:

我有一个 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);
    } 

有什么办法吗?

【问题讨论】:

    标签: primefaces pdf-generation primefaces-dataexporter


    【解决方案1】:

    最近我继承了许多已经存在的 exporters 并设置了 options 标签的项目,因此我需要找到能够保持所有标签完好无损的解决方案。

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

    我的解决方案通过 p:dataExporteroptions 标签设置列宽,因此无需进行预处理/后处理。我使用 Primefaces 4.x 及更高版本对其进行了测试。

    分步程序:

    1. 创建新包 org.primefaces.component.export 在您的项目中。

    2. 从 Primefaces git 存储库完全复制以下类 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
      

    从其他 export 方法中删除代码,但保留声明。

    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}"/>
      

    此解决方案使用PF showcase 产生以下结果

    扩展此解决方案的建议:

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

    public int[] getColumnWidths();
    

    设置绝对列宽 或者您可以根据项目要求设置任何其他选项。

    【讨论】:

      猜你喜欢
      • 2012-06-23
      • 2010-10-30
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多