如何用Java打印DYMO .label文件?

时间:2016-03-08 11:18:24

标签: java printing dymo

我为这个名为helloWorld.label的问题创建了一个非常简单的标签。我打算打印的真实标签更复杂,但为了简单起见,我们假设标签包含以下内容:

<?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>

我想要做的是用DYMO LabelWriter 450 Turbo打印这个标签,它应该打印一个如下所示的标签(101mm x54mm):

Correct label image

这是我尝试打印.label文件的内容:

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();
    }
}

但这只是打印一个空白标签。

我还能尝试什么?

0 个答案:

没有答案