Jasper reports 如何在JasperReports中将两个pdf文档合并到一个报表中?

Jasper reports 如何在JasperReports中将两个pdf文档合并到一个报表中?,jasper-reports,report,Jasper Reports,Report,我不熟悉JasperReports。我可以用Javabean数据源创建一个简单的PDF文档。在我的项目中,我使用单独的javabean数据源创建了两个单独的pdf文档。现在我想把两个文档合并成一个文档。有谁能告诉我如何使用JasperReports将两个文档合并到单个文档中 您可以为此使用子报表。您不必重新创建当前报告。 创建边距为0的主报告。将所有报告作为子报告添加到此中,并设置一个条件,即如果数据源对此可用,则只打印此报告。 现在,将所有单独的数据源放在一个地图数据源中,并将此数据源传递给主

我不熟悉JasperReports。我可以用Javabean数据源创建一个简单的PDF文档。在我的项目中,我使用单独的javabean数据源创建了两个单独的pdf文档。现在我想把两个文档合并成一个文档。有谁能告诉我如何使用JasperReports将两个文档合并到单个文档中

您可以为此使用子报表。您不必重新创建当前报告。 创建边距为0的主报告。将所有报告作为子报告添加到此中,并设置一个条件,即如果数据源对此可用,则只打印此报告。 现在,将所有单独的数据源放在一个地图数据源中,并将此数据源传递给主报表。
将所有子报表配置为映射中的键。

不幸的是,解决方案是构建一个子报表并使用2个不同的数据源或您使用的任何连接

但是有一个简单的方法可以解决这个问题:D 只是简单,没有新的报告。。。。。瞧

好吧,我们开始吧

JasperPrint jp1 = JasperFillManager.fillReport(url.openStream(), parameters,
                    new JRBeanCollectionDataSource(inspBean));
JasperPrint jp2 = JasperFillManager.fillReport(url.openStream(), parameters,
                    new JRBeanCollectionDataSource(inspBean));
好的,我们有2条以上的记录。让我们把我们的第一条记录jp1和jp2内容添加到其中

