我使用Jasper创建了两个PDF报告,并将它们合并为一个PDF文件。第一个PDF报告包含Anchor链接,第二个PDF报告包含Anchor目标。合并两个PDF后,锚点不起作用。我该如何解决这个问题?
part1.jrxml代码
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 5.6.1.final using JasperReports Library version 5.6.1 -->
<!-- 2016-01-03T23:14:55 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="part1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8d0dcb70-0391-45e2-9263-4ac8cfa5f0de">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="405" splitType="Stretch">
<textField>
<reportElement x="320" y="370" width="100" height="30" uuid="0f29cf02-60d5-43d7-b360-3254134a6f77"/>
<textFieldExpression><![CDATA["Anchor Name"]]></textFieldExpression>
<anchorNameExpression><![CDATA["expert"]]></anchorNameExpression>
</textField>
</band>
</summary>
</jasperReport>
part2.jrxml代码
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 5.6.1.final using JasperReports Library version 5.6.1 -->
<!-- 2016-01-03T23:24:42 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="part2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3f401f6e-4962-4a54-9674-4b0c613f9e73">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<queryString>
<![CDATA[]]>
</queryString>
<background>
<band splitType="Stretch"/>
</background>
<summary>
<band height="389" splitType="Stretch">
<textField hyperlinkType="LocalAnchor">
<reportElement x="430" y="350" width="100" height="30" uuid="53f9f5fa-df7a-4fa3-bc1d-57b8be0c130a"/>
<textFieldExpression><![CDATA["Anchor Target"]]></textFieldExpression>
<hyperlinkReferenceExpression><![CDATA["./result.pdf"]]></hyperlinkReferenceExpression>
<hyperlinkAnchorExpression><![CDATA["expert"]]></hyperlinkAnchorExpression>
</textField>
</band>
</summary>
</jasperReport>
Merged.pdf
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfSmartCopy;
public class MergePdf {
public static void main(String[] args) throws IOException, DocumentException {
String RESULT= "/home/expert/Desktop/result.pdf";
String[] files = {"/home/expert/Desktop/part1.pdf", "/home/expert/Desktop/part2.pdf"};
mergePDF(files,RESULT);
}
public static boolean mergePDF(String sourcefiles[],String targetfile){
Document document = new Document();
PdfCopy copy;
try {
//copy = new PdfCopy(document, new FileOutputStream(targetfile));
copy = new PdfSmartCopy(document, new FileOutputStream(targetfile));
document.open();
PdfReader reader;
int n;
for (int i = 0; i < sourcefiles.length; i++) {
reader = new PdfReader(sourcefiles[i]);
n = reader.getNumberOfPages();
for (int page = 0; page < n; )
copy.addPage(copy.getImportedPage(reader, ++page));
copy.freeReader(reader);
reader.close();
}
document.close();
System.out.println("INFO :: Merging Complete of temp files");
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}
答案 0 :(得分:0)
考虑合并JasperPrint
然后导出为pdf,使用SimpleExporterInput.getInstance(List<JasperPrint>)
作为导出器输入
这将使书签和锚点正常工作
实施例
Map<String, Object> paramMap = new HashMap<String, Object>();
JasperReport report1 = JasperCompileManager.compileReport("part1.jrxml");
JasperReport report2 = JasperCompileManager.compileReport("part2.jrxml");
//Get all the JasperPrint and add them to my list
List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
JasperPrint jasperPrint1 = JasperFillManager.fillReport(report1, paramMap);
jasperPrintList.add(jasperPrint1);
JasperPrint jasperPrint2 = JasperFillManager.fillReport(report2, paramMap);
jasperPrintList.add(jasperPrint2);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList)); //Set as export input my list with JasperPrint s
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("pdf/BookmarkTest.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setCreatingBatchModeBookmarks(true);
exporter.setConfiguration(configuration);
exporter.exportReport();