java.awt.print.PrinterJob Java Examples

The following examples show how to use java.awt.print.PrinterJob. 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: PrintUtils.java    From ApprovalTests.Java with Apache License 2.0 7 votes vote down vote up
public void print(boolean prompt)
{
  PrinterJob printJob = PrinterJob.getPrinterJob();
  PageFormat format = printJob.defaultPage();
  Paper paper = format.getPaper();
  paper.setImageableArea(18, 0, 180, 840);// Paper format for receipt printer
  format.setPaper(paper);
  printJob.setPrintable(this, format);
  if (!prompt || printJob.printDialog())
  {
    try
    {
      printJob.print();
    }
    catch (PrinterException pe)
    {
      SimpleLogger.variable("Error printing: " + pe);
    }
  }
}
 
Example #2
Source File: ImageableAreaTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static void printWithJavaPrintDialog() {
    final JTable table = createAuthorTable(42);
    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();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example #3
Source File: PrintAttributeUpdateTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example #4
Source File: WPrinterJob.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void setPrinterNameAttrib(String printerName) {
    PrintService service = this.getPrintService();

    if (printerName == null) {
        return;
    }

    if (service != null && printerName.equals(service.getName())) {
        return;
    } else {
        PrintService []services = PrinterJob.lookupPrintServices();
        for (int i=0; i<services.length; i++) {
            if (printerName.equals(services[i].getName())) {

                try {
                    this.setPrintService(services[i]);
                } catch (PrinterException e) {
                }
                return;
            }
        }
    }
//** END Functions called by native code for querying/updating attributes

}
 
Example #5
Source File: ImageableAreaTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void printWithJavaPrintDialog() {
    final JTable table = createAuthorTable(50);
    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();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}
 
Example #6
Source File: Config.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public boolean showPageSetup() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat oldFormat = getPageFormat();
    PageFormat newFormat = job.pageDialog(oldFormat);

    if (oldFormat == newFormat) {
        return false;
    }
    myPageFormat = newFormat;

    // save
    set(PAGE_ORIENTATION, myPageFormat.getOrientation());
    Paper paper = myPageFormat.getPaper();

    set(PAPER_WIDTH, paper.getWidth());
    set(PAPER_HEIGHT, paper.getHeight());

    set(AREA_X, paper.getImageableX());
    set(AREA_Y, paper.getImageableY());

    set(AREA_WIDTH, paper.getImageableWidth());
    set(AREA_HEIGHT, paper.getImageableHeight());

    return true;
}
 
Example #7
Source File: PrintTest.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
private void printTextAction()
{
    printStr = area.getText().trim();
    if(printStr != null && printStr.length() > 0)
    {
        PAGES = getPagesCount(printStr);
        PrinterJob myPrtJob = PrinterJob.getPrinterJob();
        PageFormat pageFormat = myPrtJob.defaultPage();
        myPrtJob.setPrintable(this, pageFormat);
        if(myPrtJob.printDialog())
            try
            {
                myPrtJob.print();
            }
            catch(PrinterException pe)
            {
                pe.printStackTrace();
            }
    } else
    {
        JOptionPane.showConfirmDialog(null, "Sorry, Printer Job is Empty, Print Cancelled!", "Empty", -1, 2);
    }
}
 
Example #8
Source File: ChartPanel.java    From ECG-Viewer with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        if (job.printDialog()) {
            try {
                job.print();
            }
            catch (PrinterException e) {
                JOptionPane.showMessageDialog(this, e);
            }
        }
    }
}
 
Example #9
Source File: ChartComposite.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a print job for the chart.
 */
public void createChartPrintJob() {
    //FIXME try to replace swing print stuff by swt
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat pf = job.defaultPage();
    PageFormat pf2 = job.pageDialog(pf);
    if (pf2 != pf) {
        job.setPrintable(this, pf2);
        if (job.printDialog()) {
            try {
                job.print();
            }
            catch (PrinterException e) {
                MessageBox messageBox = new MessageBox(
                        this.canvas.getShell(), SWT.OK | SWT.ICON_ERROR);
                messageBox.setMessage(e.getMessage());
                messageBox.open();
            }
        }
    }
}
 
Example #10
Source File: GUIFramework.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
public PrinterJob getPrinterJob(String name) {
    PrinterJob job = jobs.get(name);
    if (job == null) {
        job = PrinterJob.getPrinterJob();
        jobs.put(name, job);
        propertyChanged(PRINT_JOB_INIT, job, name);
    }
    return job;
}
 
Example #11
Source File: SetPrintServiceTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    PrintServiceStub service = new PrintServiceStub("CustomPrintService");
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    try {
        printerJob.setPrintService(service);
        System.out.println("Test Passed");
    } catch (PrinterException e) {
        throw new RuntimeException("Test FAILED", e);
    }
}
 
Example #12
Source File: bug8023392.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }
}
 
