Set sizes of report bands in jasper report - java

Let me know how to set the height of Detail band programmaticaly in iReport in swing application? That means, when the data filled in Detail band, then footer should be next, and no any white space.In my report when the few data filled in report, there is more white space till the page bottom.I want to remove that white space and set the column footer just after the last data in detail band.Thank you

You can set band via JasperReports API using JRDesignBand class:
public void JRDesignBand.setHeight(int height)
or via DynamicJasper API using AbstractLayoutManager class
protected void AbstractLayoutManager.setBandFinalHeight(net.sf.jasperreports.engine.design.JRDesignBand band)
- Sets the band's height to hold all its children
You can see samples:
with help of JasperReports API
and with help of DynamicJasper API
You can also solve your problem without programming using only iReport. Try to set ignore pagination report property value to true:
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" ... isIgnorePagination="true"
You can also view this link.
JasperReports API allows to set this property using JasperDesign class :
public void JasperDesign.setIgnorePagination(boolean ignorePagination)
- Sets the value of the ignore pagination flag

JasperDesign jasperDesign = JRXmlLoader.load(file);
JRDesignBand band = (JRDesignBand) jasperDesign.getPageFooter();
band.setHeight(SIZE);
You can change getPageFooter to another band...

Related

Print custom invoice of page size (20.5 x 14 cm) using jasper report or iText for Java

I want to print data on invoice receipt size of 20.5 x 14 cm(hard copy). In which I try to put text at some absolute location. I tried iText first. In that I try to set page size by following code.
// here what is unite used in bracket of rectangle?
Document document = new Document(new Rectangle(552,377));
PdfWriter.getInstance(document, new FileOutputStream("report.pdf"));
document.open();
...
...
...
document.close();
Second I tried Jasper report. In that I set page size to 20.5 x 14 cm. But how can I take value from my java application's textfield and put it in to some absolute location in iReport.
As I know jasper report take value from database but how can I take value from java application's textfiled?
I am more familiar with iText.
I will be thankful to any developer who can guide me how can I print custom invoice using iText or Jasper Report.I am developing java application using netbeans.
In iReport -> Report Inspektor -> right click on "report name" -> Page Format
(also you might need to go to Tools -> Options -> iReport -> General -> Units, you can change the default units of measurement... and mm is an option.)
The jrxml output should look smthg like this:
jasperReport
name="networth_p200"
orientation="Landscape"
pageWidth="842"
pageHeight="595"
2. Take a look at these for passing parameters from a Java app:
JasperReports parameters & Passing params to Jasper
The size of a page in PDF is defined by the MediaBox (required). The visible area is defined by the CropBox (optional). The measurement unit in PDF is the user unit. By default 1 user unit coincides with 1 point. In other words: 1 inch = 72 user units.
However, one can define a UserUnit value between 1 and 75,000. You won't find that much PDFs that change the default and as you're creating a PDF from scratch, you are using the default user unit. In your case, a page that measures 552 by 377 user units, is 7.66 by 5.24 inch or 19.47 by 13.30 cm. You need a Rectangle of 581.10 by 396.85 user units:
new Rectangle(581.1f, 396.85f);
I am not at all familiar with JasperReports, so I can only answer the first part of your question regarding the measurement system. Maybe you can post a second question that focuses on the JasperReports part of your question.

Can't use Row Data Resulted from Scripted DataSet

