Java Code Examples for java.awt.print.PrinterJob#setPrintable()

The following examples show how to use java.awt.print.PrinterJob#setPrintable() . 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: TestMediaTraySelection.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void printTest() {

        MediaTray tray = null;
        //tray = getMediaTray( prservices, "Bypass Tray" );
        tray = getMediaTray( prservices, "Tray 4" );
        PrintRequestAttributeSet atrset = new HashPrintRequestAttributeSet();
        //atrset.add( MediaSizeName.ISO_A4 );
        atrset.add(tray);
        PrinterJob pjob = PrinterJob.getPrinterJob();
        pjob.setPrintable(new TestMediaTraySelection());
        try {
            pjob.print(atrset);
        } catch (PrinterException e) {
            e.printStackTrace();
            fail();
        }
    }
 
Example 2
Source File: PrintManager.java    From ganttproject with GNU General Public License v3.0 6 votes vote down vote up
public static void printChart(Chart chart, GanttExportSettings settings) {
  RenderedImage image = chart.getRenderedImage(settings);
  BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
  image.copyData(bufferedImage.getRaster());

  PrinterJob printJob = PrinterJob.getPrinterJob();

  printJob.setPrintable(new GanttPrintable(bufferedImage, GanttPrintable.REDUCE_FACTOR_DEFAULT));

  PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
  attr.add(MediaSizeName.ISO_A4);
  attr.add(OrientationRequested.LANDSCAPE);

  if (printJob.printDialog()) {
    try {
      printJob.print(attr);
    } catch (Exception e) {
      if (!GPLogger.log(e)) {
        e.printStackTrace(System.err);
      }
    }
  }
}
 
Example 3
Source File: ImageableAreaTest.java    From jdk8u_jdk 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 4
Source File: SwingChartPrinter.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @param args
 */
public static void main( String[] args )
{
	try
	{
		Chart chart = PrimitiveCharts.createBarChart();
	
		PrinterJob job = createPrinter();
		job.setPrintable( new SwingChartPrinter( chart ) );
		 boolean ok = job.printDialog();
         if (ok) {
             try {
                  job.print();
             } catch (PrinterException ex) {
              /* The job did not successfully complete */
             }
         }

	}
	catch ( Exception e )
	{
		e.printStackTrace();
	}

}
 
Example 5
Source File: WrongPaperPrintingTest.java    From TencentKona-8 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 6
Source File: PlotFrame.java    From opt4j with MIT License 6 votes vote down vote up
/**
 * Print using the native dialog. FIXME: This method does not seem to honor
 * the user's choice of portrait vs. landscape.
 */
protected void _printNative() {
	PrinterJob job = PrinterJob.getPrinterJob();

	// PageFormat pageFormat = job.defaultPage();
	// job.setPrintable(plot, pageFormat);
	job.setPrintable(plot);

	if (job.printDialog()) {
		try {
			// job.print() eventually
			// calls PlotBox.print(Graphics, PageFormat)
			job.print();
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(this, "Printing failed:\n" + ex.toString(), "Print Error",
					JOptionPane.WARNING_MESSAGE);
		}
	}
}
 
Example 7
Source File: DlgAttrsBug.java    From openjdk-jdk9 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 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: AbstractRamusPrintable.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void print(GUIFramework framework) throws PrinterException {
    final PrinterJob pj = framework.getPrinterJob(getJobKey());
    final Printable printable = createPrintable();
    pj.setPrintable(printable, getPageFormat());

    if (pj.printDialog()) {
        pj.setJobName(getJobName());
        pj.print();
        setPageFormat(getPageFormat());
    }
}
 
Example 10
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 11
Source File: ImageableAreaTest.java    From hottub 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 12
Source File: PrintCrashTest.java    From TencentKona-8 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 13
Source File: Print.java    From Logisim with GNU General Public License v3.0 5 votes vote down vote up
public static void doPrint(Project proj) {
	CircuitJList list = new CircuitJList(proj, true);
	Frame frame = proj.getFrame();
	if (list.getModel().getSize() == 0) {
		JOptionPane.showMessageDialog(proj.getFrame(), Strings.get("printEmptyCircuitsMessage"),
				Strings.get("printEmptyCircuitsTitle"), JOptionPane.YES_NO_OPTION);
		return;
	}
	ParmsPanel parmsPanel = new ParmsPanel(list);
	int action = JOptionPane.showConfirmDialog(frame, parmsPanel, Strings.get("printParmsTitle"),
			JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
	if (action != JOptionPane.OK_OPTION)
		return;
	List<Circuit> circuits = list.getSelectedCircuits();
	if (circuits.isEmpty())
		return;

	PageFormat format = new PageFormat();
	Printable print = new MyPrintable(proj, circuits, parmsPanel.getHeader(), parmsPanel.getRotateToFit(),
			parmsPanel.getPrinterView());

	PrinterJob job = PrinterJob.getPrinterJob();
	job.setPrintable(print, format);
	if (job.printDialog() == false)
		return;
	try {
		job.print();
	} catch (PrinterException e) {
		JOptionPane.showMessageDialog(proj.getFrame(), StringUtil.format(Strings.get("printError"), e.toString()),
				Strings.get("printErrorTitle"), JOptionPane.ERROR_MESSAGE);
	}
}
 
Example 14
Source File: PrintLatinCJKTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    try {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(testInstance);
        if (job.printDialog()) {
            job.print();
        }
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}
 
Example 15
Source File: bug8023392.java    From dragonwell8_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: PageDlgPrnButton.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void pageDialogExample() throws PrinterException
{
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat originalPageFormat = job.defaultPage();
    PageFormat pageFormat = job.pageDialog(originalPageFormat);

    if(originalPageFormat == pageFormat) return;

    job.setPrintable(this,pageFormat);
    job.print();
}
 
Example 17
Source File: PrintLatinCJKTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    try {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(testInstance);
        if (job.printDialog()) {
            job.print();
        }
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}
 
Example 18
Source File: ImageableAreaTest.java    From openjdk-jdk8u 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 19
Source File: PrintLatinCJKTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    try {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(testInstance);
        if (job.printDialog()) {
            job.print();
        }
    } catch (PrinterException ex) {
        ex.printStackTrace();
    }
}
 
Example 20
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!");
    }
}