1 /* 2 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 6543815 6601097 8160888 27 * @summary Image should be sent to printer, no exceptions thrown. 28 * The 3 printouts should have a rectangle which is the minimum 29 * possible margins ie, the margins should be hardware margins 30 * and not java default 1 inch margins. 31 * @run main Margins 32 */ 33 34 import java.awt.print.PrinterJob; 35 import java.awt.print.Printable; 36 import java.awt.print.PageFormat; 37 import java.awt.print.Paper; 38 import java.awt.print.PrinterException; 39 import java.awt.Graphics; 40 import java.awt.Graphics2D; 41 import java.awt.Color; 42 import java.awt.Robot; 43 import java.awt.event.KeyEvent; 44 45 public class Margins implements Printable { 46 47 public static void main(String args[]) throws Exception { 48 Robot robot = new Robot(); 49 Thread t = new Thread (() -> { 50 robot.waitForIdle(); 51 robot.delay(5000); 52 robot.keyPress(KeyEvent.VK_ENTER); 53 robot.keyRelease(KeyEvent.VK_ENTER); 54 robot.waitForIdle(); 55 robot.delay(5000); 56 robot.keyPress(KeyEvent.VK_ENTER); 57 robot.keyRelease(KeyEvent.VK_ENTER); 58 robot.waitForIdle(); 59 robot.delay(5000); 60 robot.keyPress(KeyEvent.VK_ENTER); 61 robot.keyRelease(KeyEvent.VK_ENTER); 62 }); 63 64 PrinterJob job = PrinterJob.getPrinterJob(); 65 PageFormat pageFormat = job.defaultPage(); 66 Paper paper = pageFormat.getPaper(); 67 double wid = paper.getWidth(); 68 double hgt = paper.getHeight(); 69 paper.setImageableArea(0, -10, wid, hgt); 70 t.start(); 71 72 pageFormat = job.pageDialog(pageFormat); 73 pageFormat.setPaper(paper); 74 job.setPrintable(new Margins(), pageFormat); 75 try { 76 job.print(); 77 } catch (PrinterException e) { 78 } 79 80 paper.setImageableArea(0, 0, wid, hgt + 72); 81 pageFormat = job.pageDialog(pageFormat); 82 pageFormat.setPaper(paper); 83 84 job.setPrintable(new Margins(), pageFormat); 85 try { 86 job.print(); 87 } catch (PrinterException e) { 88 } 89 90 pageFormat = job.defaultPage(); 91 paper = pageFormat.getPaper(); 92 wid = paper.getWidth(); 93 hgt = paper.getHeight(); 94 95 paper.setImageableArea(0, -10, -wid, hgt); 96 pageFormat = job.pageDialog(pageFormat); 97 pageFormat.setPaper(paper); 98 99 job.setPrintable(new Margins(), pageFormat); 100 try { 101 job.print(); 102 } catch (PrinterException e) { 103 } 104 } 105 106 public int print(Graphics g, PageFormat pf, int page) 107 throws PrinterException { 108 109 if (page > 0) { 110 return NO_SUCH_PAGE; 111 } 112 int ix = (int)pf.getImageableX(); 113 int iy = (int)pf.getImageableY(); 114 int iw = (int)pf.getImageableWidth(); 115 int ih = (int)pf.getImageableHeight(); 116 System.out.println("ix="+ix+" iy="+iy+" iw="+iw+" ih="+ih); 117 if ((ix < 0) || (iy < 0)) { 118 throw new RuntimeException("Imageable x or y is a negative value."); 119 } 120 121 122 Paper paper = pf.getPaper(); 123 int wid = (int)paper.getWidth(); 124 int hgt = (int)paper.getHeight(); 125 System.out.println("wid="+wid+" hgt="+hgt); 126 /* 127 * If imageable width/height is -ve, then print was done with 1" margin 128 * e.g. ix=72 iy=72 iw=451 ih=697 and paper wid=595 129 * but with fix, we get print with hardware margin e.g. 130 * ix=12, iy=12, iw=571, ih=817 131 */ 132 if ((wid - iw > 72) || (hgt - ih > 72)) { 133 throw new RuntimeException("Imageable width or height is negative value"); 134 } 135 if ((ix+iw > wid) || (iy+ih > hgt)) { 136 throw new RuntimeException("Printable width or height " 137 + "exceeds paper width or height."); 138 } 139 // runtime checking to see if the margins/printable area 140 // correspond to the entire size of the paper, for now, make it pass 141 // as for linux, the hwmargin is not taken into account - bug6574279 142 if (ix == 0 && iy == 0 && (ix+iw == wid) && (iy+ih == hgt)) { 143 return PAGE_EXISTS; 144 } 145 146 Graphics2D g2d = (Graphics2D)g; 147 g2d.translate(ix, iy); 148 g2d.setColor(Color.black); 149 g2d.drawRect(1, 1, iw-2, ih-2); 150 151 return PAGE_EXISTS; 152 } 153 }