Subscribe to Grim's Projects        RSS Feed
-----

Statistical Printer Assistance

Icon 5 Comments
Fellow DIColytes I once again require your assistance for the purposes of data gathering. I have written a Java app (admittedly not a great one) to determine the minimum printer margins of a printer connected to your system. I'm particularly interested in data from folks where the standard paper size is A4 (i.e. printers designed for A4 paper as opposed to Letter), but all data is welcome.

My request is that you run the java app on your system and post your results here for me so that I may collect them in a spreadsheet. Thank you for your participation. I am displaying the source code so you will not have to fear getting some malware attack and will include the compiled JAR file in case you don't want to spend the time compiling it yourself. Thanks again for your help.

/*
 * 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.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class HelloWorldPrinter implements Printable, ActionListener {

    private String printerName;
    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;

        NumberFormat formatter = new DecimalFormat("#0.000");
        JTextArea textArea = new JTextArea(
                "Width: " + formatter.format(pageWidth) + " in. >>> " +
                formatter.format(pageWidth * 25.4) +
                " mm\nHeight: " + formatter.format(pageHeight) + " in. >>> " +
                formatter.format(pageHeight * 25.4) +
                " mm\nLeft: " + formatter.format(leftMargin) + " in. >>> " +
                formatter.format(leftMargin * 25.4) +
                " mm\nTop: " + formatter.format(topMargin) + " in. >>> " +
                formatter.format(topMargin * 25.4) +
                " mm\nRight: " + formatter.format(rightMargin) + " in. >>> " +
                formatter.format(rightMargin * 25.4) +
                " mm\nBottom: " + formatter.format(bottomMargin) + " in. >>> " +
                formatter.format(bottomMargin * 25.4) + " mm"
                );
        textArea.setEditable(true);
        JOptionPane.showMessageDialog(null, textArea, this.printerName + " - Margins", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);

        /* 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();
         this.printerName = job.getPrintService().getName();
         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("Media Print Area Calculator");
        f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JButton printButton = new JButton("Proceed");
        printButton.addActionListener(new HelloWorldPrinter());
        f.add("Center", printButton);
        f.pack();
        f.setVisible(true);
    }
}

There is a possibility the code MIGHT engage your printer as this code simply exits the app during the print method for the purposes of not doing so (couldn't figure out a way to get the data I needed otherwise), but if it does it will simply print a blank page so no worries about ink consumption.

The app will launch a tiny window with a single button on it that reads "Proceed". It will then launch a standard printer selection dialog box. When you've selected your printer and clicked "Ok" a JOptionPane will pop up with the results. When you click "Ok" the program will terminate.

NOTE: PDF printer results are not needed, PDF printers have no physical limits, therefore it should be obvious the margins will always be 0 (i.e. borderless printing).

Attached File(s)

5 Comments On This Entry

Page 1 of 1

Curtis Rutland Icon

22 February 2011 - 01:47 PM
Here's the output for one of my printers. I'd suggest that you change the program slightly to output to a text box rather than directly to the form, so that we can copy and paste. Since I'm too lazy to transcribe the text, here's a screenshot:

Posted Image
0

grimpirate Icon

22 February 2011 - 04:46 PM
Changes made insertAlias, now you just have to highlight the text and press Ctrl + C and you can copy the text.
0

westforduk Icon

22 February 2011 - 05:44 PM
Width: 8.268 in. >>> 210.000 mm
Height: 11.693 in. >>> 297.000 mm
Left: 0.117 in. >>> 2.963 mm
Top: 0.117 in. >>> 2.963 mm
Right: 0.118 in. >>> 2.990 mm
Bottom: 0.118 in. >>> 2.995 mm
0

n8wxs Icon

26 February 2011 - 12:33 PM
Lexmark Z1420

Width: 8.500 in. >>> 215.900 mm
Height: 11.000 in. >>> 279.400 mm
Left: 0.250 in. >>> 6.350 mm
Top: 0.067 in. >>> 1.693 mm
Right: 0.250 in. >>> 6.350 mm
Bottom: 0.500 in. >>> 12.700 mm
0

Tryparse Icon

26 February 2011 - 10:02 PM
Cannon MP190

Width: 8.500 in. >>> 215.900 mm
Height: 11.000 in. >>> 279.400 mm
Left: 0.252 in. >>> 6.392 mm
Top: 0.117 in. >>> 2.963 mm
Right: 0.248 in. >>> 6.308 mm
Bottom: 0.198 in. >>> 5.038 mm
0
Page 1 of 1