如何在加载和现有模板进入JasperDesign后更改页面大小/格式然后更改它? [英] How to change page sizing/format when load and existing template into JasperDesign and then changed it?

查看:238
本文介绍了如何在加载和现有模板进入JasperDesign后更改页面大小/格式然后更改它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个帖子中提出了这个问题。我不确定是否应该创建一个新问题,因为我似乎已经在创建一个新主题了?为此道歉。

I am bringing this question from this post. I was not sure if I should create a new question since it seems I was already creating a new topic? Apologies upfront for that.

所以这是我的问题:

我正在尝试动态更改格式我的报告(书报告,包含多个页面或部分或子报告)。我正在做的是首先将它从现有模板加载到JasperDesign对象持有者中。然后,修改页面本身的其他参数的大小。稍后,我将编译并尝试使用编译的文件打印PDF格式的报告。最后,我想打印一本带有不同页面和不同模板的孔洞书报告,每个页面都有我可以指定的格式。

I am trying to dynamically change the format of my report (A book report, with several pages or parts or subreports). What I am doing is loading it first, from an existing template into a JasperDesign Object holder. Then, modifying the size an other parameters of the page itself. Later, I will compile and try to print a PDF format report with the compiled file. Ultimately, I want to print a hole book report with different pages and different templates for each individual pages with a format that I can specify.

这就是我所做的。我能够将主报告指向其他单个页面格式。因此,当我将报告导出为PDF时,一切看起来都不错(就像你可能想象的那样,到达那里并不那么简单。但现在正在运行)。所以现在我想在书报告的每一页上对运行时的格式(A4,LETTER等)进行参数化。

Here is what I have done. I was able to point the main report to the other individual pages formats. So, when I export the report to PDF everything looks good(Like you probably imagine it was not that simple to get there. but now is working). So now I want to parametrize the format (A4, LETTER, etc.) on runtime for every single page of the Book Report.

这是否可行?我的意思是动态地这样做,而不是为每个页面和格式创建一个新的静态模板,然后创建一个mantainence问题?如果是这样做的话?

Is this posible? I mean to do this dynamically instead of creating a new static template for each page and format and then creating a mantainence issue? If so which is the way of doing this?

以下是我重新审视JasperDesing对象模板的代码:

Here is my code for retriving the JasperDesing Object template:

private static JasperDesign getTemplateDesign(String templatePath) throws JRException{

    JasperDesign design = JRXmlLoader.load(templatePath);

    logger.info("Setting Page Dimessions Format to: " + pageFormat);
    if(JRDriver.pageFormat.equals("A3")){
    JRDriver.pageWidth=842; 
    JRDriver.pageHeight=1190;
    JRDriver.columnCount=1;
    JRDriver.columnWidth=802;
    JRDriver.columnSpacing=0;
    JRDriver.leftMargin=20;
    JRDriver.rightMargin=20;
    JRDriver.topMargin=30;
    JRDriver.bottomMargin=30;
    }
    else if (JRDriver.pageFormat.equals("A4")) {
        JRDriver.pageWidth=842; 
        JRDriver.pageHeight=1190;
        JRDriver.columnCount=1;
        JRDriver.columnWidth=555;
        JRDriver.columnSpacing=0;
        JRDriver.leftMargin=20;
        JRDriver.rightMargin=20;
        JRDriver.topMargin=30;
        JRDriver.bottomMargin=30;
    }
    else if (JRDriver.pageFormat.equals("LETTER")) {            
        JRDriver.pageWidth=612; 
        JRDriver.pageHeight=792;
        JRDriver.columnCount=1;
        JRDriver.columnWidth=517;
        JRDriver.columnSpacing=0;
        JRDriver.leftMargin=20;
        JRDriver.rightMargin=20;
        JRDriver.topMargin=30;
        JRDriver.bottomMargin=30;
    }
    //Setting twmplate dimesions
    design.setPageWidth(JRDriver.pageWidth);
    design.setPageHeight(JRDriver.pageHeight);

    design.setColumnCount(JRDriver.columnCount);
    design.setColumnWidth(JRDriver.columnWidth);
    design.setColumnSpacing(JRDriver.columnSpacing);

    design.setLeftMargin(JRDriver.leftMargin);
    design.setRightMargin(JRDriver.rightMargin);
    design.setTopMargin(JRDriver.topMargin);
    design.setBottomMargin(JRDriver.bottomMargin);

    return design;
}

这是我生成PDF报告的代码片段:

Here is my code fragment for generating the PDF report:

...

