import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.print.*; import java.lang.reflect.InvocationTargetException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; /** * * @author gwarner */ public class LabelPrinter implements Printable { JComponent comp; JFrame frame; public LabelPrinter() { frame = new JFrame("label test"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); // printPanel is the component which will get printed: JPanel printPanel = new JPanel(); Dimension printSize = new Dimension(144, 54); printPanel.setSize(printSize); printPanel.setPreferredSize(printSize); printPanel.setMinimumSize(printSize); printPanel.setMaximumSize(printSize); printPanel.setLayout(new BoxLayout(printPanel, BoxLayout.PAGE_AXIS)); printPanel.setBorder(BorderFactory.createEmptyBorder(0, 18, 0, 0)); printPanel.setBackground(Color.WHITE); Font font = new Font("Arial", Font.PLAIN, 8); JLabel patientLabel = new JLabel("PATIENT: Doe, John"); patientLabel.setFont(font); patientLabel.setAlignmentX(Component.LEFT_ALIGNMENT); printPanel.add(patientLabel); JLabel ssnLabel = new JLabel("SSN: 123-45-6789 DOB: 01/01/08"); ssnLabel.setFont(font); ssnLabel.setAlignmentX(Component.LEFT_ALIGNMENT); printPanel.add(ssnLabel); JLabel idLabel = new JLabel("ID #: T1234"); idLabel.setFont(font); idLabel.setAlignmentX(Component.LEFT_ALIGNMENT); printPanel.add(idLabel); // Store this component for printing later. comp = printPanel; // The purpose of viewPanel is just to put a little border around the preview onscreen. This border shouldn't be printed, though. JPanel viewPanel = new JPanel(); viewPanel.setBorder(BorderFactory.createEmptyBorder(9, 0, 9, 18)); viewPanel.setBackground(Color.WHITE); viewPanel.setAlignmentX(Component.CENTER_ALIGNMENT); viewPanel.add(comp); panel.add(viewPanel); JButton printButton = new JButton("Print"); printButton.setAlignmentX(Component.CENTER_ALIGNMENT); printButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { PrinterJob job = PrinterJob.getPrinterJob(); if (job.printDialog()) { try { PageFormat format = job.defaultPage(); Paper paper = format.getPaper(); paper.setSize(54.0, 144.0); paper.setImageableArea(0.0, 0.0, 54.0, 144.0); format.setPaper(paper); format.setOrientation(PageFormat.LANDSCAPE); Book book = new Book(); book.append(LabelPrinter.this, format); job.setPageable(book); job.print(); } catch (PrinterException ex) { Logger.getLogger(LabelPrinter.class.getName()).log(Level.SEVERE, null, ex); } } } }); panel.add(printButton); frame.add(panel); try { SwingUtilities.invokeAndWait(new Runnable() { @Override public void run() { frame.pack(); frame.setVisible(true); } }); } catch (InterruptedException ex) { Logger.getLogger(LabelPrinter.class.getName()).log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { Logger.getLogger(LabelPrinter.class.getName()).log(Level.SEVERE, null, ex); } } public static void main(String[] args) { final LabelPrinter label = new LabelPrinter(); } @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { return NO_SUCH_PAGE; } else { Graphics2D g2d = (Graphics2D) graphics; g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); disableDoubleBuffering(comp); comp.printAll(g2d); enableDoubleBuffering(comp); return PAGE_EXISTS; } } public static void disableDoubleBuffering(JComponent c) { RepaintManager currentManager = RepaintManager.currentManager(c); currentManager.setDoubleBufferingEnabled(false); } public static void enableDoubleBuffering(JComponent c) { RepaintManager currentManager = RepaintManager.currentManager(c); currentManager.setDoubleBufferingEnabled(true); } }
It looks fine on the screen:

However, when I print it, there are 2 problems: The 'n' on the name line is getting cut off, and the space between the SSN and the DOB has disappeared: (I've rotated the image 90 degrees since the label is printed in landscape, and it shows up vertically on the PDF. This orientation is correct.) I'm using PrimoPDF to simulate a printer here by capturing the print job as a PDF file. What's interesting is the I-beam cursor selects as though the space and the 'n' are still there, they're just invisible in the case of the 'n', and of negative width in the case of the space.

Likewise, when I actually print it, you can see that the 'n' is still cut off, though not quite as much, but the space between the SSN and the DOB is still missing:

I'm printing this on a Dymo LabelWriter 330 Turbo.
What's weird to me is that the 1st line is cutting off at the end, but the 2nd line is ignoring my space in the middle of the string, which seems strange since it should cut off only at the end if it was some sort of margin width or container width issue. There's also nothing wrong with the 3rd line. In other words, the behavior doesn't seem consistent. Any ideas what's causing this?