Example #13
Source File: MasterReport.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private PageDefinition createDefaultPageDefinition() {
  final PageDefinition format;
  final ExtendedConfiguration config = ClassicEngineBoot.getInstance().getExtendedConfig();
  if ( config.getBoolProperty( ClassicEngineCoreModule.NO_PRINTER_AVAILABLE_KEY ) ) {
    format = new SimplePageDefinition( new PageFormat() );
  } else {
    format = new SimplePageDefinition( PrinterJob.getPrinterJob().defaultPage() );
  }
  return format;
}
 
Example #14
Source File: PathGraphics.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected PathGraphics(Graphics2D graphics, PrinterJob printerJob,
                       Printable painter, PageFormat pageFormat,
                       int pageIndex, boolean canRedraw) {
    super(graphics, printerJob);

    mPainter = painter;
    mPageFormat = pageFormat;
    mPageIndex = pageIndex;
    mCanRedraw = canRedraw;
}
 
Example #15
Source File: bug8023392.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }
}
 
Example #16
Source File: PrintPageDirect.java    From haxademic with MIT License 5 votes vote down vote up
protected void printPage(PrinterJob printJob) {
	try {
		printJob.print();
	} catch (Exception PrintException) {
		PrintException.printStackTrace();
	}
}
 
Example #17
Source File: PathGraphics.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected PathGraphics(Graphics2D graphics, PrinterJob printerJob,
                       Printable painter, PageFormat pageFormat,
                       int pageIndex, boolean canRedraw) {
    super(graphics, printerJob);

    mPainter = painter;
    mPageFormat = pageFormat;
    mPageIndex = pageIndex;
    mCanRedraw = canRedraw;
}
 
Example #18
Source File: PrintCrashTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
        }
    }
}
 
Example #19
Source File: ImageExportUtils.java    From chipster with MIT License 5 votes vote down vote up
public static void printComponent(Printable component) throws PrinterException {
	PrinterJob job = PrinterJob.getPrinterJob();
	job.setPrintable(component);
	
	boolean doPrint = job.printDialog();
	if (doPrint) {
		job.print();
	}
}
 
Example #20
Source File: bug8023392.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }
}
 
Example #21
Source File: JRPrinterAWT.java    From nordpos with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Fix for bug ID 6255588 from Sun bug database
 * @param job print job that the fix applies to
 */
public static void initPrinterJobFields(PrinterJob job)
{
	try
	{
		job.setPrintService(job.getPrintService());
	}
	catch (PrinterException e)
	{
	}
}
 
Example #22
Source File: Win32PrintService.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public PrintRequestAttributeSet
    showDocumentProperties(PrinterJob job,
                           Window owner,
                           PrintService service,
                           PrintRequestAttributeSet aset) {

    if (!(job instanceof WPrinterJob)) {
        return null;
    }
    WPrinterJob wJob = (WPrinterJob)job;
    return wJob.showDocumentProperties(owner, service, aset);
}
 
Example #23
Source File: PageDlgStackOverflowTest.java    From TencentKona-8 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 #24
Source File: SetPrintServiceTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    PrintServiceStub service = new PrintServiceStub("CustomPrintService");
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    try {
        printerJob.setPrintService(service);
        System.out.println("Test Passed");
    } catch (PrinterException e) {
        throw new RuntimeException("Test FAILED", e);
    }
}
 
Example #25
Source File: TexturePaintPrintingTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void printTexture() {
    f = new JFrame("Texture Printing Test");
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    final TexturePaintPrintingTest gpt = new TexturePaintPrintingTest();
    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);
}
 
Example #26
Source File: SetPrintServiceTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    PrintServiceStub service = new PrintServiceStub("CustomPrintService");
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    try {
        printerJob.setPrintService(service);
        System.out.println("Test Passed");
    } catch (PrinterException e) {
        throw new RuntimeException("Test FAILED", e);
    }
}
 
Example #27
Source File: TestPageDlgFrameAssociation.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void frameTest() {
    Panel panel =new Panel();

    print = new Button("PageDialog");
    print.setActionCommand("PageDialog");
    print.addActionListener((e) -> {
        PrinterJob job = PrinterJob.getPrinterJob();
            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            t.start();
            start = true;
            PageFormat pf = job.pageDialog(aset);
    });

    panel.add(print);

    frame = new Frame("Test Frame");
    frame.setLayout (new BorderLayout ());
    frame.add(panel,"South");
    frame.pack();
    frame.setVisible(true);

    t = new Thread (() -> {
        if (start) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {}
            frame.dispose();
        }
    });
}
 
Example #28
Source File: bug8023392.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(this);
    if (job.printDialog()) {
        try {
            job.print();
        } catch (PrinterException ex) {
            ex.printStackTrace();
        }
    }
}
 
Example #29
Source File: printJobExample.java    From chuidiang-ejemplos with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(new Printable() {
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            return 0;
        }
    });
    boolean top = job.printDialog();
}
 
Example #30
Source File: Win32PrintService.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public PrintRequestAttributeSet
    showDocumentProperties(PrinterJob job,
                           Window owner,
                           PrintService service,
                           PrintRequestAttributeSet aset) {

    if (!(job instanceof WPrinterJob)) {
        return null;
    }
    WPrinterJob wJob = (WPrinterJob)job;
    return wJob.showDocumentProperties(owner, service, aset);
}