资源简介
此项目是用Java技术实现套打快递单功能,快递单模版是EMS,里面的代码比较简单,用的主要是BufferedImage读取模版,然后使用Graphics画图来实现对应关系,生成的是xps文件,可以直接在复印机或者打印机进行操作。
代码片段和文件信息
package cn.print;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
//import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import javax.imageio.ImageIO;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
public class PrintGif {
public static void main(String[] args) {
print();
}
public static void print(){
try{
DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
PrintService[] printers = PrintServiceLookup.lookupPrintServices(
flavor null);
for (int i = 0; i < printers.length; i++)
System.out.println(printers[i].getName());
PrintService printer = printers[0];
DocPrintJob job = printer.createPrintJob();
String filePath = Thread.currentThread().getContextClassLoader().getResource(“Image_00008.jpg“).getPath();
BufferedImage img =ImageIO.read(new File(filePath));
Graphics g1 = img.getGraphics();
Graphics g2 = img.getGraphics();
Graphics g3 = img.getGraphics();
Graphics g4 = img.getGraphics();
Graphics g5 = img.getGraphics();
Graphics g6 = img.getGraphics();
Graphics g7 = img.getGraphics();
Graphics g8 = img.getGraphics();
g1.setColor(Color.BLACK); //字体设为黑色
g2.setColor(Color.BLACK);
g3.setColor(Color.BLACK);
g4.setColor(Color.BLACK);
g5.setColor(Color.BLACK);
g6.setColor(Color.BLACK);
g7.setColor(Color.BLACK);
g8.setColor(Color.BLACK);
((Graphics2D) g1).rotate(-(3.1415926/2)105781); //逆时针选中90度相同坐标的文字
g1.drawString(“罗兵逊“ 105 781);
((Graphics2D) g2).rotate(-(3.1415926/2)105 618);
g2.drawString(“12345677890“ 105 618);
((Graphics2D) g3).rotate(-(3.1415926/2)135 775);
g3.drawString(“圣安东尼奥马刺GDP“135 775);
((Graphics2D) g4).rotate(-(3.1415926/2)153 793);
g4.drawString(“北京市东花市北里20号楼6单元501室“ 153 793);
((Graphics2D) g5).rotate(-(3.1415926/2)229 784);
g5.drawString(“大卫·罗兵逊“ 229 784);
((Graphics2D) g6).rotate(-(3.1415926/2)229 622);
g6.drawString(“78945612301“ 229 622);
((Graphics2D) g7).rotate(-(3.1415926/2)257 770);
g7.drawString(“俄克拉荷马雷霆杜兰特“ 257 770);
((Graphics2D) g8).rotate(-(3.1415926/2)278 784);
g8.drawString(“虹口区西康南路125弄34号201室 “ 278 784);
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
ImageIO.write(img “gif“ outstream);
byte[] buf = outstream.toByteArray();
InputStream stream = new ByteArrayInputStream(buf);
Doc doc = new SimpleDoc(stream flavor null);
System.out.println(doc);
// print
for(int i=0; i<1; i++){
job.print(doc null);
}
}catch(Exception e){
e.printStackTrace();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 301 2016-03-28 14:40 Print\.classpath
文件 381 2016-03-28 14:40 Print\.project
文件 598 2016-03-28 14:40 Print\.settings\org.eclipse.jdt.core.prefs
文件 4233 2016-03-28 15:16 Print\bin\cn\print\PrintGif.class
文件 155647 2016-03-28 14:43 Print\bin\Image_00008.jpg
文件 3106 2016-03-28 15:16 Print\src\cn\print\PrintGif.java
文件 155647 2016-03-28 14:43 Print\src\Image_00008.jpg
目录 0 2016-03-28 15:17 Print\bin\cn\print
目录 0 2016-03-28 15:17 Print\src\cn\print
目录 0 2016-03-28 15:17 Print\bin\cn
目录 0 2016-03-28 15:17 Print\src\cn
目录 0 2016-03-28 15:17 Print\.settings
目录 0 2016-03-28 15:17 Print\bin
目录 0 2016-03-28 15:17 Print\src
目录 0 2016-03-28 15:17 Print
----------- --------- ---------- ----- ----
319913 15
评论
共有 条评论