Performing a test with BIRT I was able to create a report and render it in PDF, but unfortunatelly I'm not getting the expected result.
For my DataSource I created a Scripted DataSource and no code was needed in there (as far as I could see the documentation to achieve what I'm trying to do).
For my DataSet I create a Scripted DataSet using my Scripted DataSource as source. In there I defined the script for open like:
importPackage(Packages.org.springframework.context);
importPackage(Packages.org.springframework.web.context.support);
var sc = reportContext.getHttpServletRequest().getSession().getServletContext();
var spring = WebApplicationContextUtils.getWebApplicationContext(sc);
myPojo = spring.getBean("myDao").findById(params["pojoId"]);
And script for fetch like:
if(myPojo != null){
row["title"] = myPojo.getTitle();
myPojo = null;
return true;
}
return false;
As the population of row will be done on runtime, I wasn't able to automatically get the DataSet columns, so I created one with the following configuration: name: columnTitle (as this is the name used to populated row object in fetch code).
Afterwards I edited the layout of my report and added the column to my layout.
I was able to confirm that spring.getBean("myDao").findById(params["pojoId"]); is executed. But my rendered report is not showing the title. If I double click on my column label on report layout I can see there that expression is dataSetRow["columnTitle"] is it right? Even I'm using row in my fetch script? What am I missing here?
Well, what is conctractVersion?
It is obviously not initialized in the open event.
Should this read myPojo.contractVersion or perhaps myPojo.getContractVersion()?
Another point: Is the DS with the column "columnTitle" bound to the layout?
You should also run your report as HTML or in the previewer to check for script errors.
Unfortunately, these are silently ignored when generating the report as PDF...
The problem was the use of batik twice (two different versions), one dependency for BIRT and other for DOCX4J.
The issue is quite difficult to identify because there is no log output rendering PDF files.
Rendering HTML I could see an error message which I could investigate and find the problem.
For my case I could remove the DocX4j from maven POM.

How to set Record Selection Formula on Crystal Reports java

How do you create/set Record Selection Formula programatically on crystal reports using java? I tried searching on the internet but the only option is through IFilter which requires a Crystal Report Server. My program only uses the JRC library. Also this is a java desktop application using swing.
It may be a bit late, but maybe this is useful for someone:
reportClientDoc.getDataDefController().getRecordFilterController().setFormulaText("your record selection formula here");
I was doing some research about this and noticed that there are 3 methods with which you can do this:
Using the IFilter interface as shown in this example provided by SAP
// Set the filter string to be used as the Record Filter
String freeEditingFilter = "{Customer.Country} = 'Canada'";
// Retrieve the record filter for the Data Definition Controller
IFilter iFilter = clientDoc.getDataDefController().getDataDefinition().getRecordFilter();
// Set the filter to free editing text filter string
iFilter.setFreeEditingText(freeEditingFilter);
// Modify the filter through the Record Filter Controller to the report
clientDoc.getDataDefController().getRecordFilterController().modify(iFilter);
I am using the JRC only without a Crystal Report Server and the above example worked for me.
As Francisco said in his answer, using the setFormulaText method:
clientDoc.getDataDefController().getRecordFilterController().setFormulaText("{Customer.Country} = 'Canada'");
Using parameters. Parameters can be passed to the report using code (you can use the addDiscreteParameterValue function in the helper class) or else they can be filled in by the user during runtime. I chose not to opt for this option because they can not be set to optional
If you want to create a crystal report of your program, you need another jar file of software.
You can create your program in NetBeans IDE and link your IDE with IReport software which is used in NetBeans for creating Reporting in java.
You get many example from internet about this.

Jasper Reports Exclude Column Headers in a Table?

I am using Jasper reports for a project that needs both PDF and CSV output and the majority of the data is the Detail section, within a table. I know you can remove the pageHeader and columnHeader at the document level, but is it possible to remove, or only print once, the column headers within a table? If not the CSV outputs,
User Type,Time,Username,Event,IP Address,Student Name,Student Number
Admin,6/6/11 8:09 PM,admin,Uploaded a report file.,0:0:0:0:0:0:0:1,,
....[about 20 more lines of CSV then]....
User Type,Time,Username,Event,IP Address,Student Name,Student Number
This just looks very unprofessional and isn't very functional. Like I said I know the page level headers can be removed with:
jasperPrint.getPropertiesMap().setProperty("net.sf.jasperreports.export.exclude.origin.band.1", "pageHeader");
jasperPrint.getPropertiesMap().setProperty("net.sf.jasperreports.export.exclude.origin.band.2", "pageFooter");
jasperPrint.getPropertiesMap().setProperty("net.sf.jasperreports.export.csv.exclude.origin.band.1", "columnHeader");
jasperPrint.getPropertiesMap().setProperty("net.sf.jasperreports.export.csv.exclude.origin.band.2", "pageFooter");
jasperPrint.getPropertiesMap().setProperty("net.sf.jasperreports.export.csv.exclude.origin.keep.first.band.1", "columnHeader");
but I am looking for a solution to remove them on table for CSV output only, not PDF. Is this possible?
Any help would be greatly appreciated!
Thanks,
Chuck
Some useful properties to control report export for different formats.
net.sf.jasperreports.export.xls.exclude.origin.band.1=title
net.sf.jasperreports.export.xls.exclude.origin.band.2=summary
net.sf.jasperreports.export.xls.exclude.origin.band.3=pageHeader
net.sf.jasperreports.export.xls.exclude.origin.band.4=pageFooter
net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.1=columnHeader
net.sf.jasperreports.export.xls.collapse.row.span=false
net.sf.jasperreports.export.xls.remove.empty.space.between.columns=true
net.sf.jasperreports.export.csv.exclude.origin.band.csvSummary=summary
net.sf.jasperreports.export.csv.exclude.origin.band.1=title
net.sf.jasperreports.export.csv.exclude.origin.band.2=pageFooter
net.sf.jasperreports.export.csv.exclude.origin.keep.first.band.1=columnHeader
net.sf.jasperreports.export.xls.exclude.origin.band.1=title
net.sf.jasperreports.export.xls.exclude.origin.band.2=summary
net.sf.jasperreports.export.xls.exclude.origin.band.3=pageHeader
net.sf.jasperreports.export.xls.exclude.origin.band.4=pageFooter
net.sf.jasperreports.export.xls.exclude.origin.keep.first.band.1=columnHeader
net.sf.jasperreports.export.xls.collapse.row.span=false
net.sf.jasperreports.export.xls.remove.empty.space.between.columns=true
net.sf.jasperreports.export.html.using.images.to.align=false
net.sf.jasperreports.export.html.remove.emtpy.space.between.rows=true
net.sf.jasperreports.export.ignore.page.margins=true
Full reference.
Column headers in the table component are meant to be repeated when the table overflows and cannot be hidden. To achieve what you want you could either:
move the contents of your columnHeader into the tableHeader so that only the table header prints once
or filter out the elements when performing a specific export by adding sets of properties like these:
<property name="net.sf.jasperreports.export.pdf.exclude.origin.keep.first.band.1" value="columnHeader"/>
<property name="net.sf.jasperreports.export.pdf.exclude.origin.keep.first.report.1" value="*"/>
More info on filtering elements at export time here and here.
Maybe you should use different report definitions for each output. If not, then you could just recognise when you're printing to csv and only set those properties then.

Blank subreport in Jasper Reports

I'm trying to launch a report that I created in iReport which contains a main report and a subreport. In iReport, the subreport launches fine and contains data, however when I try to launch it from within my java code, the subreport is blank.
What I've done so far:
I have a String parameter in the main report called "SUBREPORT" that is used to define the path to the subreport. E.g. value: "E:\java\ReportLauncher\reports\test_subreport1.jasper"
The subreport expression in the main report is set to: $P{SUBREPORT}
The subreport connection expression is: $P{REPORT_CONNECTION}
I also have a subreport parameter defined called "INVOICE_NUMBER" that is set to $F{InviInvNo}, which maps to a field in the main report.
In my java code, I have:
HashMap<String, Object> paramHash = new HashMap();
paramHash.put("INVOICE_NUMBER", invoiceID);
paramHash.put("REPORT_CONNECTION", this.conn);
paramHash.put("SUBREPORT", subReportPath);
JasperPrint jasperprint = JasperFillManager.fillReport(this.reportPath, paramHash, this.conn);
The main report is created fine and is populated. The subreport area however is blank. Any thoughts for what I'm doing wrong?
Thanks in advance,
Andy.
Since you didn't mention anything about the subreport query:
If you are using the Detail Band in the subreport, then you will need a Data Query for the report.
In old iReport, from Data menu, choose Report Query and write your query here. If you want only one record (i.e. Detail band only one time) you can use a dummy table. Like:
SELECT 'a' FROM DUMMY
Otherwise, if you don't want to use the Detail Band, you have the option to view the other bands without using a query. From Edit menu, choose Report Properties, under More... tab, set the flag When no data to All Sections, no detail

Categories

Resources