Find out Print configuration in Java

Description

The following code shows how to find out Print configuration.

Example


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.PrintJob;
import java.awt.Toolkit;
import java.util.Properties;
// w ww.  jav  a  2 s. c o  m
import javax.swing.JFrame;

public class Main {
  public static void main(String args[]) {
    Toolkit toolkit = Toolkit.getDefaultToolkit();

    String name = "Test print job";
    Properties properties = new Properties();
    PrintJob pj = toolkit.getPrintJob(new JFrame(), name, properties);
    if (pj == null)
      System.out.println("A null PrintJob was returned.");
    else {
      String output = "Name: " + name + "\nProperties: "
          + properties.toString();
      Dimension pageDim = pj.getPageDimension();
      int resolution = pj.getPageResolution();
      boolean lastPageFirst = pj.lastPageFirst();
      output += "\nPage dimension (in pixels):";
      output += "\n height: " + String.valueOf(pageDim.height);
      output += "\n width: " + String.valueOf(pageDim.width);
      output += "\nResolution (pixels/inch): " + String.valueOf(resolution);
      output += "\nLast Page First: " + String.valueOf(lastPageFirst);
      System.out.println(output);
      Graphics g = pj.getGraphics();
      g.dispose();
      pj.end();
    }
  }

}

The code above generates the following result.

Find out Print configuration in Java




















Home »
  Java Tutorial »
    Graphics »




Animation
BufferedImage
Color
Font
Gradient
Graphics Settings
Image
Mouse Draw
Print
Shape
Text
Transform