public interface FormatUtil
package test.zebra.sdk.printer.examples;
import java.util.HashMap;
import java.util.Map;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.comm.TcpConnection;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
import com.zebra.sdk.printer.ZebraPrinterLinkOs;
public class FormatUtilExample {
public static void main(String[] args) {
try {
new FormatUtilExample().example1();
new FormatUtilExample().example2();
new FormatUtilExample().example3();
} catch (ConnectionException e) {
e.printStackTrace();
}
}
// Print a stored format with the given variables. This ZPL will store a format on a printer, for use with example1.
// ^XA
// ^DFE:FORMAT1.ZPL
// ^FS
// ^FT26,243^A0N,56,55^FH\^FN12"First Name"^FS
// ^FT26,296^A0N,56,55^FH\^FN11"Last Name"^FS
// ^FT258,73^A0N,39,38^FH\^FDVisitor^FS
// ^BY2,4^FT403,376^B7N,4,0,2,2,N^FH^FDSerial Number^FS
// ^FO5,17^GB601,379,8^FS
// ^XZ
private void example1() throws ConnectionException {
Connection connection = new TcpConnection("192.168.1.32", TcpConnection.DEFAULT_ZPL_TCP_PORT);
try {
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
Map<Integer, String> vars = new HashMap<Integer, String>();
vars.put(12, "John");
vars.put(11, "Smith");
printer.printStoredFormat("E:FORMAT1.ZPL", vars);
} catch (ConnectionException e) {
e.printStackTrace();
} catch (ZebraPrinterLanguageUnknownException e) {
e.printStackTrace();
} finally {
connection.close();
}
}
// Print a stored format with the given variables. This ZPL will store a format on the Link-OS™ printer, for
// use with
// example2.
// ^XA
// ^DFE:FORMAT2.ZPL
// ^FS
// ^FT26,243^A0N,56,55^FH\^FN12"First Name"^FS
// ^FT26,296^A0N,56,55^FH\^FN11"Last Name"^FS
// ^FT258,73^A0N,39,38^FH\^FDVisitor^FS
// ^FO100,100^XG^FN13,1,1^FS
// ^FO5,17^GB601,379,8^FS
// ^XZ
private void example2() throws ConnectionException {
Connection connection = new TcpConnection("192.168.1.32", TcpConnection.DEFAULT_ZPL_TCP_PORT);
try {
connection.open();
Map<Integer, String> vars = new HashMap<Integer, String>();
vars.put(12, "John");
vars.put(11, "Smith");
vars.put(13, "R:PIC.GRF");
ZebraPrinter genericPrinter = ZebraPrinterFactory.getInstance(connection);
ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.createLinkOsPrinter(genericPrinter);
if (linkOsPrinter != null) {
linkOsPrinter.printStoredFormatWithVarGraphics("E:FORMAT2.ZPL", vars);
}
} catch (ConnectionException e) {
e.printStackTrace();
} catch (ZebraPrinterLanguageUnknownException e) {
e.printStackTrace();
} finally {
connection.close();
}
}
// Print a stored format with the given variables. This ZPL will store a format on a printer,
// for use with example3.
// This example also requires the ANMDS.TTF font to have been download to the printer prior to using this code.
// ^XA^DFE:FORMAT3.ZPL
// ^FS
// ^FT26,223^FH^A@N,56,55,E:ANMDS.TTF^CI28^FH\^FN12"Customer Name"^FS
// ^FT26,316^FH\^A@N,56,55,E:ANMDS.TTF^FH\^FN11"Invoice Number"^FS
// ^FT348,73^FH^A@N,39,38,E:ANMDS.TTF^FH\^FN13"Vendor Name^FS
// ^BY2,4^FT643,376^B7N,4,0,2,2,N^FH\^FDSerial Number^FS
// ^FO5,17^GB863,379,8^FS
// ^XZ
private void example3() throws ConnectionException {
Connection connection = new TcpConnection("192.168.1.32", TcpConnection.DEFAULT_ZPL_TCP_PORT);
try {
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
Map<Integer, String> vars = new HashMap<Integer, String>();
vars.put(12, "东风伟世通汽车饰件系统有限公司"); // Customer Name
vars.put(11, "订单号"); // Invoice Number
vars.put(13, "供应商名称"); // Vendor Name
printer.printStoredFormat("E:FORMAT3.ZPL", vars);
} catch (ConnectionException e) {
e.printStackTrace();
} catch (ZebraPrinterLanguageUnknownException e) {
e.printStackTrace();
} finally {
connection.close();
}
}
}
Modifier and Type | Method and Description |
---|---|
FieldDescriptionData[] |
getVariableFields(String formatString)
Returns a list of descriptors of the variable fields in this format.
|
void |
printStoredFormat(String formatPathOnPrinter,
Map<Integer,String> vars)
Prints a stored format on the printer, filling in the fields specified by the Map.
|
void |
printStoredFormat(String formatPathOnPrinter,
Map<Integer,String> vars,
String encoding)
Prints a stored format on the printer, filling in the fields specified by the Map.
|
void |
printStoredFormat(String formatPathOnPrinter,
String[] vars)
Prints a stored format on the printer, filling in the fields specified by the array.
|
void |
printStoredFormat(String formatPathOnPrinter,
String[] vars,
String encoding)
Prints a stored format on the printer, filling in the fields specified by the array.
|
void |
retrieveFormatFromPrinter(OutputStream formatData,
String formatPathOnPrinter)
Retrieves a format from the printer.
|
byte[] |
retrieveFormatFromPrinter(String formatPathOnPrinter)
Retrieves a format from the printer.
|
byte[] retrieveFormatFromPrinter(String formatPathOnPrinter) throws ConnectionException
.ZPL
files are supported. On a
CPCL printer, only .FMT
and .LBL
files are supported.formatPathOnPrinter
- the location of the file on the printer (e.g. "E:FORMAT.ZPL").ConnectionException
- if there is an issue communicating with the printer (e.g. the connection is not
open).void retrieveFormatFromPrinter(OutputStream formatData, String formatPathOnPrinter) throws ConnectionException
.ZPL
files are supported. On a
CPCL printer, only .FMT
and .LBL
files are supported.formatData
- The format.formatPathOnPrinter
- the location of the file on the printer (e.g. "E:FORMAT.ZPL").ConnectionException
- if there is an issue communicating with the printer (e.g. the connection is not
open).void printStoredFormat(String formatPathOnPrinter, String[] vars) throws ConnectionException
encoding
type. On a LinkOS/ZPL printer, only ZPL formats
are supported. On a CPCL printer, only CPCL formats are supported.formatPathOnPrinter
- the name of the format on the printer, including the extension (e.g. "E:FORMAT.ZPL").vars
- an array of strings representing the data to fill into the format. For LinkOS/ZPL printer formats,
index 0 of the array corresponds to field number 2 (^FN2). For CPCL printer formats, the variables are passed in
the order that they are found in the format.ConnectionException
- if an I/O error occurs.void printStoredFormat(String formatPathOnPrinter, String[] vars, String encoding) throws ConnectionException, UnsupportedEncodingException
encoding
type. eg. UTF-8. On a LinkOS/ZPL printer, only
ZPL formats are supported. On a CPCL printer, only CPCL formats are supported.formatPathOnPrinter
- the location of the file on the printer (e.g. "E:FORMAT.ZPL").vars
- an array of strings representing the data to fill into the format. For LinkOS/ZPL printer formats,
index 0 of the array corresponds to field number 2 (^FN2). For CPCL printer formats, the variables are passed in
the order that they are found in the format.encoding
- a character-encoding name (eg. UTF-8).ConnectionException
- if an I/O error occurs.UnsupportedEncodingException
- if the encoding is not supported.void printStoredFormat(String formatPathOnPrinter, Map<Integer,String> vars) throws ConnectionException
encoding
type. On a LinkOS/ZPL printer, only ZPL formats
are supported. On a CPCL printer, only CPCL formats are supported.formatPathOnPrinter
- the location of the file on the printer (e.g. "E:FORMAT.ZPL").vars
- a map which contains the key/value pairs for the stored format. For LinkOS/ZPL printer formats, the
key number should correspond directly to the number of the field in the format. For CPCL printer formats, the
values will be passed in ascending numerical order.ConnectionException
- if an I/O error occurs.void printStoredFormat(String formatPathOnPrinter, Map<Integer,String> vars, String encoding) throws ConnectionException, UnsupportedEncodingException
encoding
type. eg. UTF-8. On a LinkOS/ZPL printer, only
ZPL formats are supported. On a CPCL printer, only CPCL formats are supported.formatPathOnPrinter
- the location of the file on the printer (e.g. "E:FORMAT.ZPL").vars
- a map which contains the key/value pairs for the stored format. For LinkOS/ZPL printer formats, the
key number should correspond directly to the number of the field in the format. For CPCL printer formats, the
values will be passed in ascending numerical order.encoding
- a character-encoding name (e.g. UTF-8).ConnectionException
- if an I/O error occurs.UnsupportedEncodingException
- if the encoding is not supported.FieldDescriptionData[] getVariableFields(String formatString)
formatString
- the contents of the recalled format.FieldDescriptionData
for an example of how variable fields look.FieldDescriptionData
© 2016 ZIH Corp. All Rights Reserved.