Java Code Examples for javax.print.attribute.standard.MediaSize#getY()

The following examples show how to use javax.print.attribute.standard.MediaSize#getY() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnixPrintService.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private MediaPrintableArea[] getAllPrintableAreas() {

        if (mpas == null) {
            Media[] media = (Media[])getSupportedAttributeValues(Media.class,
                                                                 null, null);
            mpas = new MediaPrintableArea[media.length];
            for (int i=0; i< mpas.length; i++) {
                if (media[i] instanceof MediaSizeName) {
                    MediaSizeName msn = (MediaSizeName)media[i];
                    MediaSize mediaSize = MediaSize.getMediaSizeForName(msn);
                    if (mediaSize == null) {
                        mpas[i] = (MediaPrintableArea)
                            getDefaultAttributeValue(MediaPrintableArea.class);
                    } else {
                        mpas[i] = new MediaPrintableArea(0.25f, 0.25f,
                                        mediaSize.getX(MediaSize.INCH)-0.5f,
                                        mediaSize.getY(MediaSize.INCH)-0.5f,
                                        MediaSize.INCH);
                    }
                }
            }
        }
        MediaPrintableArea[] mpasCopy = new MediaPrintableArea[mpas.length];
        System.arraycopy(mpas, 0, mpasCopy, 0, mpas.length);
        return mpasCopy;
    }
 
Example 2
Source File: WPrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/** MediaSizeName / dmPaper */
private final int[] getWin32MediaAttrib() {
    int wid_ht[] = {0, 0};
    if (attributes != null) {
        Media media = (Media)attributes.get(Media.class);
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize ms = MediaSize.getMediaSizeForName(msn);
            if (ms != null) {
                wid_ht[0] = (int)(ms.getX(MediaSize.INCH) * 72.0);
                wid_ht[1] = (int)(ms.getY(MediaSize.INCH) * 72.0);
            }
        }
    }
    return wid_ht;
}
 
Example 3
Source File: UnixPrintService.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private MediaPrintableArea[] getAllPrintableAreas() {

        if (mpas == null) {
            Media[] media = (Media[])getSupportedAttributeValues(Media.class,
                                                                 null, null);
            mpas = new MediaPrintableArea[media.length];
            for (int i=0; i< mpas.length; i++) {
                if (media[i] instanceof MediaSizeName) {
                    MediaSizeName msn = (MediaSizeName)media[i];
                    MediaSize mediaSize = MediaSize.getMediaSizeForName(msn);
                    if (mediaSize == null) {
                        mpas[i] = (MediaPrintableArea)
                            getDefaultAttributeValue(MediaPrintableArea.class);
                    } else {
                        mpas[i] = new MediaPrintableArea(0.25f, 0.25f,
                                        mediaSize.getX(MediaSize.INCH)-0.5f,
                                        mediaSize.getY(MediaSize.INCH)-0.5f,
                                        MediaSize.INCH);
                    }
                }
            }
        }
        MediaPrintableArea[] mpasCopy = new MediaPrintableArea[mpas.length];
        System.arraycopy(mpas, 0, mpasCopy, 0, mpas.length);
        return mpasCopy;
    }
 
Example 4
Source File: WPrinterJob.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/** MediaSizeName / dmPaper */
private final int[] getWin32MediaAttrib() {
    int wid_ht[] = {0, 0};
    if (attributes != null) {
        Media media = (Media)attributes.get(Media.class);
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize ms = MediaSize.getMediaSizeForName(msn);
            if (ms != null) {
                wid_ht[0] = (int)(ms.getX(MediaSize.INCH) * 72.0);
                wid_ht[1] = (int)(ms.getY(MediaSize.INCH) * 72.0);
            }
        }
    }
    return wid_ht;
}
 
Example 5
Source File: WPrinterJob.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/** MediaSizeName / dmPaper */
private final int[] getWin32MediaAttrib() {
    int wid_ht[] = {0, 0};
    if (attributes != null) {
        Media media = (Media)attributes.get(Media.class);
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize ms = MediaSize.getMediaSizeForName(msn);
            if (ms != null) {
                wid_ht[0] = (int)(ms.getX(MediaSize.INCH) * 72.0);
                wid_ht[1] = (int)(ms.getY(MediaSize.INCH) * 72.0);
            }
        }
    }
    return wid_ht;
}
 
