如何使用Java打印API确保页面尺寸
我不知道这个标题是否真的能解释我的问题。我有一个认知标签打印机,我正在打印条形码和一些文本。标签为4个“x2”标签。问题是图像打印距离标签左侧约一英寸,距离页面顶部约半英寸。由于某种原因,页面维度似乎关闭了,我不知道是什么原因导致了这种情况。任何帮助都会很好。下面是我正在编写的PrintService类的完整代码如何使用Java打印API确保页面尺寸,java,swing,awt,Java,Swing,Awt,我不知道这个标题是否真的能解释我的问题。我有一个认知标签打印机,我正在打印条形码和一些文本。标签为4个“x2”标签。问题是图像打印距离标签左侧约一英寸,距离页面顶部约半英寸。由于某种原因,页面维度似乎关闭了,我不知道是什么原因导致了这种情况。任何帮助都会很好。下面是我正在编写的PrintService类的完整代码 import java.awt.Color; import java.awt.Graphics; import java.awt.print.*; import java.awt.im
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);
}
}
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.print.*;
导入java.awt.image.buffereImage;
导入javax.print.attribute.*;
导入javax.print.attribute.standard.*;
导入net.sourceforge.burbage.*;
公共类PrintService实现可打印{
缓冲图像bi;
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
PrintService PrintService=新的PrintService(args);
}
PrintService(字符串[]args){
开关(新整数(参数[0])){
案例1:printPackageLabel(参数[1]);中断;
违约:
}
}
专用void打印包标签(字符串条形码){
bi=获取条形码(条形码);
//--创建printerJob对象
PrinterJob printJob=PrinterJob.getPrinterJob();
PageFormat PageFormat=printJob.defaultPage();
纸张=新纸张();
//--4英寸x2英寸
纸张尺寸(288144);
//--x,y,宽度,高度
纸张.可设置成像面积(0,0,200,110);
页面格式。设定纸(纸);
//--将可打印类设置为此类,因为我们
//--传入PageFormat对象
printJob.setPrintable(这是pageFormat);
//PrintRequestAttributeSet attr_set=新的HashPrintRequestAttributeSet();
//attr_set.add(新的MediaSize(0,0292));
//--禁用打印对话框
//if(printJob.printDialog()){
试一试{
printJob.print();
}捕获(异常打印异常){
PrintException.printStackTrace();
}
//}
}
专用缓冲区图像getBarcode(字符串数据){
条码=空;
BuffereImage BuffereImage=null;
试一试{
barcode=BarcodeFactory.createCode128(数据);
条形码。设置宽度(1);
条码.立根高度(20);
条形码。setDrawingText(真);
}捕获(条形码异常e){
抛出新的运行时异常(e);
}
试一试{
BuffereImage=BarcodeImageHandler.getImage(条形码);
}捕获(例外e){
抛出新的运行时异常(e);
}
返回缓冲区图像;
}
公共整型打印(图形g、页面格式、页面格式、整型页面){
如果(第==0页){
g、 设置颜色(颜色为黑色);
//--在可成像区域周围打印一个方框
//--启动(x1,y1),停止(x2,y2)
g、 抽绳(0,0200,0);
g、 抽绳(0,0,0110);
g、 抽绳(0110200110);
g、 抽绳(200,0200110);
//--图像,偏移量(x,y),尺寸(宽度,高度),图像观察者空值
g、 drawImage(bi,5,5150,50,空);
//--识别左上角-要删除
g、 抽卵(0,0,10,10);
//--弦,x,y
g、 抽绳(“、5、65);
g、 抽绳(“,5,75”);
g、 抽绳(“、5、85”);
g、 抽绳(包装:,5,95);
返回(第_页已存在);
}否则
返回(无此页面);
}
}
您可能需要translate()
图形上下文的原点来反映打印机的可成像区域,如所示。是的,这没有改变任何内容。是的,您可能需要在小格式打印机上使用负片。嘿,检查页面格式中的值,看看它们是否与打印机匹配。如果没有,我想重新安装驱动程序也没什么坏处。