javax.print.attribute.standard.MediaSizeName Java Examples

The following examples show how to use javax.print.attribute.standard.MediaSizeName. 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: WrongPaperForBookPrintingTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    Book pageable = new Book();
    pageable.append(new WrongPaperForBookPrintingTest(), pf);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPageable(pageable);
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example #2
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 #3
Source File: PrintJob2D.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static MediaType unMapMedia(MediaSizeName mSize) {
    MediaType media = null;

    // JAVAXSIZES.length and SIZES.length must be equal!
    // Attempt to recover by getting the smaller size.
    int length = Math.min(SIZES.length, JAVAXSIZES.length);

    for (int i=0; i < length; i++) {
        if (JAVAXSIZES[i] == mSize) {
            if (SIZES[i] != null) {
                media = SIZES[i];
                break;
            }
        }
    }
    return media;
}
 
Example #4
Source File: WPrinterJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private void addPaperSize(PrintRequestAttributeSet aset,
                          int dmIndex, int width, int length) {

    if (aset == null) {
        return;
    }
    MediaSizeName msn =
       ((Win32PrintService)myService).findWin32Media(dmIndex);
    if (msn == null) {
        msn = ((Win32PrintService)myService).
            findMatchingMediaSizeNameMM((float)width, (float)length);
    }

    if (msn != null) {
        aset.add(msn);
    }
}
 
Example #5
Source File: WPrinterJob.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private void addPaperSize(PrintRequestAttributeSet aset,
                          int dmIndex, int width, int length) {

    if (aset == null) {
        return;
    }
    MediaSizeName msn =
       ((Win32PrintService)myService).findWin32Media(dmIndex);
    if (msn == null) {
        msn = ((Win32PrintService)myService).
            findMatchingMediaSizeNameMM((float)width, (float)length);
    }

    if (msn != null) {
        aset.add(msn);
    }
}
 
Example #6
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 #7
Source File: PrintServiceStub.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public PrintServiceStub(String name) {
    _name = name;
    _flavors = new HashSet<DocFlavor>();
    _flavors.add(DocFlavor.SERVICE_FORMATTED.PAGEABLE);
    _flavors.add(DocFlavor.SERVICE_FORMATTED.PRINTABLE);
    _attributes = new HashMap<>();
    _attributes.put(PrinterName.class, new PrinterName(name, null));
    _attributes.put(PrinterState.class, PrinterState.IDLE);
    _attributes.put(PrinterInfo.class, new PrinterInfo("Custom location",
            null));
    _attributes.put(PrinterIsAcceptingJobs.class,
            PrinterIsAcceptingJobs.ACCEPTING_JOBS);
    _attributes.put(PrinterMakeAndModel.class, new PrinterMakeAndModel(
            "Custom printer", null));
    _attributes.put(Media.class, new Media[] { MediaSizeName.ISO_A4 });
}
 
Example #8
Source File: Win32PrintService.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public MediaSizeName findMatchingMediaSizeNameMM (float w, float h){
    if (predefMedia != null) {
        for (int k=0; k<predefMedia.length;k++) {
            if (predefMedia[k] == null) {
                continue;
            }

            if (isSameSize(predefMedia[k].getX(MediaSize.MM),
                           predefMedia[k].getY(MediaSize.MM),
                           w, h)) {
              return predefMedia[k].getMediaSizeName();
            }
        }
    }
    return null;
}
 
Example #9
Source File: Win32PrintService.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public MediaSizeName findMatchingMediaSizeNameMM (float w, float h){
    if (predefMedia != null) {
        for (int k=0; k<predefMedia.length;k++) {
            if (predefMedia[k] == null) {
                continue;
            }

            if (isSameSize(predefMedia[k].getX(MediaSize.MM),
                           predefMedia[k].getY(MediaSize.MM),
                           w, h)) {
              return predefMedia[k].getMediaSizeName();
            }
        }
    }
    return null;
}
 
Example #10
Source File: WrongPaperPrintingTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static void doTest() {
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(Chromaticity.MONOCHROME);

    MediaSize isoA5Size = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A5);
    float[] size = isoA5Size.getSize(Size2DSyntax.INCH);
    Paper paper = new Paper();
    paper.setSize(size[0] * 72.0, size[1] * 72.0);
    paper.setImageableArea(0.0, 0.0, size[0] * 72.0, size[1] * 72.0);
    PageFormat pf = new PageFormat();
    pf.setPaper(paper);

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new WrongPaperPrintingTest(), job.validatePage(pf));
    if (job.printDialog()) {
        try {
            job.print(aset);
        } catch (PrinterException pe) {
            throw new RuntimeException(pe);
        }
    }
}
 
Example #11
Source File: Win32PrintService.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public MediaSizeName findMatchingMediaSizeNameMM (float w, float h){
    if (predefMedia != null) {
        for (int k=0; k<predefMedia.length;k++) {
            if (predefMedia[k] == null) {
                continue;
            }

            if (isSameSize(predefMedia[k].getX(MediaSize.MM),
                           predefMedia[k].getY(MediaSize.MM),
                           w, h)) {
              return predefMedia[k].getMediaSizeName();
            }
        }
    }
    return null;
}
 
