2

我正在尝试在碧玉报告中实现类似于此处(如链接中)的批量导出。请指点。

我有: 1. 纵向模板 2. 横向模板 3. 我需要生成报告的顺序 4. 相同的 Bean 数据源,每个模板都有过滤查询

我需要什么: 分别生成报告并合并所有生成的 JasperPrint 对象并调整页码

4

3 回答 3

2

由于您正在生成报告然后合并它,因此当前页码和总页数仅在导出期间可用,具体取决于您合并的报告数量。我们使用通用元素解决了类似的问题。您需要编写自定义逻辑来处理您创建的处理程序中的页码。

于 2012-09-04T06:33:07.893 回答
0

我的报告第 1 页 (jr1) 是纵向的,第 2 页 (jr2) 是横向的。

JasperReport jr1 = (JasperReport) JRLoader.loadObject(...);
JasperReport jr2 = (JasperReport) JRLoader.loadObject(...);

JREmptyDataSource ds = new JREmptyDataSource(1);
JasperPrint print1 = JasperFillManager.fillReport(jr1, new HashMap(), ds);

ds.moveFirst();

JasperPrint print2 = JasperFillManager.fillReport(jr2, new HashMap(), ds);
List l = new ArrayList();

l.add(print1);
l.add(print2);

JRPdfExporter exp = new JRPdfExporter();
exp.setParameter(JRExporterParameter.JASPER_PRINT_LIST, l);
exp.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, SAVE_LOCATION);
exp.exportReport();
于 2012-09-03T08:34:48.960 回答
0

这可能适用于页码问题:

private static void mergeAndNumber() 抛出 IOException,DocumentException {

    PdfReader readerOne = new PdfReader("c:\file_one.pdf"«»);



    // we retrieve the total number of pages

    int nOne = readerOne.getNumberOfPages();



    PdfReader readerTwo = new PdfReader("c:\file_two.pdf"«»);



    // we retrieve the total number of pages

    int nTwo = readerTwo.getNumberOfPages();



    int totalPages = (nOne + nTwo);

    System.out.println("Total number of pages: " + (nOne + nTwo));



    Rectangle psize = readerOne.getPageSize(1);

    float widthPort = psize.width();

    float heightPort = psize.height();



    // step 1: creation of a document-object

    Document document = new Document(psize, 50, 50, 50, 50);

    // step 2: we create a writer that listens to the document

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\file_combined.pdf"«»));



    document.open();

    // step 4: we add content

    PdfContentByte cb = writer.getDirectContent();



    PdfImportedPage importPage = null;



    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

    float pageNumXPos = widthPort;

    float pageNumYPos = heightPort;

    for (int i = 1; i <= nOne; i++) {

        document.newPage();

        importPage = writer.getImportedPage(readerOne, i);

        cb.addTemplate(importPage, 0,0);



        cb.moveTo(400, 40);         

        cb.beginText();

        cb.setFontAndSize(bf, 10);

        //cb.showText("Page " + i  + " of " + totalPages);          

        cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "Page " + i  + " of " + totalPages, pageNumXPos - 45, pageNumYPos - 55, 0);

        cb.endText();

    }



    document.setPageSize(PageSize.A4.rotate());



    for (int i = 1; i <= nTwo; i++) {

        document.newPage();

        importPage = writer.getImportedPage(readerTwo, i);

        cb.addTemplate(importPage, 0,0);



        cb.moveTo(400, 40);        

        cb.beginText();

        cb.setFontAndSize(bf, 10);

        cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "Page " + (nOne + i)  + " of " + totalPages, pageNumYPos - 20, pageNumXPos - 60, 0);         

        cb.endText();

    }



    document.close();        

}
于 2012-09-03T09:51:10.557 回答