猿问

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

我想在 A4 尺寸的打印机上打印 A2 尺寸的碧玉报告。我想以四张横向打印的方式打印它,因此第 1 页和第 2 页位于 A2 的顶部,第 3 页和第 4 页位于 A2 的底部。


.________________________________________。

| | |

| 1 | 2 |

| | |

|------------..------------+--.------------ |

| | |

| 3 | 4 |

|.______________|______________.|


通常打印只打印左边的第 1 页和第 3 页。如何打印页面的所有四个部分,每个部分都在自己的页面上


四季花海
浏览 223回答 1
1回答

紫衣仙女

实际上我能够做到(不是最好的质量,但目前对我有用):首先转换碧玉报告一个图像,然后将图像裁剪成打印机纸张大小的碎片,然后再将它们发送到打印机。并将图像一张一张地发送到打印机JasperPrint jp = the_jasper_print_to_be_printed; //&nbsp;int i = 1; // Page Number to printfloat 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()) {&nbsp; &nbsp; x = 0;&nbsp; &nbsp; while (x < image.getWidth()) {&nbsp; &nbsp; &nbsp; &nbsp; Rectangle rect = new Rectangle(x, y, paperWidth, paperHeight);&nbsp; &nbsp; &nbsp; &nbsp; printImage(Functions.cropImage(image, rect), printJob);&nbsp; &nbsp; &nbsp; &nbsp; x += paperWidth;&nbsp; &nbsp; }&nbsp; &nbsp; y += paperHeight;}裁剪图像的功能public static BufferedImage cropImage(BufferedImage src, Rectangle rect) {&nbsp; &nbsp; int w = (rect.x + rect.width > src.getWidth()) ? src.getWidth() - rect.x : rect.width;&nbsp; &nbsp; int h = (rect.y + rect.height > src.getHeight()) ? src.getHeight()- rect.y : rect.height;&nbsp; &nbsp; BufferedImage dest = src.getSubimage(rect.x, rect.y, w, h);&nbsp; &nbsp; return dest;}将裁剪图像发送到打印机的功能private static void printImage(BufferedImage image, PrinterJob printJob) {&nbsp; &nbsp; printJob.setPrintable(new Printable() {&nbsp; &nbsp; &nbsp; &nbsp; public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pageIndex != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return NO_SUCH_PAGE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; graphics.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return PAGE_EXISTS;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; printJob.print();&nbsp; &nbsp; } catch (PrinterException e1) {&nbsp; &nbsp; &nbsp; &nbsp; e1.printStackTrace();&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答