Primefaces DataExporter设置表格列宽度

ihe*_*heb 6 pdf-generation data-export primefaces

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

我添加了一个preorcessor

<p:dataExporter type="pdf" target="tbl" fileName="cars" preProcessor="#{customizedDocumentsView.preProcessPDF}" />
Run Code Online (Sandbox Code Playgroud)

这是我的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);
    } 
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

Dus*_*vic 4

最近,我继承了许多现有出口商的项目,这些出口商已经设置了选项标签,因此我需要找到能够保持所有出口完好无损的解决方案。

我从这个接受的答案中得到了一个想法。

我的解决方案通过p:dataExporter选项标签设置列宽,因此不需要预/后处理。我使用 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并完全遵循导出方法

    public void export(FacesContext context, DataTable table, String filename, boolean pageOnly, boolean selectionOnly, String encodingType,MethodExpression preProcessor, MethodExpression postProcessor, ExporterOptions options) throws IOException
    
    Run Code Online (Sandbox Code Playgroud)

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

  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);
        }
    
        //....
    
    Run Code Online (Sandbox Code Playgroud)

    }

  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);
    ...
    
    Run Code Online (Sandbox Code Playgroud)
  3. 最后你的p:dataExporter组件应该是这样的

    <p:dataExporter type="pdf" target="tbl" fileName="cars" options="#{customizedDocumentsView.pdfOpt}"/>
    
    Run Code Online (Sandbox Code Playgroud)

该解决方案使用PF 展示柜产生以下结果 在此输入图像描述

扩展此解决方案的建议:

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

public int[] getColumnWidths();
Run Code Online (Sandbox Code Playgroud)

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