问题
Backgroud: Use Java + BIRT to generate report. Generate report in viewer and allow user to choose to export it to different format (pdf, xls, word...).
All program are in "Layout", no program in "Master Page". Have 1 "Data Set". The fields in "Layout" refer to this DS. There is Group in "Layout", gropu by one field. In "Group Header", I create one cell to use as page number. "Page : MyPageNumber". "MyPageNumber" is a field I define which would +1 in Group Header.
Problem: When I use 1st method to generate report, "MyPageNumber" could not show correctly. Because group header only load one time for each group. It would always show 1.
Question: As I know there is "restart page number in group" in Crystal report. How to restart page in BIRT? I want to show data of different group in 1 report file, and the page number start from 1 for each group.
回答1:
You can do it with BIRT reports using page variables. For example:
- Add 2 page variables... Group_page, Group_name.
- Add 1 report variable... Group_total_page.
In the report beforeFactory add the script:
prevGroupKey = ""; groupPageNumber = 1; reportContext.setGlobalVariable("gGROUP_NAME", ""); reportContext.setGlobalVariable("gGROUP_PAGE", 1);
In the report onPageEnd add the script:
var groupKey = currGroup; var prevGroupKey = reportContext.getGlobalVariable("gGROUP_NAME"); var groupPageNumber = reportContext.getGlobalVariable("gGROUP_PAGE"); if( prevGroupKey == null ){ prevGroupKey = ""; } if (prevGroupKey == groupKey) { if (groupPageNumber != null) { groupPageNumber = parseInt(groupPageNumber) + 1; } else { groupPageNumber = 1; } } else { groupPageNumber = 1; prevGroupKey = groupKey; } reportContext.setPageVariable("GROUP_NAME", groupKey); reportContext.setPageVariable("GROUP_PAGE", groupPageNumber); reportContext.setGlobalVariable("gGROUP_NAME", groupKey); reportContext.setGlobalVariable("gGROUP_PAGE", groupPageNumber); var groupTotalPage = reportContext.getPageVariable("GROUP_TOTAL_PAGE"); if (groupTotalPage == null) { groupTotalPage = new java.util.HashMap(); reportContext.setPageVariable("GROUP_TOTAL_PAGE", groupTotalPage); } groupTotalPage.put(groupKey, groupPageNumber);
In a master page onRender script add the following script:
var totalPage = reportContext.getPageVariable("GROUP_TOTAL_PAGE"); var groupName = reportContext.getPageVariable("GROUP_NAME"); if (totalPage != null) { this.text = java.lang.Integer.toString(totalPage.get(groupName)); }
In the table group header onCreate event, add the following script, replacing 'COUNTRY' with the name of the column that you are grouping on:
currGroup = this.getRowData().getColumnValue("COUNTRY");
- In the master page add a grid to the header or footer and add an autotext variable for Group_page and Group_total_page. Optionally add the page variable for the Group_name as well.
Check out these links for more information about BIRT page variables:
- https://books.google.ch/books?id=aIjZ4FYJOQkC&pg=PA85&lpg=PA85&dq=birt+change+autotext&source=bl&ots=K0nCmF2hrD&sig=CBOr_otRW0B72sZoFS7LC_1Mrz4&hl=en&sa=X&ei=ZKNAVcnuLYLHsAXRmIHoCw&ved=0CEoQ6AEwBQ#v=onepage&q=birt%20change%20autotext&f=false
- https://www.youtube.com/watch?v=lw_k1qHY_gU
- http://www.eclipse.org/birt/phoenix/project/notable2.5.php#jump_4
- https://bugs.eclipse.org/bugs/show_bug.cgi?id=316173
- http://www.eclipse.org/forums/index.php/t/575172/
回答2:
Alas, this is not supported with BIRT.
That's probably not the answer you've hoped for, but it's the truth. This is one of the very few aspects where BIRT is way behind other report generator tools.
However, depending on how you have BIRT integrated into your environment, a workaround approach is possible for PDF export that we use in our solution with great success. The idea is to let BIRT generate a PDF outline based on the grouping. And the BIRT report creates information in the ReportContext about where and how it wants the page numbers to be displayed. After BIRT generated the PDF, a custom PDFPostProcessor uses the PDF outline and the information from the ReportContext to add the page numbers with iText. If this work-around is viable for you, feel free to contact me.
来源:https://stackoverflow.com/questions/29909479/how-to-restart-page-number-from-1-in-different-group-of-birt-report