实际上我能够做到这一点(不是最好的质量,但目前对我有用):
- 先将碧玉报表转换成一张图片,
- 然后将图像裁剪成打印机纸张大小的片段,然后再发送到打印机。
- 并将图像一张一张发送到打印机
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();
}
}