10 Replies - 1564 Views - Last Post: 21 February 2011 - 09:06 AM Rate Topic: -----

#1 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Media Print Area

Posted 20 February 2011 - 10:02 PM

This is a bit of code I modified from the Oracle tutorial pages to suit my purposes:
/*
 * 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.

Is This A Good Question/Topic? 0
  • +

Replies To: Media Print Area

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,784
  • Joined: 27-December 08

Re: Media Print Area

Posted 20 February 2011 - 10:11 PM

This is one of the few times you may actually want to invoke the paint() method. If you extend something like JPanel, with a JFrame equivelant to the paper size, you could invoke the JPanel's paint() method passing the Graphics param in the print() method to the paint() method.

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. :)
Was This Post Helpful? 0
  • +
  • -

#3 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Media Print Area

Posted 20 February 2011 - 10:22 PM

I'm not interested in printing graphics in general. Specifically, I'm interested in computing the minimum page margins and displaying this result, preferrably without having to print a page for each printer.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,784
  • Joined: 27-December 08

Re: Media Print Area

Posted 20 February 2011 - 10:36 PM

Let me make sure I understand you correctly. Are you asking how to preview the output of your page? If so, see the print() method header:
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.
Was This Post Helpful? 0
  • +
  • -

#5 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Media Print Area

Posted 20 February 2011 - 11:18 PM

No I don't want to print preview anything. What I want is the results of the margin computations to simply be told to be me in a dialog box as opposed to being printed to the page.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,784
  • Joined: 27-December 08

Re: Media Print Area

Posted 20 February 2011 - 11:19 PM

Ah, I completely misunderstood. Sorry about the confusion. :)

Rather than using the Graphics param to drawString() the margins, just use a JOptionPane showMessageDialog() box:
JOptionPane.showMessageDialog(null, "String you want to display");


Was This Post Helpful? 0
  • +
  • -

#7 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Media Print Area

Posted 20 February 2011 - 11:48 PM

lol yes I know I require a JOptionPane, what I mean to say is that the PageFormat object is encapsulated within the print method. In order to get the results of the PageFormat object I HAVE to print to the printer. How can I bypass this? Obviously I could simply not insert paper and cancel the print, but I would prefer simply to bypass the printer altogether.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,784
  • Joined: 27-December 08

Re: Media Print Area

Posted 21 February 2011 - 12:02 AM

Sorry, I misunderstood again. :stupid:

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.
Was This Post Helpful? 0
  • +
  • -

#9 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Media Print Area

Posted 21 February 2011 - 12:19 AM

How would I go about getting the default attributes for a particular printer?
Was This Post Helpful? 0
  • +
  • -

#10 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: Media Print Area

Posted 21 February 2011 - 12:30 AM

Screw it, I'm just going to short the print method and exit after the JOptionPane is clicked. Messy but it gets me my results unless someone cares to do better.
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

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10694
  • View blog
  • Posts: 39,784
  • Joined: 27-December 08

Re: Media Print Area

Posted 21 February 2011 - 09:06 AM

You could show the printDialog() and see what is specified there, and just hit cancel to prevent printing. There is definitely a lot more Oracle could do with the Java Printing API, as it is definitely not the best tool in the world. Sorry I can't get you a better solution for what you are looking for. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1