How can I move the whole layout to adapt to pre-pr

2019-08-09 14:00发布

问题:

We have US Healthcare Medical Billing Product developed using Java, Hibernate, Spring and Jasper Report 5.6. that are printing with CMS 1500 and UB04 Form.

We want to print the values in the pre printed form i.e user will keep the this pre printed form in the printer and from the application we need to print the values in the boxes.

So we attached the image in the Jasper Report and put the text boxes in each of the boxes. It is printing correctly, but if the user change the printer, then alignment is becomes a problem. As a alternate dirty option, we took the copy and make the alignment for that printer, so now for every printer we have separate jasper report file even though the values that are printed are the same.

My client is asking to give them a option to set the X and Y value in separate form and then use these values to print correctly.

So the question is can we do this in jasper reports?

回答1:

The easiest way to move all report elements is to modify the report margins

Load the jrxml in to the JasperDesign object and switch margins as desired. The minimum you can move in x is the original report margin, the maximum depends on your report (naturally the columWidth can not become 0, but has no really sense checking this better to define a max)

Example:

JasperDesign design = JRXmlLoader.load("YourReport.jrxml");
moveDesign(design,x,y);
JasperReport report = JasperCompileManager.compileReport(design);

private void moveDesign(JasperDesign design, int x, int y) {
    int maxX = 100; //I define it so that elements is not out of report
    int maxY = 100;

    int pageWidth = design.getPageWidth();
    int intitalLeftMargin = design.getLeftMargin();
    int intitalRightMargin = design.getRightMargin();
    int intitalTopMargin= design.getTopMargin();

    //Check that not less then 0 and not more then my max       
    int newLeftMargin = Math.min(Math.max(intitalLeftMargin+x,0),maxX);
    int newTopMargin = Math.min(Math.max(intitalTopMargin+y,0),maxY);

    //set our new margins
    int newColumWidth = pageWidth - newLeftMargin - intitalRightMargin;
    design.setLeftMargin(newLeftMargin);
    design.setTopMargin(newTopMargin);
    design.setColumnWidth(newColumWidth);
}

The down-side of this is that you need to recompile your report (this will take a couple of ms).

If execution speed is of fundamental importance another solution (more complex but probably faster) is to move all elements in every page in the JasperPrint

I will leave the complete code to OP but it will be similar to this

List<JRPrintPage> pages = jasperPrint.getPages();
for (JRPrintPage jrPrintPage : pages) {
    List<JRPrintElement> elements = jrPrintPage.getElements();
    for (JRPrintElement jjpe : elements) {
        jjpe.setX(newX);
        jjpe.setY(newX);    
    }   
}