Example 6
Source File: WPrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/** MediaSizeName / dmPaper */
private final int[] getWin32MediaAttrib() {
    int wid_ht[] = {0, 0};
    if (attributes != null) {
        Media media = (Media)attributes.get(Media.class);
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize ms = MediaSize.getMediaSizeForName(msn);
            if (ms != null) {
                wid_ht[0] = (int)(ms.getX(MediaSize.INCH) * 72.0);
                wid_ht[1] = (int)(ms.getY(MediaSize.INCH) * 72.0);
            }
        }
    }
    return wid_ht;
}
 
Example 7
Source File: UnixPrintService.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private MediaPrintableArea[] getAllPrintableAreas() {

        if (mpas == null) {
            Media[] media = (Media[])getSupportedAttributeValues(Media.class,
                                                                 null, null);
            mpas = new MediaPrintableArea[media.length];
            for (int i=0; i< mpas.length; i++) {
                if (media[i] instanceof MediaSizeName) {
                    MediaSizeName msn = (MediaSizeName)media[i];
                    MediaSize mediaSize = MediaSize.getMediaSizeForName(msn);
                    if (mediaSize == null) {
                        mpas[i] = (MediaPrintableArea)
                            getDefaultAttributeValue(MediaPrintableArea.class);
                    } else {
                        mpas[i] = new MediaPrintableArea(0.25f, 0.25f,
                                        mediaSize.getX(MediaSize.INCH)-0.5f,
                                        mediaSize.getY(MediaSize.INCH)-0.5f,
                                        MediaSize.INCH);
                    }
                }
            }
        }
        MediaPrintableArea[] mpasCopy = new MediaPrintableArea[mpas.length];
        System.arraycopy(mpas, 0, mpasCopy, 0, mpas.length);
        return mpasCopy;
    }
 
Example 8
Source File: WPrinterJob.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** MediaSizeName / dmPaper */
private int[] getWin32MediaAttrib() {
    int wid_ht[] = {0, 0};
    if (attributes != null) {
        Media media = (Media)attributes.get(Media.class);
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize ms = MediaSize.getMediaSizeForName(msn);
            if (ms != null) {
                wid_ht[0] = (int)(ms.getX(MediaSize.INCH) * 72.0);
                wid_ht[1] = (int)(ms.getY(MediaSize.INCH) * 72.0);
            }
        }
    }
    return wid_ht;
}
 
Example 9
Source File: UnixPrintService.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private MediaPrintableArea[] getAllPrintableAreas() {

        if (mpas == null) {
            Media[] media = (Media[])getSupportedAttributeValues(Media.class,
                                                                 null, null);
            mpas = new MediaPrintableArea[media.length];
            for (int i=0; i< mpas.length; i++) {
                if (media[i] instanceof MediaSizeName) {
                    MediaSizeName msn = (MediaSizeName)media[i];
                    MediaSize mediaSize = MediaSize.getMediaSizeForName(msn);
                    if (mediaSize == null) {
                        mpas[i] = (MediaPrintableArea)
                            getDefaultAttributeValue(MediaPrintableArea.class);
                    } else {
                        mpas[i] = new MediaPrintableArea(0.25f, 0.25f,
                                        mediaSize.getX(MediaSize.INCH)-0.5f,
                                        mediaSize.getY(MediaSize.INCH)-0.5f,
                                        MediaSize.INCH);
                    }
                }
            }
        }
        MediaPrintableArea[] mpasCopy = new MediaPrintableArea[mpas.length];
        System.arraycopy(mpas, 0, mpasCopy, 0, mpas.length);
        return mpasCopy;
    }
 
