jonsinfinity
jonsinfinity

Reputation: 197

How to ensure page demensions using Java Print API

I don't know if the title of this really explains what my issue is. I have a Cognitive label printer that I am printing a barcode and some text to. The labels are 4"x2" labels. The problem is that the image is printing about an inch from the left side of the label and about half an inch from the top of the page. For some reason the page dimensions seem to be off and I can't figure out what is causing this. Any help would be great. Here is the full code for the PrintService class that I am writing.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.print.*;
import java.awt.image.BufferedImage;

import javax.print.attribute.*;
import javax.print.attribute.standard.*;

import net.sourceforge.barbecue.*;

public class PrintService implements Printable {

    BufferedImage bi;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        PrintService printService = new PrintService(args);
    }

    PrintService(String[] args) {
      switch(new Integer(args[0])) {
        case 1: printPackageLabel(args[1]); break;
        default: 
      }
    }

  private void printPackageLabel(String barcode) {

    bi = getBarcode(barcode);

    //-- Create a printerJob object
    PrinterJob printJob = PrinterJob.getPrinterJob();

    PageFormat pageFormat = printJob.defaultPage();

    Paper paper = new Paper();

    //-- 4" x 2"
    paper.setSize(288,144);

    //-- x,y,width,height
    paper.setImageableArea(0, 0, 200, 110);

    pageFormat.setPaper(paper);

    //-- Set the printable class to this one since we
    //-- pass in the PageFormat object
    printJob.setPrintable(this, pageFormat);

    //PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
    //attr_set.add(new MediaSize(0,0, 292)); 

    //-- disable print dialog
    //if (printJob.printDialog()) {
      try {
       printJob.print();
      } catch (Exception PrintException) {
        PrintException.printStackTrace();
      }
    //}

  }

  private BufferedImage getBarcode (String data) {
  Barcode barcode = null;
  BufferedImage bufferedImage = null;
    try {
        barcode = BarcodeFactory.createCode128(data);
        barcode.setBarWidth(1);
        barcode.setBarHeight(20);
        barcode.setDrawingText(true);
    } catch (BarcodeException e) {
        throw new RuntimeException(e);
    }
    try {
        bufferedImage = BarcodeImageHandler.getImage(barcode);
    } catch (Exception  e) {
        throw new RuntimeException(e);
    }
   return bufferedImage;
  }


  public int print(Graphics g, PageFormat pageFormat, int page) {

    if (page == 0) {

      g.setColor(Color.black);

      //-- Prints a box aroung the imageable area
      //-- start(x1,y1),stop(x2,y2)
      g.drawLine(0,0,200,0);
      g.drawLine(0,0,0,110);
      g.drawLine(0,110,200,110);
      g.drawLine(200,0,200,110);

      //-- image,offset(x,y),dimensions(width,height), imageObserver null
      g.drawImage(bi,5,5,150,50,null);

      //-- identify top left corner - to be removed
      g.drawOval(0, 0, 10, 10);
      //-- string, x, y
      g.drawString("<Item>",5,65);
      g.drawString("<Type>",5,75);
      g.drawString("<Weight>",5,85);
      g.drawString("Packaged: <DateTime>",5,95);

      return (PAGE_EXISTS);
     } else
      return (NO_SUCH_PAGE);
   }
}

Upvotes: 0

Views: 1160

Answers (1)

trashgod
trashgod

Reputation: 205775

You'll probably have to translate() the graphics context's origin to reflect the printer's imageable area, as shown in A Basic Printing Program.

Upvotes: 1

Related Questions