I am trying to print barcode generated by barcode4j on Dymo printer. My barcode generator will give me the barcode as byte array which I am converting into inputstream and passing it to the print method. But somehow the printer is not able to print it properly. It looks like I need to setup the PrintRequestAttributeSet properly in order to get it printed as expected. What are the correct print attributes for printing barcodes on label printer. Here is my code. Is there a way to auto detect this PrintRequestAttributeSet within the program.
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
public class HelloWorldPrinter implements Printable, ActionListener {
public static void main(String args[]) throws PrintException, FileNotFoundException {
HelloWorldPrinter hwp = new HelloWorldPrinter();
hwp.printMe();
}
public void printMe() throws PrintException, FileNotFoundException{
Map<String, String> params = new HashMap<String, String>();
params.put("type", "Interleaved2of5");
params.put("msg", "0002221845");
NBPTSBarcodeGenerator nbg = new NBPTSBarcodeGenerator();
InputStream in = new ByteArrayInputStream(nbg.generateBarcode(params).toString().getBytes());
// find the printing service
AttributeSet attributeSet = new HashAttributeSet();
attributeSet.add(new PrinterName("DYMO LabelWriter 450 Turbo", null));
attributeSet.add(new Copies(1));
PrintService[] pservices = PrintServiceLookup.lookupPrintServices(
null, attributeSet);
PrintService pservice = pservices[0];
DocFlavor[] flavors = pservice.getSupportedDocFlavors();
for (int i = 0; i < flavors.length; i++)
{
System.out.println("\t" + flavors);
}
//create document
Doc doc = new SimpleDoc(in, DocFlavor.INPUT_STREAM.PDF, null);
DocPrintJob job = pservice.createPrintJob();
// monitor print job events
PrintJobWatcher watcher = new PrintJobWatcher(job);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
aset.add(MediaSizeName.ISO_A6);
System.out.println("Printing...");
job.print(doc, aset);
// wait for the job to be done
watcher.waitForDone();
System.out.println("Job Completed!!");
}
private static void printService(PrintService[] services) {
if (services!=null && services.length>0) {
for (int i = 0; i < services.length; i++) {
System.out.println("t" + services[i]);
}
}
}
class PrintJobWatcher {
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
System.out.println("Printing done ...");
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}
}