哪里可以找到globalOffsetX,globalOffsetY和放大新JRGraphics2DExporter的类?

时间:2016-07-07 10:51:25

标签: java jasper-reports

我正在我的项目中升级库(从3.x版本升级到6.0.0),正在使用它们,我不知道,哪里有3个缺少的变量。他们曾经在旧版本中受到保护。现在他们是私人的。

我的旧代码:

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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我终于找到了继承的函数getCurrentItemConfiguration();,在那里我找到了这些函数: getCurrentItemConfiguration().getZoomRatio();getCurrentItemConfiguration().getOffsetX()getCurrentItemConfiguration().getOffsetY()

所以我更改了源代码(由于其他不兼容性而导致其他一些更改):

@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);

        }
    }
}