Problem mit BufferedImage und PrinterJob im Querformat

Milkaselnuss

Aktives Mitglied
Hallo,

in meinem Programm biete ich die Möglichkeit, ein BufferedImage auszudrucken.
Die geschieht über folgenden Code:

Java:
PrinterJob pJob = PrinterJob.getPrinterJob();
pJob.setPrintable(new ImagePrintable(pJob, bufferedImage));
			
if (pJob.printDialog()){
	try {
		pJob.print();
	} catch (PrinterException e) {

	}
}

Java:
public class ImagePrintable implements Printable {

     private double x, y, width;

     private int orientation;

     private BufferedImage image;

     public ImagePrintable(PrinterJob printJob, BufferedImage image) {
         PageFormat pageFormat = printJob.defaultPage();
         this.x = pageFormat.getImageableX();
         this.y = pageFormat.getImageableY();
         this.width = pageFormat.getImageableWidth();
         this.orientation = pageFormat.getOrientation();
         this.image = image;
     }

     @Override
     public int print(Graphics g, PageFormat pageFormat, int pageIndex)
             throws PrinterException {
         if (pageIndex == 0) {
             int pWidth = 0;
             int pHeight = 0;
             if (orientation == PageFormat.PORTRAIT) {
                 pWidth = (int) Math.min(width, (double) image.getWidth());
                 pHeight = pWidth * image.getHeight() / image.getWidth();
             } else {
                 pHeight = (int) Math.min(width, (double) image.getHeight());
                 pWidth = pHeight * image.getWidth() / image.getHeight();
                 System.out.println(image.getHeight());
                 System.out.println(pHeight);
             }
             g.drawImage(image, (int) x, (int) y, pWidth, pHeight, null);
             return PAGE_EXISTS;
         } else {
             return NO_SUCH_PAGE;
         }
     }
}


Ich habe zu Demonstrationszwecken einfach ein komplett schwarzes Bild sowohl im Hoch- als auch im Querformat ausgedruckt und das Ergebnis ist das:
Scan0003.jpgScan0004.jpg

Anmerkung: In dem Scan ist unterhalb der schwarzen Quadrate eine große weiße Fläche. In echt ist die deutlich kleiner!
Mit dem Ausdruck im Hochformat bin ich vollkommen zufrieden. Das Querformat sollte meiner Meinung nach allerdings das gesamte Blatt füllen und mir fehlt da gerade die Idee, wie ich das am Besten umsetze.
 

Zurück
Oben