0

I would like to print an A2 size report on jasper on an A4 size printer. i want to print it in four landscape prints so page 1 and 2 make the top of the A2 and pages 3 and 4 make the bottom part of the A2.

.______________________________.
|                             |                             |
|              1             |              2             |
|                             |                             |
|-----------..------------+--.--------------------|
|                             |                             |
|              3             |              4             |
|.______________|______________.|

normally printing only prints the left part page 1 and 3. how can I print all the four parts of the page, each on its own page

Alex K
  • 22,315
  • 19
  • 108
  • 236
Rick
  • 43
  • 4
  • Here is a similar question about that https://stackoverflow.com/questions/4190741/how-to-generate-pdf-reports-that-spans-multiple-pages-horizontally but sadly without answer how to do that horizontal split into multiple pages. – cgrim Sep 12 '18 at 18:58

1 Answers1

1

Actually I was able to do it by (Not the best quality, but works for me at the moment):

  • first converting the jasper report an image,
  • then cropping the image into pieces of the printer paper size before sending them to the printer.
  • and sending the image to the printer one by one
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;
}

Function to crop image

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

Function to send cropped image to printer

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();
    }
}
Rick
  • 43
  • 4