0

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) {
            }
          }
    }
}
  • Take a look at http://stackoverflow.com/questions/11803741/printing-in-java-to-label-printer/11805237#11805237 for an example – MadProgrammer Oct 09 '14 at 01:10
  • I have seen that code already. It actually shows a popup window to select all the settings and printer. Whereas I dont want to show any popup and ask the user to select printer. Instead I just want to print the barcode silently when user click on a button. Because I have hard coded the printer name and everything. I just want the print it simply. I cannot use the code like this `PrinterJob job = PrinterJob.getPrinterJob();job.setPrintable(b);if(job.printDialog())' in my application. – user3888824 Oct 09 '14 at 01:19
  • Then you need to learn how printing works, see [Lesson: Printing](http://docs.oracle.com/javase/tutorial/2d/printing/) for more details. You might find how to stop the printer dialog from been shown...otherwise, from what I understand, the reset of the answer should provide you with a basic working example.. – MadProgrammer Oct 09 '14 at 01:24

0 Answers0