Example #12
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 #13
Source File: Win32PrintService.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public MediaSizeName findWin32Media(int dmIndex) {
    if (dmIndex >= 1 && dmIndex <= dmPaperToPrintService.length) {
        return dmPaperToPrintService[dmIndex - 1];
    }
    switch(dmIndex) {
        /* matching media sizes with indices beyond
           dmPaperToPrintService's length */
        case DMPAPER_A2:
            return MediaSizeName.ISO_A2;
        case DMPAPER_A6:
            return MediaSizeName.ISO_A6;
        case DMPAPER_B6_JIS:
            return MediaSizeName.JIS_B6;
        default:
            return null;
    }
}
 
Example #14
Source File: WPrinterJob.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private void addPaperSize(PrintRequestAttributeSet aset,
                          int dmIndex, int width, int length) {

    if (aset == null) {
        return;
    }
    MediaSizeName msn =
       ((Win32PrintService)myService).findWin32Media(dmIndex);
    if (msn == null) {
        msn = ((Win32PrintService)myService).
            findMatchingMediaSizeNameMM((float)width, (float)length);
    }

    if (msn != null) {
        aset.add(msn);
    }
}
 
Example #15
Source File: UnixPrintService.java    From dragonwell8_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 #16
Source File: WPrinterJob.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void addPaperSize(PrintRequestAttributeSet aset,
                          int dmIndex, int width, int length) {

    if (aset == null) {
        return;
    }
    MediaSizeName msn =
       ((Win32PrintService)myService).findWin32Media(dmIndex);
    if (msn == null) {
        msn = ((Win32PrintService)myService).
            findMatchingMediaSizeNameMM((float)width, (float)length);
    }

    if (msn != null) {
        aset.add(msn);
    }
}
 
Example #17
Source File: MediaMappingsTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
    MediaSize sizeA = MediaSize.getMediaSizeForName(MediaSizeName.A);
    new MediaSize(1.0f, 2.0f, MediaSize.MM, MediaSizeName.A);
    if (!sizeA.equals(MediaSize.getMediaSizeForName(MediaSizeName.A))) {
         throw new RuntimeException("mapping changed");
    }
    MediaSize sizeB = MediaSize.getMediaSizeForName(MediaSizeName.B);
    new MediaSize(1, 2, MediaSize.MM, MediaSizeName.B);
    if (!sizeB.equals(MediaSize.getMediaSizeForName(MediaSizeName.B))) {
         throw new RuntimeException("mapping changed");
    }
}
 
Example #18
Source File: WPrinterJob.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void setWin32MediaAttrib(Attribute attr) {
    if (!(attr instanceof MediaSizeName)) {
        return;
    }
    MediaSizeName msn = (MediaSizeName)attr;
    mAttMediaSizeName = ((Win32PrintService)myService).findPaperID(msn);
}
 
Example #19
Source File: Win32PrintService.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public int findPaperID(MediaSizeName msn) {
    if (msn instanceof Win32MediaSize) {
        Win32MediaSize winMedia = (Win32MediaSize)msn;
        return winMedia.getDMPaper();
    } else {
        for (int id=0; id<dmPaperToPrintService.length;id++) {
            if (dmPaperToPrintService[id].equals(msn)) {
                return id+1; // DMPAPER_LETTER == 1
            }
        }
        if (msn.equals(MediaSizeName.ISO_A2)) {
            return DMPAPER_A2;
        }
        else if (msn.equals(MediaSizeName.ISO_A6)) {
            return DMPAPER_A6;
        }
        else if (msn.equals(MediaSizeName.JIS_B6)) {
            return DMPAPER_B6_JIS;
        }
    }

    // If not found in predefined Windows ID, then we search through
    // the returned IDs of the driver because they can define their own
    // unique IDs.
    initMedia();

    if ((idList != null) && (mediaSizes != null) &&
        (idList.size() == mediaSizes.length)) {
        for (int i=0; i< idList.size(); i++) {
            if (mediaSizes[i].getMediaSizeName() == msn) {
                return ((Integer)idList.get(i)).intValue();
            }
        }
    }
    return 0;
}
 
Example #20
Source File: RasterPrinterJob.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected MediaSize getMediaSize(Media media, PrintService service,
        PageFormat page) {
    if (media == null) {
        media = (Media)service.getDefaultAttributeValue(Media.class);
    }
    if (!(media instanceof MediaSizeName)) {
        media = MediaSizeName.NA_LETTER;
    }
    MediaSize size = MediaSize.getMediaSizeForName((MediaSizeName) media);
    return size != null ? size : MediaSize.NA.LETTER;
}
 
Example #21
Source File: UnixPrintService.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private boolean isSupportedMedia(MediaSizeName msn) {
    for (int i=0; i<mediaSizes.length; i++) {
        if (msn.equals(mediaSizes[i])) {
            return true;
        }
    }
    return false;
}
 
