3

I've created a very simple label for this question called helloWorld.label. The real labels I intend to print are more complex but to keep things simple let's just assume that the label contains the following content:

<?xml version="1.0" encoding="utf-8"?>
<DieCutLabel Version="8.0" Units="twips">
  <PaperOrientation>Landscape</PaperOrientation>
  <Id>Shipping</Id>
  <PaperName>Hello world</PaperName>
  <DrawCommands>
    <RoundRectangle X="0" Y="0" Width="3060" Height="5715" Rx="270" Ry="270"/>
  </DrawCommands>
  <ObjectInfo>
    <AddressObject>
      <Name>Address</Name>
      <ForeColor Alpha="255" Red="0" Green="0" Blue="0"/>
      <BackColor Alpha="0" Red="255" Green="255" Blue="255"/>
      <LinkedObjectName></LinkedObjectName>
      <Rotation>Rotation0</Rotation>
      <IsMirrored>False</IsMirrored>
      <IsVariable>True</IsVariable>
      <HorizontalAlignment>Left</HorizontalAlignment>
      <VerticalAlignment>Middle</VerticalAlignment>
      <TextFitMode>ShrinkToFit</TextFitMode>
      <UseFullFontHeight>True</UseFullFontHeight>
      <Verticalized>False</Verticalized>
      <StyledText>
        <Element>
          <String>Hello world</String>
          <Attributes>
            <Font Family="Arial" Size="22" Bold="False" Italic="False" Underline="False" Strikeout="False"/>
            <ForeColor Alpha="255" Red="0" Green="0" Blue="0"/>
          </Attributes>
        </Element>
      </StyledText>
      <ShowBarcodeFor9DigitZipOnly>False</ShowBarcodeFor9DigitZipOnly>
      <BarcodePosition>Suppress</BarcodePosition>
      <LineFonts/>
    </AddressObject>
    <Bounds X="307" Y="58" Width="5265" Height="2918"/>
  </ObjectInfo>
</DieCutLabel>

What I'd like to do is print this label with a DYMO LabelWriter 450 Turbo, it should print a label (101mm x54mm) that looks like this:

Correct label image

Here's what I'm trying to print the .label file:

import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import java.awt.print.PrinterJob;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws PrintException, IOException {
        PrintService[] ps = PrinterJob.lookupPrintServices();
        if (ps.length == 0) {
            throw new IllegalStateException("No Printer found");
        }
        System.out.println("Available printers: " + Arrays.asList(ps));

        PrintService myService = null;
        for (PrintService printService : ps) {
            if (printService.getName().equals("DYMO_LabelWriter_450_Turbo")) {
                myService = printService;
                break;
            }
        }

        if (myService == null) {
            throw new IllegalStateException("Printer not found");
        }

        FileInputStream fis = new FileInputStream("/Users/<REDACTED>/IdeaProjects/<REDACTED>/helloWorld.label");
        Doc labelDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
        DocPrintJob printJob = myService.createPrintJob();
        printJob.print(labelDoc, new HashPrintRequestAttributeSet());
        fis.close();
    }
}

But this simply prints a blank label.

What else can I try?

narzero
  • 2,199
  • 5
  • 40
  • 73

0 Answers0