Example 10
Source File: CustomMediaSizeName.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public CustomMediaSizeName(String name, String choice,
                           float width, float length) {
    super(nextValue(name));
    choiceName = choice;
    customEnumTable.add(this);
    mediaName = null;
    try {
        mediaName = MediaSize.findMedia(width, length,
                                        MediaSize.INCH);
    } catch (IllegalArgumentException iae) {
    }
    // The public API method finds a closest match even if it not
    // all that close. Here we want to be sure its *really* close.
    if (mediaName != null) {
        MediaSize sz = MediaSize.getMediaSizeForName(mediaName);
        if (sz == null) {
            mediaName = null;
        } else {
            float w = sz.getX(MediaSize.INCH);
            float h = sz.getY(MediaSize.INCH);
            float dw = Math.abs(w - width);
            float dh = Math.abs(h - length);
            if (dw > 0.1 || dh > 0.1) {
                mediaName = null;
            }
        }
    }
}
 
Example 11
Source File: CustomMediaSizeName.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public CustomMediaSizeName(String name, String choice,
                           float width, float length) {
    super(nextValue(name));
    choiceName = choice;
    customEnumTable.add(this);
    mediaName = null;
    try {
        mediaName = MediaSize.findMedia(width, length,
                                        MediaSize.INCH);
    } catch (IllegalArgumentException iae) {
    }
    // The public API method finds a closest match even if it not
    // all that close. Here we want to be sure its *really* close.
    if (mediaName != null) {
        MediaSize sz = MediaSize.getMediaSizeForName(mediaName);
        if (sz == null) {
            mediaName = null;
        } else {
            float w = sz.getX(MediaSize.INCH);
            float h = sz.getY(MediaSize.INCH);
            float dw = Math.abs(w - width);
            float dh = Math.abs(h - length);
            if (dw > 0.1 || dh > 0.1) {
                mediaName = null;
            }
        }
    }
}
 
Example 12
Source File: CustomMediaSizeName.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public CustomMediaSizeName(String name, String choice,
                           float width, float length) {
    super(nextValue(name));
    choiceName = choice;
    customEnumTable.add(this);
    mediaName = null;
    try {
        mediaName = MediaSize.findMedia(width, length,
                                        MediaSize.INCH);
    } catch (IllegalArgumentException iae) {
    }
    // The public API method finds a closest match even if it not
    // all that close. Here we want to be sure its *really* close.
    if (mediaName != null) {
        MediaSize sz = MediaSize.getMediaSizeForName(mediaName);
        if (sz == null) {
            mediaName = null;
        } else {
            float w = sz.getX(MediaSize.INCH);
            float h = sz.getY(MediaSize.INCH);
            float dw = Math.abs(w - width);
            float dh = Math.abs(h - length);
            if (dw > 0.1 || dh > 0.1) {
                mediaName = null;
            }
        }
    }
}
 
Example 13
Source File: CustomMediaSizeName.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public CustomMediaSizeName(String name, String choice,
                           float width, float length) {
    super(nextValue(name));
    choiceName = choice;
    customEnumTable.add(this);
    mediaName = null;
    try {
        mediaName = MediaSize.findMedia(width, length,
                                        MediaSize.INCH);
    } catch (IllegalArgumentException iae) {
    }
    // The public API method finds a closest match even if it not
    // all that close. Here we want to be sure its *really* close.
    if (mediaName != null) {
        MediaSize sz = MediaSize.getMediaSizeForName(mediaName);
        if (sz == null) {
            mediaName = null;
        } else {
            float w = sz.getX(MediaSize.INCH);
            float h = sz.getY(MediaSize.INCH);
            float dw = Math.abs(w - width);
            float dh = Math.abs(h - length);
            if (dw > 0.1 || dh > 0.1) {
                mediaName = null;
            }
        }
    }
}
 
Example 14
Source File: CustomMediaSizeName.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public CustomMediaSizeName(String name, String choice,
                           float width, float length) {
    super(nextValue(name));
    choiceName = choice;
    customEnumTable.add(this);
    mediaName = null;
    try {
        mediaName = MediaSize.findMedia(width, length,
                                        MediaSize.INCH);
    } catch (IllegalArgumentException iae) {
    }
    // The public API method finds a closest match even if it not
    // all that close. Here we want to be sure its *really* close.
    if (mediaName != null) {
        MediaSize sz = MediaSize.getMediaSizeForName(mediaName);
        if (sz == null) {
            mediaName = null;
        } else {
            float w = sz.getX(MediaSize.INCH);
            float h = sz.getY(MediaSize.INCH);
            float dw = Math.abs(w - width);
            float dh = Math.abs(h - length);
            if (dw > 0.1 || dh > 0.1) {
                mediaName = null;
            }
        }
    }
}
 
