RSS RSS feed | Atom Atom feed

Java Label Printing to DYMO Printers

Label printing

I have done many bar code printing projects in the past for Zebra printers.  In fact those projects were more complicated then the one I was going to work on today.  Today I just wanted to print out Salesforce contacts onto a DYMO LabelWriter 330, no barcodes, no images, nothing to complicated.  I wrote the part to pull our contact data from salesforce with their wonderful web services (If only they allowed you into ALL your data / Settings) but then ran into trouble with the printer.  It would work fine when prompting the user with the print dialog, even though my margins, paper size, layout, .... was all the same as the format returned from the prompt, it would not work.  Finally I just started a small trial and error, and after about 20 wasted labels I got it working.  Below is a small extract from the project to hopefully help people not waste there time like I did, and not waste labels / trees.

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.OrientationRequested;

public class PrintDymoLabel implements Printable
{
    protected String personName = null;
    protected String companyName = null;
    protected String companyAddress1 = null;
    protected String companyAddress2 = null;
    protected String city = null;
    protected String state = null;
    protected String zip = null;
   
    protected int copies = 1;
    protected String jobName;
   
   
    public PrintDymoLabel(String printerName, String personName, String companyName, String companyAddress1, String companyAddress2, String city, String state, String zip, int copies, String jobName)
    {
        super();
   
        // This should be broken up into funtions
        this.personName = personName;
        this.companyName = companyName;
        this.companyAddress1 = companyAddress1;
        this.companyAddress2 = companyAddress2;
        this.city = city;
        this.state = state;
        this.zip = zip;
        this.copies = copies;
        this.jobName = jobName;

        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(OrientationRequested.LANDSCAPE);
        aset.add(new Copies((copies<1?1:copies)));
        aset.add(new JobName((jobName==null?"My Labels":jobName), null));

        PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset);
        PrintService service = null;
       
        if (services.length > 0)
        {
            for (int i = 0; i < services.length; i++)
            {
                System.out.println(services[i].getName());
               
                if(service == null && services[i].getName().indexOf(printerName) != -1)
                {
                    service = services[i];
                }
            }
           
            if(service != null)
            {
                DocPrintJob pj = service.createPrintJob();

                try
                {
                    Doc myDoc = new SimpleDoc(this, flavor, null);

                    PrintRequestAttributeSet atSet = new HashPrintRequestAttributeSet();
                    MediaPrintableArea printableArea  = new MediaPrintableArea(0, 0, 612, 792, MediaPrintableArea.MM);
                    atSet.add(printableArea);
                    atSet.add(OrientationRequested.LANDSCAPE);
                    atSet.add(new JobName((jobName==null?"My Labels":jobName), null));
                    aset.add(new Copies((copies<1?1:copies)));
                    pj.print(myDoc, atSet);

                }
                catch (Exception e)
                {
                    System.err.println(e);
                    e.printStackTrace();
                }
            }
           
        }
    }

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
    {
        if (pageIndex == 0)
        {
            Graphics2D g = (Graphics2D) graphics;
            graphics.setClip(0, 0, 612, 792);

            g.setFont(new Font("Arial", Font.PLAIN, 10));
            // g.translate(pageFormat.getImageableX(),
            // pageFormat.getImageableY());
            g.translate(20, 20);
            g.drawString(personName, 5.0f, 0.0f);
            g.drawString(companyName, 5.0f, 12.0f);
            g.drawString(companyAddress1, 5.0f, 24.0f);
            if(companyAddress2 == null || companyAddress2.equals(""))
            {
                g.drawString(city +", " + state + " " + zip, 5.0f, 36.0f);
            }
            else
            {
                g.drawString(companyAddress2, 5.0f, 36.0f);
                g.drawString(city +", " + state + " " + zip, 5.0f, 48.0f);
               
            }
           
            return PAGE_EXISTS;
        }
        else
            return NO_SUCH_PAGE;
    }

    public static void main(String arg[])
    {
        try
        {
            // Read in from somewhere my case Salesforce Contact

            PrintDymoLabel sp = new PrintDymoLabel("DYMO", "Jeff Matthes", "Java Code LLC", "Somewhere",
                    null, "Madison", "WI", "53590", 1, "Label Print Job");
           
            System.out.println("Yea we got labels " + sp.toString());

        }
        catch (Exception e1)
        {
            // Never do this just for Example
            e1.printStackTrace();
        }
       
    }
}



Add a comment Send a TrackBack