001: /*
002: * PrintUtil.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.print;
013:
014: import java.awt.print.PageFormat;
015:
016: import javax.print.attribute.HashPrintRequestAttributeSet;
017: import javax.print.attribute.PrintRequestAttribute;
018: import javax.print.attribute.PrintRequestAttributeSet;
019: import javax.print.attribute.standard.MediaPrintableArea;
020: import javax.print.attribute.standard.OrientationRequested;
021:
022: /**
023: *
024: * @author support@sql-workbench.net
025: */
026: public class PrintUtil {
027:
028: /** Creates a new instance of PrintUtil */
029: public PrintUtil() {
030: }
031:
032: public static PrintRequestAttributeSet getPrintAttributes(
033: PageFormat format) {
034: PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
035: PrintRequestAttribute att = null;
036: int value = format.getOrientation();
037: switch (value) {
038: case PageFormat.LANDSCAPE:
039: att = OrientationRequested.LANDSCAPE;
040: break;
041: case PageFormat.PORTRAIT:
042: att = OrientationRequested.PORTRAIT;
043: break;
044: case PageFormat.REVERSE_LANDSCAPE:
045: att = OrientationRequested.REVERSE_LANDSCAPE;
046: break;
047: }
048: attr.add(att);
049: att = new MediaPrintableArea((float) format.getImageableX(),
050: (float) format.getImageableY(), (float) format
051: .getImageableWidth(), (float) format
052: .getImageableHeight(), MediaPrintableArea.INCH);
053: attr.add(att);
054:
055: return attr;
056: }
057:
058: public static boolean pageFormatEquals(PageFormat first,
059: PageFormat second) {
060: if (first == null || second == null)
061: return false;
062: if (first.getOrientation() != second.getOrientation())
063: return false;
064: if (first.getHeight() != second.getHeight())
065: return false;
066: if (first.getWidth() != second.getWidth())
067: return false;
068: if (first.getImageableX() != second.getImageableX())
069: return false;
070: if (first.getImageableY() != second.getImageableY())
071: return false;
072: if (first.getImageableWidth() != second.getImageableWidth())
073: return false;
074: if (first.getImageableHeight() != second.getImageableHeight())
075: return false;
076: return true;
077: }
078:
079: public static double millimeterToPoints(double mm) {
080: double inch = millimeterToInch(mm);
081: return (inch * 72);
082: }
083:
084: public static double pointsToMillimeter(double points) {
085: // convert mm to inch
086: double inch = points / 72;
087: return inchToMillimeter(inch);
088: }
089:
090: public static double millimeterToInch(double mm) {
091: return (mm / 25.4);
092: }
093:
094: public static double inchToMillimeter(double inch) {
095: return (inch * 25.4);
096: }
097:
098: public static void printPageFormat(String aName, PageFormat aFormat) {
099: double width = aFormat.getPaper().getWidth();
100: double height = aFormat.getPaper().getHeight();
101:
102: double leftmargin = aFormat.getImageableX();
103: double rightmargin = width - leftmargin
104: - aFormat.getImageableWidth();
105:
106: double topmargin = (int) aFormat.getImageableY();
107: double bottommargin = height - topmargin
108: - aFormat.getImageableHeight();
109:
110: System.out.println(aName + ": paper size=[" + width + ","
111: + height + "]");
112: System.out.println(aName + ": imageable size=["
113: + aFormat.getImageableWidth() + ","
114: + aFormat.getImageableHeight() + "]");
115: System.out.println(aName + ": margins (l,r,t,b)=" + leftmargin
116: + "," + rightmargin + "," + topmargin + ","
117: + bottommargin);
118: }
119: }
|