Search in sources :

Example 1 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project ACS by ACS-Community.

the class PrintUtil method printPDF.

//
//  -- PRIVATE METHODS ---------------------------------------------
//
private void printPDF(String name, Printable printable) {
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.ISO_A4);
    aset.add(OrientationRequested.PORTRAIT);
    PrintService service = findPrinterService(flavor, aset);
    Assertion.assertTrue(service != null, "service != null");
    PrinterJob pj = PrinterJob.getPrinterJob();
    try {
        pj.setPrintService(service);
        pj.setPrintable(printable);
        pj.pageDialog(aset);
        pj.print(aset);
    } catch (PrinterException e) {
        Log log = LogFactory.getLog(PrintUtil.class);
        log.warn("The component " + name + " could not be printed");
    }
}
Also used : Log(org.apache.commons.logging.Log) PrinterException(java.awt.print.PrinterException) DocFlavor(javax.print.DocFlavor) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintService(javax.print.PrintService) PrinterJob(java.awt.print.PrinterJob)

Example 2 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project jdk8u_jdk by JetBrains.

the class PrintAttributeUpdateTest method main.

public static void main(String[] args) throws Exception {
    String[] instructions = { "Select Pages Range From instead of All in print dialog. ", "Then select Print" };
    SwingUtilities.invokeAndWait(() -> {
        JOptionPane.showMessageDialog((Component) null, instructions, "Instructions", JOptionPane.INFORMATION_MESSAGE);
    });
    HashPrintRequestAttributeSet as = new HashPrintRequestAttributeSet();
    PrinterJob j = PrinterJob.getPrinterJob();
    j.setPageable(new PrintAttributeUpdateTest());
    as.add(DialogTypeSelection.NATIVE);
    j.printDialog(as);
    if (as.containsKey(PageRanges.class) == false) {
        throw new RuntimeException("Print Dialog did not update " + " attribute set with page range");
    }
    Attribute[] attrs = as.toArray();
    for (int i = 0; i < attrs.length; i++) {
        System.out.println("attr " + attrs[i]);
    }
    j.print(as);
}
Also used : PageRanges(javax.print.attribute.standard.PageRanges) Attribute(javax.print.attribute.Attribute) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob)

Example 3 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project jdk8u_jdk by JetBrains.

the class PrintCrashTest method main.

public static void main(String[] args) throws Exception {
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    printerJob.setPrintable((graphics, pageFormat, pageIndex) -> {
        if (pageIndex != 0) {
            return Printable.NO_SUCH_PAGE;
        } else {
            Shape shape = new Rectangle(110, 110, 10, 10);
            Rectangle rect = shape.getBounds();
            BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(rect.width, rect.height, Transparency.BITMASK);
            graphics.drawImage(image, rect.x, rect.y, rect.width, rect.height, null);
            return Printable.PAGE_EXISTS;
        }
    });
    File file = null;
    try {
        HashPrintRequestAttributeSet hashPrintRequestAttributeSet = new HashPrintRequestAttributeSet();
        file = File.createTempFile("out", "ps");
        file.deleteOnExit();
        Destination destination = new Destination(file.toURI());
        hashPrintRequestAttributeSet.add(destination);
        printerJob.print(hashPrintRequestAttributeSet);
    } finally {
        if (file != null) {
            file.delete();
        }
    }
}
Also used : Destination(javax.print.attribute.standard.Destination) Shape(java.awt.Shape) Rectangle(java.awt.Rectangle) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob)

Example 4 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project jdk8u_jdk by JetBrains.

the class PageDlgApp method main.

public static void main(String[] args) throws Exception {
    String[] instructions = { "Visual inspection of the dialog is needed. ", "It should be a Printer Job Setup Dialog", "Do nothing except Cancel", "You must NOT press OK" };
    SwingUtilities.invokeAndWait(() -> {
        JOptionPane.showMessageDialog((Component) null, instructions, "information", JOptionPane.INFORMATION_MESSAGE);
    });
    PrinterJob pj = PrinterJob.getPrinterJob();
    PrintRequestAttributeSet pSet = new HashPrintRequestAttributeSet();
    pSet.add(DialogTypeSelection.NATIVE);
    if ((pj.pageDialog(pSet)) != null) {
        throw new RuntimeException("PrinterJob.pageDialog(PrintRequestAttributeSet)" + " does not return null when dialog is cancelled");
    }
}
Also used : HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 5 with HashPrintRequestAttributeSet

use of javax.print.attribute.HashPrintRequestAttributeSet in project jdk8u_jdk by JetBrains.

the class ImageableAreaTest method printWithCustomImageareaSize.

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!");
    }
}
Also used : MediaPrintableArea(javax.print.attribute.standard.MediaPrintableArea) MessageFormat(java.text.MessageFormat) Copies(javax.print.attribute.standard.Copies) JTable(javax.swing.JTable) Printable(java.awt.print.Printable) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterException(java.awt.print.PrinterException) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrinterJob(java.awt.print.PrinterJob)

Aggregations

HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)40 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)28 PrinterJob (java.awt.print.PrinterJob)16 PrintService (javax.print.PrintService)13 IOException (java.io.IOException)12 JobName (javax.print.attribute.standard.JobName)12 PrinterException (java.awt.print.PrinterException)10 File (java.io.File)9 DocFlavor (javax.print.DocFlavor)9 Copies (javax.print.attribute.standard.Copies)9 DocPrintJob (javax.print.DocPrintJob)8 Attribute (javax.print.attribute.Attribute)7 SimpleDoc (javax.print.SimpleDoc)6 PageFormat (java.awt.print.PageFormat)5 PrintException (javax.print.PrintException)5 PrintRequestAttribute (javax.print.attribute.PrintRequestAttribute)5 PrintServiceAttributeSet (javax.print.attribute.PrintServiceAttributeSet)4 Destination (javax.print.attribute.standard.Destination)4 java.awt.print (java.awt.print)3 Printable (java.awt.print.Printable)3