Search code examples
javajasper-reports

Where can be found globalOffsetX, globalOffsetY and zoom in class extending new JRGraphics2DExporter?


I am upgrading libraries (from 3.x version to 6.0.0) in my project, which is using them, and I don't know, where get those 3 missing variables. They used to be protected in older version. Now they are private.

My old code:

import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.export.JRGraphics2DExporter;

public class SRptGraphics2DExporter extends JRGraphics2DExporter {

    Double rotationAngle = 0.0;

    public SRptGraphics2DExporter() throws JRException {
        super();
        this.setParameter(SRptGraphics2DExporterParameter.ROTATION_ANGLE, new Double(0.0));
    }

    @Override
    public void exportReportToGraphics2D() throws JRException {
        boolean printPageBorders = (Boolean) parameters.get(SRptGraphics2DExporterParameter.PRINT_PAGE_BORDERS);
        rotationAngle = (Double) parameters.get(SRptGraphics2DExporterParameter.ROTATION_ANGLE);
        grx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        grx.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        grx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

        AffineTransform atrans = new AffineTransform();
        atrans.translate(globalOffsetX, globalOffsetY);
        atrans.rotate(rotationAngle);
        atrans.scale(zoom, zoom);

        AffineTransform sa = grx.getTransform();
        grx.transform(atrans);

        java.util.List pages = jasperPrint.getPages();
        if (pages != null) {
            Shape oldClipShape = grx.getClip();
            grx.clip(new Rectangle(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight()));

            try {
                JRPrintPage page = (JRPrintPage) pages.get(startPageIndex);
                exportPage(page);
                if (printPageBorders) {
                    grx.drawRect(0, 0, jasperPrint.getPageWidth(), jasperPrint.getPageHeight());
                }
            } finally {
                grx.setClip(oldClipShape);
                grx.setTransform(sa);
            }
        }
    }
}

Solution

  • I have finally found inherited function getCurrentItemConfiguration();, where I found these functions: getCurrentItemConfiguration().getZoomRatio();, getCurrentItemConfiguration().getOffsetX() and getCurrentItemConfiguration().getOffsetY().

    So I've changed source code (with some other changes due other incompatibilities):

    @Override
    public void exportReportToGraphics2D(Graphics2D grx) throws JRException {
    
        Shape oldClipShape = grx.getClip();
        boolean printPageBorders = (Boolean) parameters.get(SRptGraphics2DExporterParameter.PRINT_PAGE_BORDERS);
        grx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //grx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        grx.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        grx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    
        rotationAngle = (Double) parameters.get(SRptGraphics2DExporterParameter.ROTATION_ANGLE);
    
        setCurrentExporterInputItem(exporterInput.getItems().get(0));
    
        ReportExportConfiguration configuration = getCurrentItemConfiguration();
        AffineTransform sa = grx.getTransform();
    
        float zoom = 1f;
    
        Float zoomRatio = getCurrentItemConfiguration().getZoomRatio();
        if (zoomRatio != null) {
            zoom = zoomRatio.floatValue();
            if (zoom <= 0) {
                throw new JRRuntimeException(
                        EXCEPTION_MESSAGE_KEY_INVALID_ZOOM_RATIO,
                        new Object[]{zoom}
                );
            }
        }
    
        AffineTransform atrans = new AffineTransform();
        atrans.translate(
                configuration.getOffsetX() == null ? 0 : configuration.getOffsetX(),
                configuration.getOffsetY() == null ? 0 : configuration.getOffsetY()
        );
        atrans.rotate(rotationAngle);
    
        atrans.scale(zoom, zoom);
    
        grx.transform(atrans);
    
        List<JRPrintPage> pages = jasperPrint.getPages();
        if (pages != null) {
            PageRange pageRange = getPageRange();
            int startPageIndex = (pageRange == null || pageRange.getStartPageIndex() == null) ? 0 : pageRange.getStartPageIndex();
    
            PrintPageFormat pageFormat = jasperPrint.getPageFormat(startPageIndex);
    
            grx.clip(new Rectangle(0, 0, pageFormat.getPageWidth(), pageFormat.getPageHeight()));
            atrans = grx.getTransform();
            try {                
                AffineTransform ertrans = new AffineTransform();
                ertrans.translate(
                        configuration.getOffsetX() == null ? 0 : -configuration.getOffsetX(),
                        configuration.getOffsetY() == null ? 0 : -configuration.getOffsetY()
                ); 
                grx.transform(ertrans);
    
                exportPage(grx, startPageIndex);
    
                grx.setTransform(atrans);
    
                if (printPageBorders) {
                    grx.drawRect(1, 1, pageFormat.getPageWidth() -1, pageFormat.getPageHeight()-1);
                }
    
    
            } finally {
    
                grx.setTransform(sa);
                grx.setClip(oldClipShape);
    
            }
        }
    }