2011-07-13 9 views
0

タイトルに実際に何が問題になっているのか分かりません。私はバーコードとテキストを印刷している認知ラベルプリンタを持っています。ラベルは4 "×2"のラベルです。問題は、イメージがラベルの左側から約1インチ、ページの上部から約1/2インチ印刷されていることです。何らかの理由でページの寸法が外れているように見えますが、何が原因かわかりません。どんな助けも素晴らしいだろう。ここに私が書いているPrintServiceクラスの完全なコードがあります。Java Print APIを使用してページデメンションを保証する方法

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); 
    } 
} 

答えて

1

おそらくA Basic Printing Programに示すように、プリンタのイメージング可能領域を反映するために、グラフィックスコンテキストの原点をtranslate()する必要があります。

+0

それは何も変わっていません。 – jonsinfinity

+0

ええ、あなたは小フォーマットのプリンタで否定的にしなければならないかもしれません。ねえ、あなたの 'PageFormat'の値を調べて、それらがプリンタに合っているかどうか確認してください。もしそうでなければ、私はドライバを再インストールすることを傷つけることはないと思う。 – trashgod

関連する問題