Search in sources :

Example 1 with PrinterJob

use of java.awt.print.PrinterJob 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 PrinterJob

use of java.awt.print.PrinterJob in project jdk8u_jdk by JetBrains.

the class LinearGradientPrintingTest method createUI.

public static void createUI() {
    f = new JFrame("LinearGradient Printing Test");
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    final LinearGradientPrintingTest gpt = new LinearGradientPrintingTest();
    Container c = f.getContentPane();
    c.add(BorderLayout.CENTER, gpt);
    final JButton print = new JButton("Print");
    print.addActionListener(new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(gpt);
            final boolean doPrint = job.printDialog();
            if (doPrint) {
                try {
                    job.print();
                } catch (PrinterException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    });
    c.add(print, BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);
}
Also used : Container(java.awt.Container) JFrame(javax.swing.JFrame) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) PrinterException(java.awt.print.PrinterException) AbstractAction(javax.swing.AbstractAction) PrinterJob(java.awt.print.PrinterJob)

Example 3 with PrinterJob

use of java.awt.print.PrinterJob 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 4 with PrinterJob

use of java.awt.print.PrinterJob 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 5 with PrinterJob

use of java.awt.print.PrinterJob in project jdk8u_jdk by JetBrains.

the class PrintDialog method main.

public static void main(String[] args) throws Exception {
    // instruction dialog
    Frame instruction = new Frame("Verify that no native print dialog is showed");
    instruction.add(new TextArea(instructions));
    instruction.pack();
    instruction.show();
    // test begin
    PrintServiceStub service = new PrintServiceStub("test");
    PrintServiceLookup.registerService(service);
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintService(service);
    job.printDialog();
    System.out.println("test passed");
}
Also used : PrinterJob(java.awt.print.PrinterJob)

Aggregations

PrinterJob (java.awt.print.PrinterJob)57 PrinterException (java.awt.print.PrinterException)28 PageFormat (java.awt.print.PageFormat)22 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)19 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)17 PDFPageable (org.apache.pdfbox.printing.PDFPageable)9 IOException (java.io.IOException)7 Paper (java.awt.print.Paper)6 Printable (java.awt.print.Printable)5 File (java.io.File)5 Copies (javax.print.attribute.standard.Copies)5 ActionEvent (java.awt.event.ActionEvent)4 PrintService (javax.print.PrintService)4 Container (java.awt.Container)3 Book (java.awt.print.Book)3 JobName (javax.print.attribute.standard.JobName)3 PageRanges (javax.print.attribute.standard.PageRanges)3 AbstractAction (javax.swing.AbstractAction)3 JButton (javax.swing.JButton)3 JFrame (javax.swing.JFrame)3