1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
import java.io.*;
import java.util.regex.*;
import javax.print.*;
import javax.print.attribute.*;
public class UPCAs {
public static void main(String[] args) throws Exception {
if (args.length!=2)
throw new Exception("Requires two arguments");
String template="";
try {
BufferedReader in=new BufferedReader(new FileReader("barcode.ps"));
String line;
while ((line=in.readLine())!=null)
template+=line+"\n";
in.close();
} catch (IOException e) {
throw new Exception("File not found");
}
Matcher m=Pattern.compile(
"(?s).*% --BEGIN TEMPLATE--(.*)"+
"% --END TEMPLATE--.*").matcher(template);
if (!m.matches())
throw new Exception("Unable to parse out template");
String contents="%!PS-Adobe-2.0\n"+m.group(1);
for (int i=Integer.parseInt(args[0]), j=0;
i<Integer.parseInt(args[1]); i++, j++) {
int x=100+150*(j/7);
int y=100+100*(j%7);
contents+="gsave\n";
contents+=x+" "+y+" translate\n";
contents+="(9781860"+i+") () ean13 barcode\n";
contents+="grestore\n";
}
contents+="showpage\n";
try {
ByteArrayInputStream in=new ByteArrayInputStream(contents.getBytes());
DocFlavor flavor=DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet pras=
new HashPrintRequestAttributeSet();
PrintService service=PrintServiceLookup.
lookupPrintServices(flavor,pras)[0];
if (service==null)
throw new Exception("Could not locate printer");
service.createPrintJob().print(new SimpleDoc(in,flavor,new HashDocAttributeSet()),pras);
} catch (PrintException e) {
throw new Exception("Failed to print");
}
}
}
|