Example 15
Source File: RasterPrinterJob.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The passed in PageFormat will be copied and altered to describe
 * the default page size and orientation of the PrinterJob's
 * current printer.
 * Platform subclasses which can access the actual default paper size
 * for a printer may override this method.
 */
public PageFormat defaultPage(PageFormat page) {
    PageFormat newPage = (PageFormat)page.clone();
    newPage.setOrientation(PageFormat.PORTRAIT);
    Paper newPaper = new Paper();
    double ptsPerInch = 72.0;
    double w, h;
    Media media = null;

    PrintService service = getPrintService();
    if (service != null) {
        MediaSize size;
        media =
            (Media)service.getDefaultAttributeValue(Media.class);

        if (media instanceof MediaSizeName &&
           ((size = MediaSize.getMediaSizeForName((MediaSizeName)media)) !=
            null)) {
            w =  size.getX(MediaSize.INCH) * ptsPerInch;
            h =  size.getY(MediaSize.INCH) * ptsPerInch;
            newPaper.setSize(w, h);
            newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                      w - 2.0*ptsPerInch,
                                      h - 2.0*ptsPerInch);
            newPage.setPaper(newPaper);
            return newPage;

        }
    }

    /* Default to A4 paper outside North America.
     */
    String defaultCountry = Locale.getDefault().getCountry();
    if (!Locale.getDefault().equals(Locale.ENGLISH) && // ie "C"
        defaultCountry != null &&
        !defaultCountry.equals(Locale.US.getCountry()) &&
        !defaultCountry.equals(Locale.CANADA.getCountry())) {

        double mmPerInch = 25.4;
        w = Math.rint((210.0*ptsPerInch)/mmPerInch);
        h = Math.rint((297.0*ptsPerInch)/mmPerInch);
        newPaper.setSize(w, h);
        newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                  w - 2.0*ptsPerInch,
                                  h - 2.0*ptsPerInch);
    }

    newPage.setPaper(newPaper);

    return newPage;
}
 
Example 16
Source File: PrinterJob.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates a <code>PageFormat</code> with values consistent with those
 * supported by the current <code>PrintService</code> for this job
 * (ie the value returned by <code>getPrintService()</code>) and media,
 * printable area and orientation contained in <code>attributes</code>.
 * <p>
 * Calling this method does not update the job.
 * It is useful for clients that have a set of attributes obtained from
 * <code>printDialog(PrintRequestAttributeSet attributes)</code>
 * and need a PageFormat to print a Pageable object.
 * @param attributes a set of printing attributes, for example obtained
 * from calling printDialog. If <code>attributes</code> is null a default
 * PageFormat is returned.
 * @return a <code>PageFormat</code> whose settings conform with
 * those of the current service and the specified attributes.
 * @since 1.6
 */
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) {

    PrintService service = getPrintService();
    PageFormat pf = defaultPage();

    if (service == null || attributes == null) {
        return pf;
    }

    Media media = (Media)attributes.get(Media.class);
    MediaPrintableArea mpa =
        (MediaPrintableArea)attributes.get(MediaPrintableArea.class);
    OrientationRequested orientReq =
       (OrientationRequested)attributes.get(OrientationRequested.class);

    if (media == null && mpa == null && orientReq == null) {
       return pf;
    }
    Paper paper = pf.getPaper();

    /* If there's a media but no media printable area, we can try
     * to retrieve the default value for mpa and use that.
     */
    if (mpa == null && media != null &&
        service.isAttributeCategorySupported(MediaPrintableArea.class)) {
        Object mpaVals =
            service.getSupportedAttributeValues(MediaPrintableArea.class,
                                                null, attributes);
        if (mpaVals instanceof MediaPrintableArea[] &&
            ((MediaPrintableArea[])mpaVals).length > 0) {
            mpa = ((MediaPrintableArea[])mpaVals)[0];
        }
    }

    if (media != null &&
        service.isAttributeValueSupported(media, null, attributes)) {
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize msz = MediaSize.getMediaSizeForName(msn);
            if (msz != null) {
                double inch = 72.0;
                double paperWid = msz.getX(MediaSize.INCH) * inch;
                double paperHgt = msz.getY(MediaSize.INCH) * inch;
                paper.setSize(paperWid, paperHgt);
                if (mpa == null) {
                    paper.setImageableArea(inch, inch,
                                           paperWid-2*inch,
                                           paperHgt-2*inch);
                }
            }
        }
    }

    if (mpa != null &&
        service.isAttributeValueSupported(mpa, null, attributes)) {
        float [] printableArea =
            mpa.getPrintableArea(MediaPrintableArea.INCH);
        for (int i=0; i < printableArea.length; i++) {
            printableArea[i] = printableArea[i]*72.0f;
        }
        paper.setImageableArea(printableArea[0], printableArea[1],
                               printableArea[2], printableArea[3]);
    }

    if (orientReq != null &&
        service.isAttributeValueSupported(orientReq, null, attributes)) {
        int orient;
        if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
            orient = PageFormat.REVERSE_LANDSCAPE;
        } else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
            orient = PageFormat.LANDSCAPE;
        } else {
            orient = PageFormat.PORTRAIT;
        }
        pf.setOrientation(orient);
    }

    pf.setPaper(paper);
    pf = validatePage(pf);
    return pf;
}
 
