我开始使用 jasper 库。如果我尝试编译我的项目,jasper 会生成一个空 PDF。通常,这个 main 函数应该生成一个带有文本“hello”的 pdf 文件。谁能帮我?
主.java
public class Main {
public static void main(String[] args) throws Throwable {
JasperReport jasperReport;
JasperPrint jasperPrint;
Map<String, Object> parameter = new HashMap<String, Object>();
parameter.put("aParameter", "hello");
try {
jasperReport = JasperCompileManager
.compileReport("C:/Users/Sam/Desktop/helloworld.jrxml");
jasperPrint = JasperFillManager.fillReport(jasperReport, parameter);
JasperExportManager.exportReportToPdfFile(jasperPrint,
"C:/Users/Sam/Desktop/helloworld.pdf");
} catch (JRException e) {
e.printStackTrace();
}
}
helloworld.jrxml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="Example1">
<parameter name="aParameter" class="java.lang.String"/>
<detail>
<band height="150">
<textField >
<reportElement x="100" y="60" width="100" height="50" />
<textElement><font size="36"/></textElement>
<textFieldExpression class="java.lang.String"><![CDATA[$P{aParameter}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>