List pages = jp2 .getPages();
for (int j = 0; j < pages.size(); j++) {
    JRPrintPage object = (JRPrintPage)pages.get(j);
    jp1.addPage(object);

}
JasperViewer.viewReport(jp1,false);
List pages=jp2.getPages();
对于(int j=0;j
这工作很有魅力。。通过几个循环,您可以将任意数量的报表合并在一起。。不创建新报告


一张茉莉花印花多页

示例代码:

DefaultTableModel dtm = new DefaultTableModel(new Object[0][3], new String[]{"Id","Name","Family"});
String[] fields= new String[3];    
boolean firstFlag=true;
JasperPrint jp1 =null;
JasperPrint jp2 =null;
for (int i=0 ; i<=pagesCount ; i++)
{
   fields[0]= "id";
   fields[1]= "name";
   fields[2]= "family";
   dtm.insertRow(0, fields); 
   try
   {
      Map<String, Object> params = new HashMap<String, Object>();                
      if (firstFlag)
      {
         jp1 = JasperFillManager.fillReport(getClass().getResourceAsStream(reportsource), params, new JRTableModelDataSource(dtm));                
         firstFlag=false;                            
      }else
      {
         jp2 = JasperFillManager.fillReport(getClass().getResourceAsStream(reportsource), params, new JRTableModelDataSource(dtm));
         jp1.addPage(jp2.getPages().get(0));
      }     
   }catch (Exception e) 
   {
      System.out.println(e.fillInStackTrace().getMessage());
   }
}
JasperViewer.viewReport(jp1,false);
DefaultTableModelDTM=新的DefaultTableModel(新对象[0][3],新字符串[]{“Id”,“Name”,“Family”});
字符串[]字段=新字符串[3];
布尔值firstFlag=true;
JasperPrint jp1=null;
JasperPrint jp2=null;

对于(int i=0;iLahiru Nirmal的答案简单而中肯。这里有一个稍微扩展的版本,它也复制了风格和其他东西(我认为并非所有这些都是至关重要的)

请注意,所有页面的大小都相同

public static JasperPrint createJasperReport(String name, JasperPrint pattern)
{
    JasperPrint newPrint = new JasperPrint();
    newPrint.setBottomMargin(pattern.getBottomMargin());
    newPrint.setLeftMargin(pattern.getLeftMargin());
    newPrint.setTopMargin(pattern.getTopMargin());
    newPrint.setRightMargin(pattern.getRightMargin());
    newPrint.setLocaleCode(pattern.getLocaleCode());
    newPrint.setName(name);
    newPrint.setOrientation(pattern.getOrientationValue());
    newPrint.setPageHeight(pattern.getPageHeight());
    newPrint.setPageWidth(pattern.getPageWidth());
    newPrint.setTimeZoneId(pattern.getTimeZoneId());

    return newPrint;
}

public static void addJasperPrint(JasperPrint base, JasperPrint add)
{
    for (JRStyle style : add.getStyles())
    {
        String styleName = style.getName();
        if (!base.getStylesMap().containsKey(styleName))
        {
            try
            {
                base.addStyle(style);
            }
            catch (JRException e)
            {
                logger.log(Level.WARNING, "Couldn't add a style", e);
            }
        }
        else
            logger.log(Level.FINE, "Dropping duplicate style: " + styleName);
    }

    for (JRPrintPage page : add.getPages())
        base.addPage(page);

    if (add.hasProperties())
    {
        JRPropertiesMap propMap = add.getPropertiesMap();
        for (String propName : propMap.getPropertyNames())
        {
            String propValue = propMap.getProperty(propName);
            base.setProperty(propName, propValue);
        }
    }

    if (add.hasParts())
    {
        PrintParts parts = add.getParts();
        Iterator<Entry<Integer, PrintPart>> partsIterator = parts.partsIterator();
        while (partsIterator.hasNext())
        {
            Entry<Integer, PrintPart> partsEntry = partsIterator.next();
            base.addPart(partsEntry.getKey(), partsEntry.getValue());
        }
    }

    List<PrintBookmark> bookmarks = add.getBookmarks();
    if (bookmarks != null)
        for (PrintBookmark bookmark : bookmarks)
            base.addBookmark(bookmark);
}

我看到JasperReports现在有一个更新的“Report Book”功能,这可能是一个更好的解决方案,但我还没有使用它。

您可以使用如下JasperPrint列表:

List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();

jasperPrintList.add(JasperFillManager.fillReport("Report_file1.jasper", getReportMap(1), new JREmptyDataSource()));
jasperPrintList.add(JasperFillManager.fillReport("Report_file2.jasper", getReportMap(2), new JREmptyDataSource()));
jasperPrintList.add(JasperFillManager.fillReport("Report_file3.jasper", getReportMap(3), new JREmptyDataSource()));

JRPdfExporter exporter = new JRPdfExporter();

exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("Report_PDF.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setCreatingBatchModeBookmarks(true);
exporter.setConfiguration(configuration);

exporter.exportReport();
List jasperPrintList=newarraylist();
jasperPrintList.add(JasperFillManager.fillReport(“Report_file1.jasper”,getReportMap(1),newjrpemptydatasource());
jasperPrintList.add(JasperFillManager.fillReport(“Report_file2.jasper”,getReportMap(2),newjrpemptyDataSource());
jasperPrintList.add(JasperFillManager.fillReport(“Report_file3.jasper”,getReportMap(3),newjreportydatasource());
JRPdfExporter exporter=新的JRPdfExporter();
setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporter.setExporterOutput(新的SimpleOutputStreamExporterOutput(“Report_PDF.PDF”);
SimplePDExporterConfiguration配置=新的SimplePDExporterConfiguration();
configuration.setCreatingBatchModeBookmarks(true);
exporter.setConfiguration(配置);
出口商。出口报告();

+1简单而优雅的解决方案,在我被互相平移的子报告等难倒之后。这是一个很好的解决方案(感谢Lahiru)因为我们有一个要求,即向客户发送两种不同语言的通知,客户可能会选择两种语言或单一语言。@lahiru建议的perfects适合我们的要求。我认为这也可以解决以下问题??不错,但是关于页数或目录?可以填写有正确的内容?只有当您有固定数量的子报告时,这才是可行的,如果您需要以友好方式附加报告,子报告不是一个选项。
List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();

jasperPrintList.add(JasperFillManager.fillReport("Report_file1.jasper", getReportMap(1), new JREmptyDataSource()));
jasperPrintList.add(JasperFillManager.fillReport("Report_file2.jasper", getReportMap(2), new JREmptyDataSource()));
jasperPrintList.add(JasperFillManager.fillReport("Report_file3.jasper", getReportMap(3), new JREmptyDataSource()));

JRPdfExporter exporter = new JRPdfExporter();

exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput("Report_PDF.pdf"));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setCreatingBatchModeBookmarks(true);
exporter.setConfiguration(configuration);

exporter.exportReport();