Example 17
Source File: PrinterJob.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates a <code>PageFormat</code> with values consistent with those
 * supported by the current <code>PrintService</code> for this job
 * (ie the value returned by <code>getPrintService()</code>) and media,
 * printable area and orientation contained in <code>attributes</code>.
 * <p>
 * Calling this method does not update the job.
 * It is useful for clients that have a set of attributes obtained from
 * <code>printDialog(PrintRequestAttributeSet attributes)</code>
 * and need a PageFormat to print a Pageable object.
 * @param attributes a set of printing attributes, for example obtained
 * from calling printDialog. If <code>attributes</code> is null a default
 * PageFormat is returned.
 * @return a <code>PageFormat</code> whose settings conform with
 * those of the current service and the specified attributes.
 * @since 1.6
 */
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) {

    PrintService service = getPrintService();
    PageFormat pf = defaultPage();

    if (service == null || attributes == null) {
        return pf;
    }

    Media media = (Media)attributes.get(Media.class);
    MediaPrintableArea mpa =
        (MediaPrintableArea)attributes.get(MediaPrintableArea.class);
    OrientationRequested orientReq =
       (OrientationRequested)attributes.get(OrientationRequested.class);

    if (media == null && mpa == null && orientReq == null) {
       return pf;
    }
    Paper paper = pf.getPaper();

    /* If there's a media but no media printable area, we can try
     * to retrieve the default value for mpa and use that.
     */
    if (mpa == null && media != null &&
        service.isAttributeCategorySupported(MediaPrintableArea.class)) {
        Object mpaVals =
            service.getSupportedAttributeValues(MediaPrintableArea.class,
                                                null, attributes);
        if (mpaVals instanceof MediaPrintableArea[] &&
            ((MediaPrintableArea[])mpaVals).length > 0) {
            mpa = ((MediaPrintableArea[])mpaVals)[0];
        }
    }

    if (media != null &&
        service.isAttributeValueSupported(media, null, attributes)) {
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize msz = MediaSize.getMediaSizeForName(msn);
            if (msz != null) {
                double inch = 72.0;
                double paperWid = msz.getX(MediaSize.INCH) * inch;
                double paperHgt = msz.getY(MediaSize.INCH) * inch;
                paper.setSize(paperWid, paperHgt);
                if (mpa == null) {
                    paper.setImageableArea(inch, inch,
                                           paperWid-2*inch,
                                           paperHgt-2*inch);
                }
            }
        }
    }

    if (mpa != null &&
        service.isAttributeValueSupported(mpa, null, attributes)) {
        float [] printableArea =
            mpa.getPrintableArea(MediaPrintableArea.INCH);
        for (int i=0; i < printableArea.length; i++) {
            printableArea[i] = printableArea[i]*72.0f;
        }
        paper.setImageableArea(printableArea[0], printableArea[1],
                               printableArea[2], printableArea[3]);
    }

    if (orientReq != null &&
        service.isAttributeValueSupported(orientReq, null, attributes)) {
        int orient;
        if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
            orient = PageFormat.REVERSE_LANDSCAPE;
        } else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
            orient = PageFormat.LANDSCAPE;
        } else {
            orient = PageFormat.PORTRAIT;
        }
        pf.setOrientation(orient);
    }

    pf.setPaper(paper);
    pf = validatePage(pf);
    return pf;
}
 
