Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problems printing on custom sized paper.

807580Jan 23 2010 — edited May 31 2010
Hello all,

I am trying to iron out my problems with printing on custom sized tractor paper (using an impact printer). Every time I print a test page, the printer behaves as if it was loaded with tractor paper that is 11 inches long, rather then the 7 inches that the forms are on.

Before I list my code, let me explain everything that I have done. I am running Windows XP, so I went to the Printers and Faxes folder, clicked File > Server Properties, and created a custom form size (8.5" wide by 7" long with zero margins). Going back to the Printers and Faxes folder, I right clicked on the impact printer and clicked properties. From there I clicked the Device tab and selected my custom form that I had created for the tractor feed section. I then clicked the general tab and clicked the printing preferences. From there I clicked the Paper/Quality tab and selected Tractor Feed for the source. Then I clicked the advance button and selected my custom form for paper size. I also changed the resolution to 360X180.

The printer itself has hardware settings for paper size. I've went through the menu and selected 7 inches for the form length.

Now going to the code, I created the paper object and set it size to 612 for width and 504 for height. I also changed the ImageableArea to the size of the paper (which I think eliminates the margins), and set its orientation to 0, 0. I then set the paper object to the PageFormat, and used drawString to test my settings.

Program compiles fine. When I run it and click the button, I select the correct printer. I also went through the preferences to make sure that all the setting are correct (especially to make sure my custom form is selected.). The closest I can get the string to the corner of the page is 75, 80. On the paper it measures 7/8" from the left, and 1" from the top.

I have tested my custom form settings with word 2007. I was able to get the text on the very corner of the paper, and the printer was able to determine where the next form started. I believe that my problem lies within my code. Any help would be greatly appreciated!

Thanks!
import java.awt.*;  
import java.awt.event.*;  
import javax.swing.*;  
import java.awt.print.*;  
  
public class Print01 implements Printable, ActionListener {  
  
    public int print(Graphics g, PageFormat pf, int page) throws  
                                                        PrinterException {  
  
        if (page > 0) {   
            return NO_SUCH_PAGE;  
        }  
  
        double width = 612;  
        double height = 504;  
        Paper custom = new Paper();  
        custom.setSize(width,height);  
        custom.setImageableArea(0,0,width,height);  
        pf.setPaper(custom);  
  
        Graphics2D g2d = (Graphics2D)g;  
        g2d.translate(pf.getImageableX(), pf.getImageableY());  
          
        //The following returns the correct dimensions (612 width, 504 height).  
        System.out.println("Width: "+pf.getImageableWidth()+" Height: "+pf.getImageableHeight());  
  
        g.drawString("Testing", 75, 80); //Smaller values result in clipping.  
  
        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) {  
            }  
        }  
    }  
  
    public static void main(String args[]) {  
  
        UIManager.put("swing.boldMetal", Boolean.FALSE);  
        JFrame f = new JFrame("Test page printer.");  
        f.addWindowListener(new WindowAdapter() {  
              public void windowClosing(WindowEvent e) {System.exit(0);}});  
        JButton printButton = new JButton("Print test page.");  
        printButton.addActionListener(new Print01());  
        f.add("Center", printButton);  
        f.pack();  
        f.setVisible(true);  
    }  
}  
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 28 2010
Added on Jan 23 2010
8 comments
778 views