为了账号安全,请及时绑定邮箱和手机立即绑定

如何将 jasper 报告水平拆分为 2 页

如何将 jasper 报告水平拆分为 2 页

四季花海 2021-09-15 14:34:20
我想在 A4 尺寸的打印机上打印 A2 尺寸的碧玉报告。我想以四张横向打印的方式打印它,因此第 1 页和第 2 页位于 A2 的顶部,第 3 页和第 4 页位于 A2 的底部。.________________________________________。| | || 1 | 2 || | ||------------..------------+--.------------ || | || 3 | 4 ||.______________|______________.|通常打印只打印左边的第 1 页和第 3 页。如何打印页面的所有四个部分,每个部分都在自己的页面上
查看完整描述

1 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

实际上我能够做到(不是最好的质量,但目前对我有用):


首先转换碧玉报告一个图像,

然后将图像裁剪成打印机纸张大小的碎片,然后再将它们发送到打印机。

并将图像一张一张地发送到打印机

JasperPrint jp = the_jasper_print_to_be_printed; // 

int i = 1; // Page Number to print

float zoom = 1f;


BufferedImage image = (BufferedImage) JasperPrintManager.printPageToImage(jp, i, zoom);

PrinterJob printJob = PrinterJob.getPrinterJob();

PageFormat pf = printJob.getPageFormat(null);

int paperWidth = Functions.StringToInt(pf.getImageableWidth());

int paperHeight = Functions.StringToInt(pf.getImageableHeight());

int x = 0, y = 0;

while (y < image.getHeight()) {

    x = 0;

    while (x < image.getWidth()) {

        Rectangle rect = new Rectangle(x, y, paperWidth, paperHeight);

        printImage(Functions.cropImage(image, rect), printJob);

        x += paperWidth;

    }

    y += paperHeight;

}

裁剪图像的功能


public static BufferedImage cropImage(BufferedImage src, Rectangle rect) {

    int w = (rect.x + rect.width > src.getWidth()) ? src.getWidth() - rect.x : rect.width;

    int h = (rect.y + rect.height > src.getHeight()) ? src.getHeight()- rect.y : rect.height;

    BufferedImage dest = src.getSubimage(rect.x, rect.y, w, h);

    return dest;

}

将裁剪图像发送到打印机的功能


private static void printImage(BufferedImage image, PrinterJob printJob) {

    printJob.setPrintable(new Printable() {

        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

            if (pageIndex != 0) {

                return NO_SUCH_PAGE;

            }

            graphics.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);

            return PAGE_EXISTS;

        }

    });

    try {

        printJob.print();

    } catch (PrinterException e1) {

        e1.printStackTrace();

    }

}


查看完整回答
反对 回复 2021-09-15
  • 1 回答
  • 0 关注
  • 222 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信