Example 18
Source File: RasterPrinterJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The passed in PageFormat will be copied and altered to describe
 * the default page size and orientation of the PrinterJob's
 * current printer.
 * Platform subclasses which can access the actual default paper size
 * for a printer may override this method.
 */
public PageFormat defaultPage(PageFormat page) {
    PageFormat newPage = (PageFormat)page.clone();
    newPage.setOrientation(PageFormat.PORTRAIT);
    Paper newPaper = new Paper();
    double ptsPerInch = 72.0;
    double w, h;
    Media media = null;

    PrintService service = getPrintService();
    if (service != null) {
        MediaSize size;
        media =
            (Media)service.getDefaultAttributeValue(Media.class);

        if (media instanceof MediaSizeName &&
           ((size = MediaSize.getMediaSizeForName((MediaSizeName)media)) !=
            null)) {
            w =  size.getX(MediaSize.INCH) * ptsPerInch;
            h =  size.getY(MediaSize.INCH) * ptsPerInch;
            newPaper.setSize(w, h);
            newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                      w - 2.0*ptsPerInch,
                                      h - 2.0*ptsPerInch);
            newPage.setPaper(newPaper);
            return newPage;

        }
    }

    /* Default to A4 paper outside North America.
     */
    String defaultCountry = Locale.getDefault().getCountry();
    if (!Locale.getDefault().equals(Locale.ENGLISH) && // ie "C"
        defaultCountry != null &&
        !defaultCountry.equals(Locale.US.getCountry()) &&
        !defaultCountry.equals(Locale.CANADA.getCountry())) {

        double mmPerInch = 25.4;
        w = Math.rint((210.0*ptsPerInch)/mmPerInch);
        h = Math.rint((297.0*ptsPerInch)/mmPerInch);
        newPaper.setSize(w, h);
        newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                  w - 2.0*ptsPerInch,
                                  h - 2.0*ptsPerInch);
    }

    newPage.setPaper(newPaper);

    return newPage;
}
 
Example 19
Source File: PrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calculates a <code>PageFormat</code> with values consistent with those
 * supported by the current <code>PrintService</code> for this job
 * (ie the value returned by <code>getPrintService()</code>) and media,
 * printable area and orientation contained in <code>attributes</code>.
 * <p>
 * Calling this method does not update the job.
 * It is useful for clients that have a set of attributes obtained from
 * <code>printDialog(PrintRequestAttributeSet attributes)</code>
 * and need a PageFormat to print a Pageable object.
 * @param attributes a set of printing attributes, for example obtained
 * from calling printDialog. If <code>attributes</code> is null a default
 * PageFormat is returned.
 * @return a <code>PageFormat</code> whose settings conform with
 * those of the current service and the specified attributes.
 * @since 1.6
 */
