Java Code Examples for javax.print.attribute.PrintRequestAttributeSet#add()

The following examples show how to use javax.print.attribute.PrintRequestAttributeSet#add() . 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: ImageableAreaTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static void printWithoutPrintDialog() {

        final JTable table = createAuthorTable(42);
        PrintRequestAttributeSet pras
                = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));

        try {

            boolean printAccepted = table.print(JTable.PrintMode.FIT_WIDTH,
                    new MessageFormat("Author Table"),
                    new MessageFormat("Page - {0}"),
                    false, pras, false);

            closeFrame();
            if (!printAccepted) {
                throw new RuntimeException("User cancels the printer job!");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
Example 2
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 3
Source File: WrongPaperPrintingTest.java    From openjdk-jdk9 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 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: ImageableAreaTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void printWithoutPrintDialog() {

        final JTable table = createAuthorTable(42);
        PrintRequestAttributeSet pras
                = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));

        try {

            boolean printAccepted = table.print(JTable.PrintMode.FIT_WIDTH,
                    new MessageFormat("Author Table"),
                    new MessageFormat("Page - {0}"),
                    false, pras, false);

            closeFrame();
            if (!printAccepted) {
                throw new RuntimeException("User cancels the printer job!");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
Example 6
Source File: WPrinterJob.java    From openjdk-jdk8u 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 7
Source File: PageDlgStackOverflowTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) {
    PrinterJob job = PrinterJob.getPrinterJob();
    if (job == null) {
        return;
    }
    PrintRequestAttributeSet pSet =
         new HashPrintRequestAttributeSet();
    pSet.add(DialogTypeSelection.NATIVE);
    job.printDialog(pSet);
    try {
        job.pageDialog(pSet);
    } catch (StackOverflowError e) {
        throw new RuntimeException("StackOverflowError is thrown");
    }
}
 
Example 8
Source File: LandscapeStackOverflow.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static final void main( String[] parameters ) {
    PrinterJob printjob = PrinterJob.getPrinterJob();
    printjob.setJobName( "Test Print Job" );

    PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
    attributes.add( OrientationRequested.LANDSCAPE );

    try {
        printjob.setPrintable( new Painter() );
        printjob.print( attributes );
    } catch( PrinterException exception ) {
        exception.printStackTrace();
    }
}
 
Example 9
Source File: PrintUtilities.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Sets the print resolution.
 *
 * @param set        The {@link PrintRequestAttributeSet} to use.
 * @param resolution The new print resolution.
 */
public static void setResolution(PrintRequestAttributeSet set, PrinterResolution resolution) {
    if (resolution != null) {
        set.add(resolution);
    } else {
        set.remove(PrinterResolution.class);
    }
}
 
Example 10
Source File: DlgAttrsBug.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void printTest() {
    PrinterJob job = PrinterJob.getPrinterJob();
    if (job.getPrintService() == null) {
        System.out.println("No printers. Test cannot continue");
        return;
    }
    job.setPrintable(new DlgAttrsBug());
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new Copies(5));
    aset.add(new PageRanges(3,4));
    aset.add(DialogTypeSelection.NATIVE);
    job.printDialog(aset);
}
 
Example 11
Source File: DlgAttrsBug.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static void printTest() {
    PrinterJob job = PrinterJob.getPrinterJob();
    if (job.getPrintService() == null) {
        System.out.println("No printers. Test cannot continue");
        return;
    }
    job.setPrintable(new DlgAttrsBug());
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(new Copies(5));
    aset.add(new PageRanges(3,4));
    aset.add(DialogTypeSelection.NATIVE);
    job.printDialog(aset);
}
 
Example 12
Source File: ImageableAreaTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void printWithCustomImageareaSize() {
    final JTable table = createAuthorTable(18);
    PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
    printAttributes.add(DialogTypeSelection.NATIVE);
    printAttributes.add(new Copies(1));
    printAttributes.add(new MediaPrintableArea(
            0.25f, 0.25f, 8.0f, 5.0f, MediaPrintableArea.INCH));
    Printable printable = table.getPrintable(
            JTable.PrintMode.NORMAL,
            new MessageFormat("Author Table"),
            new MessageFormat("Page - {0}")
    );

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);

    boolean printAccepted = job.printDialog(printAttributes);
    if (printAccepted) {
        try {
            job.print(printAttributes);
            closeFrame();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        throw new RuntimeException("User cancels the printer job!");
    }
}
 
Example 13
Source File: PrintDlgPageable.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void printTest() {
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageableHandler handler = new PageableHandler();
    pj.setPageable(handler);

    PrintRequestAttributeSet pSet =  new HashPrintRequestAttributeSet();
    pSet.add(DialogTypeSelection.COMMON);
    pj.printDialog(pSet);
}
 
Example 14
Source File: WPrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void setSidesAttrib(Attribute attr,
                            PrintRequestAttributeSet set) {
    setSidesAttrib(attr);
    set.add(attr);
}
 
Example 15
Source File: WPrinterJob.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void setSidesAttrib(Attribute attr,
                            PrintRequestAttributeSet set) {
    setSidesAttrib(attr);
    set.add(attr);
}
 
Example 16
Source File: WPrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void setSidesAttrib(Attribute attr,
                            PrintRequestAttributeSet set) {
    setSidesAttrib(attr);
    set.add(attr);
}
 
Example 17
Source File: WPrinterJob.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private final void setJobAttributes(PrintRequestAttributeSet attributes,
                                    int fields, int values,
                                    short copies,
                                    short dmPaperSize,
                                    short dmPaperWidth,
                                    short dmPaperLength,
                                    short dmDefaultSource,
                                    short xRes,
                                    short yRes) {

    if (attributes == null) {
        return;
    }

    if ((fields & DM_COPIES) != 0) {
        attributes.add(new Copies(copies));
    }

    if ((fields & DM_COLLATE) != 0) {
        if ((values & SET_COLLATED) != 0) {
            attributes.add(SheetCollate.COLLATED);
        } else {
            attributes.add(SheetCollate.UNCOLLATED);
        }
    }

    if ((fields & DM_ORIENTATION) != 0) {
        if ((values & SET_ORIENTATION) != 0) {
            attributes.add(OrientationRequested.LANDSCAPE);
        } else {
            attributes.add(OrientationRequested.PORTRAIT);
        }
    }

    if ((fields & DM_COLOR) != 0) {
        if ((values & SET_COLOR) != 0) {
            attributes.add(Chromaticity.COLOR);
        } else {
            attributes.add(Chromaticity.MONOCHROME);
        }
    }

    if ((fields & DM_PRINTQUALITY) != 0) {
        /* value < 0 indicates quality setting.
         * value > 0 indicates X resolution. In that case
         * hopefully we will also find y-resolution specified.
         * If its not, assume its the same as x-res.
         * Maybe Java code should try to reconcile this against
         * the printers claimed set of supported resolutions.
         */
        if (xRes < 0) {
            PrintQuality quality;
            if ((values & SET_RES_LOW) != 0) {
                quality = PrintQuality.DRAFT;
            } else if ((fields & SET_RES_HIGH) != 0) {
                quality = PrintQuality.HIGH;
            } else {
                quality = PrintQuality.NORMAL;
            }
            attributes.add(quality);
        } else if (xRes > 0 && yRes > 0) {
            attributes.add(
                new PrinterResolution(xRes, yRes, PrinterResolution.DPI));
        }
    }

    if ((fields & DM_DUPLEX) != 0) {
        Sides sides;
        if ((values & SET_DUP_VERTICAL) != 0) {
            sides = Sides.TWO_SIDED_LONG_EDGE;
        } else if ((values & SET_DUP_HORIZONTAL) != 0) {
            sides = Sides.TWO_SIDED_SHORT_EDGE;
        } else {
            sides = Sides.ONE_SIDED;
        }
        attributes.add(sides);
    }

    if ((fields & DM_PAPERSIZE) != 0) {
        addPaperSize(attributes, dmPaperSize, dmPaperWidth, dmPaperLength);
    }

    if ((fields & DM_DEFAULTSOURCE) != 0) {
        MediaTray tray =
            ((Win32PrintService)myService).findMediaTray(dmDefaultSource);
        attributes.add(new SunAlternateMedia(tray));
    }
}
 
Example 18
Source File: WPrinterJob.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private final void setJobAttributes(PrintRequestAttributeSet attributes,
                                    int fields, int values,
                                    short copies,
                                    short dmPaperSize,
                                    short dmPaperWidth,
                                    short dmPaperLength,
                                    short dmDefaultSource,
                                    short xRes,
                                    short yRes) {

    if (attributes == null) {
        return;
    }

    if ((fields & DM_COPIES) != 0) {
        attributes.add(new Copies(copies));
    }

    if ((fields & DM_COLLATE) != 0) {
        if ((values & SET_COLLATED) != 0) {
            attributes.add(SheetCollate.COLLATED);
        } else {
            attributes.add(SheetCollate.UNCOLLATED);
        }
    }

    if ((fields & DM_ORIENTATION) != 0) {
        if ((values & SET_ORIENTATION) != 0) {
            attributes.add(OrientationRequested.LANDSCAPE);
        } else {
            attributes.add(OrientationRequested.PORTRAIT);
        }
    }

    if ((fields & DM_COLOR) != 0) {
        if ((values & SET_COLOR) != 0) {
            attributes.add(Chromaticity.COLOR);
        } else {
            attributes.add(Chromaticity.MONOCHROME);
        }
    }

    if ((fields & DM_PRINTQUALITY) != 0) {
        /* value < 0 indicates quality setting.
         * value > 0 indicates X resolution. In that case
         * hopefully we will also find y-resolution specified.
         * If its not, assume its the same as x-res.
         * Maybe Java code should try to reconcile this against
         * the printers claimed set of supported resolutions.
         */
        if (xRes < 0) {
            PrintQuality quality;
            if ((values & SET_RES_LOW) != 0) {
                quality = PrintQuality.DRAFT;
            } else if ((fields & SET_RES_HIGH) != 0) {
                quality = PrintQuality.HIGH;
            } else {
                quality = PrintQuality.NORMAL;
            }
            attributes.add(quality);
        } else if (xRes > 0 && yRes > 0) {
            attributes.add(
                new PrinterResolution(xRes, yRes, PrinterResolution.DPI));
        }
    }

    if ((fields & DM_DUPLEX) != 0) {
        Sides sides;
        if ((values & SET_DUP_VERTICAL) != 0) {
            sides = Sides.TWO_SIDED_LONG_EDGE;
        } else if ((values & SET_DUP_HORIZONTAL) != 0) {
            sides = Sides.TWO_SIDED_SHORT_EDGE;
        } else {
            sides = Sides.ONE_SIDED;
        }
        attributes.add(sides);
    }

    if ((fields & DM_PAPERSIZE) != 0) {
        addPaperSize(attributes, dmPaperSize, dmPaperWidth, dmPaperLength);
    }

    if ((fields & DM_DEFAULTSOURCE) != 0) {
        MediaTray tray =
            ((Win32PrintService)myService).findMediaTray(dmDefaultSource);
        attributes.add(new SunAlternateMedia(tray));
    }
}
 
Example 19
Source File: WPrinterJob.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private void setColorAttrib(Attribute attr,
                              PrintRequestAttributeSet set) {
    setColorAttrib(attr);
    set.add(attr);
}
 
Example 20
Source File: PrintUtilities.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Sets the sides.
 *
 * @param set   The {@link PrintRequestAttributeSet} to use.
 * @param sides The new sides.
 */
public static void setSides(PrintRequestAttributeSet set, PageSides sides) {
    set.add(sides.getSides());
}