import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import java.awt.print.PageFormat; 
import java.awt.print.Printable; 
import java.awt.print.PrinterException; 
import java.awt.print.PrinterJob; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 

/** 
 * @author Tilman Hausherr 
 */ 
public class PDFBox4010 implements Printable 
{ 
    public static void main(String[] args) throws IOException, PrinterException 
    { 
        new PDFBox4010().doStuff(); 
    } 

    /** 
     * Does the same graphics operations on the graphics device of an image and of a printer. 
     * Both should have the same output but only the image is OK, i.e. shows a barcode-line 
     * image within a border. The printer output only shows the border. 
     * 
     * @throws IOException 
     * @throws PrinterException 
     */ 
    void doStuff() throws IOException, PrinterException 
    { 
        PrinterJob job = PrinterJob.getPrinterJob(); 
        int width = (int) job.defaultPage().getWidth(); 
        int height = (int) job.defaultPage().getHeight(); 

        BufferedImage targetImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
        Graphics2D g = (Graphics2D) targetImage.getGraphics(); 

        renderStuff(g, targetImage.getWidth(), targetImage.getHeight()); 

        g.dispose(); 

        ImageIO.write(targetImage, "png", new File("output.png")); 

        job.setPrintable(this); 
        if (job.printDialog()) 
        { 
            // this will call renderStuff() again 
            job.print(); 
        } 
    } 

    /** 
     * Draws a rotated one-line image into a graphics device. 
     * @param g 
     * @param width 
     * @param height 
     */ 
    public void renderStuff(Graphics2D g, int width, int height) 
    { 
        // background 
        g.setBackground(Color.white); 
        g.clearRect(0, 0, width, height); 

        BufferedImage img = new BufferedImage(303, 1, BufferedImage.TYPE_BYTE_BINARY); 

        // barcode-like pattern on single pixel line 
        for (int i = 0; i < img.getWidth(); ++i) 
        { 
           img.setRGB(i, 0, (i / 2 % 3) == 0 ? 0 : 0xFFFFFF); 
        } 

        AffineTransform at = new AffineTransform(0, -0.36, -63, 0, 163, 300); 

        // draw our "barcode" 
        g.drawImage(img, at, null); 

        // draw rectangle around "barcode" 
        g.setColor(Color.red); 
        g.drawRect(100, 191, 63, 109); 
    } 

    @Override 
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException 
    { 
        if (pageIndex != 0) 
        { 
            return NO_SUCH_PAGE; 
        } 
        renderStuff((Graphics2D) graphics, (int) pageFormat.getWidth(), (int) pageFormat.getHeight()); 
        return PAGE_EXISTS; 
    } 
} 