logger.info("Retrieving Design Template..." + reportTemplatePath);
    JasperDesign design= getTemplateDesign(reportTemplatePath);

    //jasperReport = JasperCompileManager.compileReport(reportTemplatePath);
    jasperReport = JasperCompileManager.compileReport(design);

    logger.info("Filling Report...");
    jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter,new JREmptyDataSource() );//new JREmptyDataSource()


    jasperPrint.setPageWidth(JRDriver.pageWidth);
    jasperPrint.setPageHeight(JRDriver.pageHeight);

    //Create the file dir
    File file = new File(reportOuputPath);
    file.getParentFile().mkdirs();
    //file.delete();

    logger.info("Writing PDF..." + reportOuputPath);
    JasperExportManager.exportReportToPdfFile(jasperPrint, reportOuputPath);

...

我可以包括我的JRXML静态模板文件的信息。但我认为现在不需要这样做。如果您不这么认为可以这样做

I could include the information for my JRXML static template files. But I don't think that is necessary for now. It can be done if you think otherwise

请帮助我并提前致谢

推荐答案

您好,感谢您的快速解答。我找不到如何评论你的答案,所以我想我必须在这篇文章中加入。

Hello and thanks for the quick answer. I couldn't find how to comment on you answer so I guess I will have to include it in this post.

首先,你是对的,不保证整个页面通过更改de页面尺寸来调整布局大小。但是,我改变的格式非常相似(A4和LETTER),因此对组件和字段大小的影响不大,并没有那么大。

First, you were right about not guaranteeing the whole page layout resizing, by just changing the de page dimensions. However, the formats that I was changing to, were very similar (A4 and LETTER), so the impact on the components and fields size perse, was not that big.

此外,我要做的是,实际更改书籍格式的每个人的页面,首先将每个单独的页面加载到JasperDesing对象持有者中,然后保存它将修改后的大小放入jasper字段中,将用于填充过程。而不仅仅是报告书页面本身。

Moreover, what I have to do, to actually change every single individual's page of the book format, was loading every single individual page first into a JasperDesing Object holder and next save it , with the modified size into a jasper field, that will be used in the fillup process. Instead of just doing that for the Report Book page itself.

以下是代码:

    private static JasperDesign getPageTemplateDesign(String templatePath) throws JRException{

    logger.info("Setting format page desing for Template: " + templatePath);

    JasperDesign design = JRXmlLoader.load(templatePath);

    logger.info("Setting Page Dimessions Format to: " + pageFormat);
    if (JRDriver.pageFormat.equals("A4")) {
        JRDriver.pageWidth=595; 
        JRDriver.pageHeight=842;
        JRDriver.columnCount=1;
        JRDriver.columnWidth=555;
        JRDriver.columnSpacing=0;
        JRDriver.leftMargin=20;
        JRDriver.rightMargin=20;
        JRDriver.topMargin=20;
        JRDriver.bottomMargin=20;
    }
    else if (JRDriver.pageFormat.equals("LETTER")) {
        JRDriver.pageWidth=612; 
        JRDriver.pageHeight=792;
        JRDriver.columnCount=1;
        JRDriver.columnWidth=517;
        JRDriver.columnSpacing=0;
        JRDriver.leftMargin=20;
        JRDriver.rightMargin=20;
        JRDriver.topMargin=20;
        JRDriver.bottomMargin=20;
    }
    //Setting twmplate dimesions
    design.setPageWidth(JRDriver.pageWidth);
    design.setPageHeight(JRDriver.pageHeight);

    design.setColumnCount(JRDriver.columnCount);
    design.setColumnWidth(JRDriver.columnWidth);
    design.setColumnSpacing(JRDriver.columnSpacing);

    design.setLeftMargin(JRDriver.leftMargin);
    design.setRightMargin(JRDriver.rightMargin);
    design.setTopMargin(JRDriver.topMargin);
    design.setBottomMargin(JRDriver.bottomMargin);      

    return design;
}

private static void reformattingPageTemplate(String path) throws JRException{

    logger.info("Getting Design Template from: " + path);
    JasperDesign design= getPageTemplateDesign(path);

    String destFile=path.replaceAll("jrxml", "jasper");     
    logger.info("Compiling Re-formated Design Template to: " + destFile);
    JasperCompileManager.compileReportToFile(design, destFile); 
}

private static void formattingALLPage() throws JRException{

    reformattingPageTemplate(reportBookPagesDir + "/FrontCover.jrxml");
    reformattingPageTemplate(reportBookPagesDir + "/Copyright.jrxml");
    reformattingPageTemplate(reportBookPagesDir + "/TableOfContent.jrxml");
    reformattingPageTemplate(reportBookPagesDir + "/Summary.jrxml");
    reformattingPageTemplate(reportBookPagesDir + "/Content.jrxml");
    reformattingPageTemplate(reportBookPagesDir + "/AppendixWarnings.jrxml");
    reformattingPageTemplate(reportBookPagesDir + "/Index.jrxml");
    reformattingPageTemplate(reportBookPagesDir + "/BackCover.jrxml");
    //reformattingPageTemplate(reportTemplatePath);
}

感谢您的帮助

这篇关于如何在加载和现有模板进入JasperDesign后更改页面大小/格式然后更改它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