/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.print.*;
public class HelloWorldPrinter implements Printable, ActionListener {
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* My alterations for computations of media print area */
Double pageWidth = pf.getWidth() / 72.0;
Double pageHeight = pf.getHeight() / 72.0;
Double leftMargin = pf.getImageableX() / 72.0;
Double topMargin = pf.getImageableY() / 72.0;
Double rightMargin = (pf.getWidth() - pf.getImageableWidth() - pf.getImageableX()) / 72.0;
Double bottomMargin = (pf.getHeight() - pf.getImageableHeight() - pf.getImageableY()) / 72.0;
/* Now we perform our rendering */
g.drawString("Width: " + pageWidth + " in. >>> " + (pageWidth * 25.4) + " mm", 100, 100);
g.drawString("Height: " + pageHeight + " in. >>> " + (pageHeight * 25.4) + " mm", 100, 125);
g.drawString("Left: " + leftMargin + " in. >>> " + (leftMargin * 25.4) + " mm", 100, 150);
g.drawString("Top: " + topMargin + " in. >>> " + (topMargin * 25.4) + " mm", 100, 175);
g.drawString("Right: " + rightMargin + " in. >>> " + (rightMargin * 25.4) + " mm", 100, 200);
g.drawString("Bottom: " + bottomMargin + " in. >>> " + (bottomMargin * 25.4) + " mm", 100, 225);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}
}
public static void main(String args[]) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame f = new JFrame("Hello World Printer");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JButton printButton = new JButton("Media Print Area");
printButton.addActionListener(new HelloWorldPrinter());
f.add("Center", printButton);
f.pack();
f.setVisible(true);
}
}
What this code does is determine the minimum margins for a particular printer on your system. It works as is intended. However, I'm hoping someone here can show me how to modify the code further so that I won't have to waste paper printing the page. I'm not entirely sure how to capture the output returned by the pf PageFormat object without actually printing (perhaps accessed as a field of the class somehow?) Nothing fancy, if it returned the results in a JOptionPane I'd be more than satisfied and thankful. Thanks in advance, makes me wish my Java skills weren't so rusty.
Media Print Area
Page 1 of 110 Replies - 1170 Views - Last Post: 21 February 2011 - 09:06 AM
#1
Media Print Area
Posted 20 February 2011 - 10:02 PM
Replies To: Media Print Area
#2
Re: Media Print Area
Posted 20 February 2011 - 10:11 PM
Also, if you download something like Primo PDF, it installs a print driver for converting documents to PDFs. If you select the PDF printer from your Java program, you can see the output as a PDF file. That's what I did when testing my printing code.
Hope this helps some.
#3
Re: Media Print Area
Posted 20 February 2011 - 10:22 PM
#4
Re: Media Print Area
Posted 20 February 2011 - 10:36 PM
public int print(Graphics g, PageFormat pf, int page)
Notice how it has Graphics g as a param. If you pass g to the paint() method of a JPanel, then draw using g in your print() method. The print job (maybe except for margins) will display on your JPanel. This is as close to a print preview you can get in standard Java. That's what I was describing in my last post regarding the Graphics param.
Beyond that, it is hard to adjust for margins as it sounds like you are describing. I personally found the PDF print driver solution to be satisfactory when I played around with printing. I also found a 3rd party library for print previewing that you may find helpful.
#5
Re: Media Print Area
Posted 20 February 2011 - 11:18 PM
#6
Re: Media Print Area
Posted 20 February 2011 - 11:19 PM
Rather than using the Graphics param to drawString() the margins, just use a JOptionPane showMessageDialog() box:
JOptionPane.showMessageDialog(null, "String you want to display");
#7
Re: Media Print Area
Posted 20 February 2011 - 11:48 PM
#8
Re: Media Print Area
Posted 21 February 2011 - 12:02 AM
You could create a PrintRequestAttributeSet object to match the Attributes for printing, and pass it to the PrinterJob getPageFormat() method. That's as close as you can get to the PageFormat param in the print() method.
#9
Re: Media Print Area
Posted 21 February 2011 - 12:19 AM
#10
Re: Media Print Area
Posted 21 February 2011 - 12:30 AM
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* My alterations for computations of media print area */
Double pageWidth = pf.getWidth() / 72.0;
Double pageHeight = pf.getHeight() / 72.0;
Double leftMargin = pf.getImageableX() / 72.0;
Double topMargin = pf.getImageableY() / 72.0;
Double rightMargin = (pf.getWidth() - pf.getImageableWidth() - pf.getImageableX()) / 72.0;
Double bottomMargin = (pf.getHeight() - pf.getImageableHeight() - pf.getImageableY()) / 72.0;
JOptionPane.showMessageDialog(null, "Width: " + pageWidth + " in. >>> " + (pageWidth * 25.4) + " mm\nHeight: " + pageHeight + " in. >>> " + (pageHeight * 25.4) + " mm\nLeft: " + leftMargin + " in. >>> " + (leftMargin * 25.4) + " mm\nTop: " + topMargin + " in. >>> " + (topMargin * 25.4) + " mm\nRight: " + rightMargin + " in. >>> " + (rightMargin * 25.4) + " mm\nBottom: " + bottomMargin + " in. >>> " + (bottomMargin * 25.4) + " mm", "Results", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
This post has been edited by grimpirate: 21 February 2011 - 12:36 AM
#11
Re: Media Print Area
Posted 21 February 2011 - 09:06 AM
|
|

New Topic/Question
Reply



MultiQuote







|