public PageFormat getPageFormat(PrintRequestAttributeSet attributes) {

    PrintService service = getPrintService();
    PageFormat pf = defaultPage();

    if (service == null || attributes == null) {
        return pf;
    }

    Media media = (Media)attributes.get(Media.class);
    MediaPrintableArea mpa =
        (MediaPrintableArea)attributes.get(MediaPrintableArea.class);
    OrientationRequested orientReq =
       (OrientationRequested)attributes.get(OrientationRequested.class);

    if (media == null && mpa == null && orientReq == null) {
       return pf;
    }
    Paper paper = pf.getPaper();

    /* If there's a media but no media printable area, we can try
     * to retrieve the default value for mpa and use that.
     */
    if (mpa == null && media != null &&
        service.isAttributeCategorySupported(MediaPrintableArea.class)) {
        Object mpaVals =
            service.getSupportedAttributeValues(MediaPrintableArea.class,
                                                null, attributes);
        if (mpaVals instanceof MediaPrintableArea[] &&
            ((MediaPrintableArea[])mpaVals).length > 0) {
            mpa = ((MediaPrintableArea[])mpaVals)[0];
        }
    }

    if (media != null &&
        service.isAttributeValueSupported(media, null, attributes)) {
        if (media instanceof MediaSizeName) {
            MediaSizeName msn = (MediaSizeName)media;
            MediaSize msz = MediaSize.getMediaSizeForName(msn);
            if (msz != null) {
                double inch = 72.0;
                double paperWid = msz.getX(MediaSize.INCH) * inch;
                double paperHgt = msz.getY(MediaSize.INCH) * inch;
                paper.setSize(paperWid, paperHgt);
                if (mpa == null) {
                    paper.setImageableArea(inch, inch,
                                           paperWid-2*inch,
                                           paperHgt-2*inch);
                }
            }
        }
    }

    if (mpa != null &&
        service.isAttributeValueSupported(mpa, null, attributes)) {
        float [] printableArea =
            mpa.getPrintableArea(MediaPrintableArea.INCH);
        for (int i=0; i < printableArea.length; i++) {
            printableArea[i] = printableArea[i]*72.0f;
        }
        paper.setImageableArea(printableArea[0], printableArea[1],
                               printableArea[2], printableArea[3]);
    }

    if (orientReq != null &&
        service.isAttributeValueSupported(orientReq, null, attributes)) {
        int orient;
        if (orientReq.equals(OrientationRequested.REVERSE_LANDSCAPE)) {
            orient = PageFormat.REVERSE_LANDSCAPE;
        } else if (orientReq.equals(OrientationRequested.LANDSCAPE)) {
            orient = PageFormat.LANDSCAPE;
        } else {
            orient = PageFormat.PORTRAIT;
        }
        pf.setOrientation(orient);
    }

    pf.setPaper(paper);
    pf = validatePage(pf);
    return pf;
}
 
Example 20
Source File: RasterPrinterJob.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The passed in PageFormat will be copied and altered to describe
 * the default page size and orientation of the PrinterJob's
 * current printer.
 * Platform subclasses which can access the actual default paper size
 * for a printer may override this method.
 */
public PageFormat defaultPage(PageFormat page) {
    PageFormat newPage = (PageFormat)page.clone();
    newPage.setOrientation(PageFormat.PORTRAIT);
    Paper newPaper = new Paper();
    double ptsPerInch = 72.0;
    double w, h;
    Media media = null;

    PrintService service = getPrintService();
    if (service != null) {
        MediaSize size;
        media =
            (Media)service.getDefaultAttributeValue(Media.class);

        if (media instanceof MediaSizeName &&
           ((size = MediaSize.getMediaSizeForName((MediaSizeName)media)) !=
            null)) {
            w =  size.getX(MediaSize.INCH) * ptsPerInch;
            h =  size.getY(MediaSize.INCH) * ptsPerInch;
            newPaper.setSize(w, h);
            newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                      w - 2.0*ptsPerInch,
                                      h - 2.0*ptsPerInch);
            newPage.setPaper(newPaper);
            return newPage;

        }
    }

    /* Default to A4 paper outside North America.
     */
    String defaultCountry = Locale.getDefault().getCountry();
    if (!Locale.getDefault().equals(Locale.ENGLISH) && // ie "C"
        defaultCountry != null &&
        !defaultCountry.equals(Locale.US.getCountry()) &&
        !defaultCountry.equals(Locale.CANADA.getCountry())) {

        double mmPerInch = 25.4;
        w = Math.rint((210.0*ptsPerInch)/mmPerInch);
        h = Math.rint((297.0*ptsPerInch)/mmPerInch);
        newPaper.setSize(w, h);
        newPaper.setImageableArea(ptsPerInch, ptsPerInch,
                                  w - 2.0*ptsPerInch,
                                  h - 2.0*ptsPerInch);
    }

    newPage.setPaper(newPaper);

    return newPage;
}