Example #22
Source File: WPrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void setWin32MediaAttrib(Attribute attr) {
    if (!(attr instanceof MediaSizeName)) {
        return;
    }
    MediaSizeName msn = (MediaSizeName)attr;
    mAttMediaSizeName = ((Win32PrintService)myService).findPaperID(msn);
}
 
Example #23
Source File: UnixPrintService.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private boolean isSupportedMedia(MediaSizeName msn) {
    for (int i=0; i<mediaSizes.length; i++) {
        if (msn.equals(mediaSizes[i])) {
            return true;
        }
    }
    return false;
}
 
Example #24
Source File: UnixPrintService.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean isSupportedMedia(MediaSizeName msn) {
    for (int i=0; i<mediaSizes.length; i++) {
        if (msn.equals(mediaSizes[i])) {
            return true;
        }
    }
    return false;
}
 
Example #25
Source File: Win32PrintService.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean isSupportedMedia(MediaSizeName msn) {

        initMedia();

        if (mediaSizeNames != null) {
            for (int i=0; i<mediaSizeNames.length; i++) {
                if (msn.equals(mediaSizeNames[i])) {
                    return true;
                }
            }
        }
        return false;
    }
 
Example #26
Source File: MediaMappingsTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
    MediaSize sizeA = MediaSize.getMediaSizeForName(MediaSizeName.A);
    new MediaSize(1.0f, 2.0f, MediaSize.MM, MediaSizeName.A);
    if (!sizeA.equals(MediaSize.getMediaSizeForName(MediaSizeName.A))) {
         throw new RuntimeException("mapping changed");
    }
    MediaSize sizeB = MediaSize.getMediaSizeForName(MediaSizeName.B);
    new MediaSize(1, 2, MediaSize.MM, MediaSizeName.B);
    if (!sizeB.equals(MediaSize.getMediaSizeForName(MediaSizeName.B))) {
         throw new RuntimeException("mapping changed");
    }
}
 
Example #27
Source File: Win32PrintService.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public int findPaperID(MediaSizeName msn) {
    if (msn instanceof Win32MediaSize) {
        Win32MediaSize winMedia = (Win32MediaSize)msn;
        return winMedia.getDMPaper();
    } else {
        for (int id=0; id<dmPaperToPrintService.length;id++) {
            if (dmPaperToPrintService[id].equals(msn)) {
                return id+1; // DMPAPER_LETTER == 1
            }
        }
        if (msn.equals(MediaSizeName.ISO_A2)) {
            return DMPAPER_A2;
        }
        else if (msn.equals(MediaSizeName.ISO_A6)) {
            return DMPAPER_A6;
        }
        else if (msn.equals(MediaSizeName.JIS_B6)) {
            return DMPAPER_B6_JIS;
        }
    }

    // If not found in predefined Windows ID, then we search through
    // the returned IDs of the driver because they can define their own
    // unique IDs.
    initMedia();

    if ((idList != null) && (mediaSizes != null) &&
        (idList.size() == mediaSizes.length)) {
        for (int i=0; i< idList.size(); i++) {
            if (mediaSizes[i].getMediaSizeName() == msn) {
                return ((Integer)idList.get(i)).intValue();
            }
        }
    }
    return 0;
}
 
Example #28
Source File: Win32PrintService.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private boolean isSupportedMedia(MediaSizeName msn) {

        initMedia();

        if (mediaSizeNames != null) {
            for (int i=0; i<mediaSizeNames.length; i++) {
                if (msn.equals(mediaSizeNames[i])) {
                    return true;
                }
            }
        }
        return false;
    }
 
Example #29
Source File: PSStreamPrintService.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private boolean isSupportedMedia(MediaSizeName msn) {
    for (int i=0; i<mediaSizes.length; i++) {
        if (msn.equals(mediaSizes[i])) {
            return true;
        }
    }
    return false;
}
 
Example #30
Source File: PrintJob2D.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static MediaSizeName mapMedia(MediaType mType) {
    MediaSizeName media = null;

    // JAVAXSIZES.length and SIZES.length must be equal!
    // Attempt to recover by getting the smaller size.
    int length = Math.min(SIZES.length, JAVAXSIZES.length);

    for (int i=0; i < length; i++) {
        if (SIZES[i] == mType) {
            if ((JAVAXSIZES[i] != null) &&
                MediaSize.getMediaSizeForName(JAVAXSIZES[i]) != null) {
                media = JAVAXSIZES[i];
                break;
            } else {
                /* create Custom Media */
                media = new CustomMediaSizeName(SIZES[i].toString());

                float w = (float)Math.rint(WIDTHS[i]  / 72.0);
                float h = (float)Math.rint(LENGTHS[i] / 72.0);
                if (w > 0.0 && h > 0.0) {
                    // add new created MediaSize to our static map
                    // so it will be found when we call findMedia
                    new MediaSize(w, h, Size2DSyntax.INCH, media);
                }

                break;
            }
        }
    }
    return media;
}