Printing on Java A3 on A4 Macs

I have a weird issue that seems specific to Mac computers. I have a program that prints the contents of an AWT drawing surface onto a sheet of A3 paper. On Linux and Windows machines, the output is fine. Print from Mac. I get the same dialog with the same presets that the printer prints on a sheet of A3 paper, as expected, but for some reason, the image is scaled to fit the A4 area. Below are the relevant sections of the code:

public void print() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    PageFormat format = new PageFormat();
    format.setOrientation(PageFormat.LANDSCAPE);

    double margin = 18; // quarter inch
    double m2 = margin * 2;
    Paper paper = format.getPaper();
    paper.setSize(16.54 * 72, 11.69 * 72); // A3 Landscape
    paper.setImageableArea(margin, margin,  paper.getWidth()-m2, paper.getHeight()-m2);
    format.setPaper(paper);
    printJob.setPrintable(this, format);

    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    attributes.add(MediaSizeName.ISO_A3);
    attributes.add(OrientationRequested.LANDSCAPE);
    attributes.add(new MediaPrintableArea((int)margin, (int)margin,
            (int)(paper.getWidth()-m2), (int)(paper.getHeight()-m2),
            MediaPrintableArea.INCH));

    if(printJob.printDialog(attributes) ){
      try {
        printJob.print(attributes);
      } catch(PrinterException pe) {
        pe.printStackTrace();
      }
    }
  }

  public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
    if (pageIndex > 0) {
      return(NO_SUCH_PAGE);
    } else {
      Graphics2D g2d = (Graphics2D)g;
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
      if(this.componentToBePrinted instanceof PlannerView){
          setScale(g2d);
      }
      disableDoubleBuffering(componentToBePrinted);
      componentToBePrinted.paint(g2d);
      enableDoubleBuffering(componentToBePrinted);
      return(PAGE_EXISTS);
    }
  }

  public void setScale(Graphics2D g2d){
      PlannerView planner = (PlannerView)this.componentToBePrinted;
      planner.resetZoom();
      double scale = 1.0 / planner.getRowLength();
      scale *= 4.0; 
      g2d.scale(scale, scale);
  }

Does anyone know what could be causing this?

Greetings.

+3
source share
2 answers

Try to call

PageFormat newFormat =printJob.pageDialog(format);

.

, , .

, Macintosh .

, , REVERSE_LANDSCAPE LANDSCAPE ( , )

+1

setImageableArea(), . reverse_landscape mac. A3: 1190 842, .

0

Source: https://habr.com/ru/post/1765983/


All Articles