java - 如何将碧玉报告水平拆分为 2 页
问题描述
我想在 A4 尺寸的打印机上打印一份关于碧玉的 A2 尺寸报告。我想把它打印成四张横向打印,所以第 1 页和第 2 页成为 A2 的顶部,第 3 页和第 4 页成为 A2 的底部。
.______________________________。
| | |
| 1 | 2 |
| | |
|-----------..------------+--.-------------------- |
| | |
| 3 | 4 |
|.______________|______________.|
通常打印只打印左侧部分页面 1 和 3。我怎样才能打印页面的所有四个部分,每个部分都在自己的页面上
解决方案
实际上我能够做到这一点(不是最好的质量,但目前对我有用):
- 首先转换碧玉报告图像,
- 然后将图像裁剪成打印机纸张大小的片段,然后再将它们发送到打印机。
- 并将图像一张一张发送到打印机
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();
}
}
推荐阅读
- javascript - 从返回未定义的对象中获取值
- python - 如何使用 Python 去除图像中的小物体
- c# - 如何在 dotnet core API 中接受和返回图像
- javascript - Node.js:为什么永远不会调用转换流的转换函数?
- react-native - 如何找到要在 HTTP 请求中使用的 IP 地址?
- oracle - 如何在没有验证的情况下禁用 oracle apex 中的过去日期
- python - Anaconda 中 Spyder 的问题
- swiftui - 使用协议定义swiftui视图的属性
- python - 如何将 CSV 文件从 Lambda 上传到 S3 存储桶?“[错误] KeyError:'记录'和我的 lambda 函数也没有被 s3 触发
- html - 垂直对齐div内的列表项