Package com.zebra.sdk.printer
Interface FormatUtil
- All Known Subinterfaces:
ZebraPrinter,ZebraPrinterLinkOs
public interface FormatUtil
Defines functions used for interacting with printer formats.
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");
ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.getLinkOsPrinter(connection);
if (linkOsPrinter != null) {
linkOsPrinter.printStoredFormatWithVarGraphics("E:FORMAT2.ZPL", vars);
}
} catch (ConnectionException 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();
}
}
}
-
Method Summary
Modifier and TypeMethodDescriptiongetVariableFields(String formatString) Returns a list of descriptors of the variable fields in this format.voidprintStoredFormat(String formatPathOnPrinter, String[] vars) Prints a stored format on the printer, filling in the fields specified by the array.voidprintStoredFormat(String formatPathOnPrinter, String[] vars, String encoding) Prints a stored format on the printer, filling in the fields specified by the array.voidprintStoredFormat(String formatPathOnPrinter, Map<Integer, String> vars) Prints a stored format on the printer, filling in the fields specified by the Map.voidPrints a stored format on the printer, filling in the fields specified by the Map.voidretrieveFormatFromPrinter(OutputStream formatData, String formatPathOnPrinter) Retrieves a format from the printer.byte[]retrieveFormatFromPrinter(String formatPathOnPrinter) Retrieves a format from the printer.
-
Method Details
-
retrieveFormatFromPrinter
Retrieves a format from the printer. On a LinkOS/ZPL printer, only.ZPLfiles are supported. On a CPCL printer, only.FMTand.LBLfiles are supported.- Parameters:
formatPathOnPrinter- the location of the file on the printer (e.g. "E:FORMAT.ZPL").- Returns:
- the contents of the format file.
- Throws:
ConnectionException- if there is an issue communicating with the printer (e.g. the connection is not open).
-
retrieveFormatFromPrinter
void retrieveFormatFromPrinter(OutputStream formatData, String formatPathOnPrinter) throws ConnectionException Retrieves a format from the printer. On a LinkOS/ZPL printer, only.ZPLfiles are supported. On a CPCL printer, only.FMTand.LBLfiles are supported.- Parameters:
formatData- The format.formatPathOnPrinter- the location of the file on the printer (e.g. "E:FORMAT.ZPL").- Throws:
ConnectionException- if there is an issue communicating with the printer (e.g. the connection is not open).
-
printStoredFormat
Prints a stored format on the printer, filling in the fields specified by the array. The values of any format variables will be encoded using the defaultencodingtype. On a LinkOS/ZPL printer, only ZPL formats are supported. On a CPCL printer, only CPCL formats are supported.
See java.lang.String for more information about encoding types.- Parameters:
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.- Throws:
ConnectionException- if an I/O error occurs.
-
printStoredFormat
void printStoredFormat(String formatPathOnPrinter, String[] vars, String encoding) throws ConnectionException, UnsupportedEncodingException Prints a stored format on the printer, filling in the fields specified by the array. The values of any format variables will be encoded using the providedencodingtype. eg. UTF-8. On a LinkOS/ZPL printer, only ZPL formats are supported. On a CPCL printer, only CPCL formats are supported.
See java.lang.String for more information about encoding types.- Parameters:
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).- Throws:
ConnectionException- if an I/O error occurs.UnsupportedEncodingException- if the encoding is not supported.
-
printStoredFormat
void printStoredFormat(String formatPathOnPrinter, Map<Integer, String> vars) throws ConnectionExceptionPrints a stored format on the printer, filling in the fields specified by the Map. The values of any format variables will be encoded using the defaultencodingtype. On a LinkOS/ZPL printer, only ZPL formats are supported. On a CPCL printer, only CPCL formats are supported.
See java.lang.String for more information about encoding types.- Parameters:
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.- Throws:
ConnectionException- if an I/O error occurs.
-
printStoredFormat
void printStoredFormat(String formatPathOnPrinter, Map<Integer, String> vars, String encoding) throws ConnectionException, UnsupportedEncodingExceptionPrints a stored format on the printer, filling in the fields specified by the Map. The values of any format variables will be encoded using the providedencodingtype. eg. UTF-8. On a LinkOS/ZPL printer, only ZPL formats are supported. On a CPCL printer, only CPCL formats are supported.
See java.lang.String for more information about encoding types.- Parameters:
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).- Throws:
ConnectionException- if an I/O error occurs.UnsupportedEncodingException- if the encoding is not supported.
-
getVariableFields
Returns a list of descriptors of the variable fields in this format. On a LinkOS/ZPL printer, only ZPL formats are supported. On a CPCL printer, only CPCL formats are supported.- Parameters:
formatString- the contents of the recalled format.- Returns:
- a list of field data descriptors. For a CPCL printer, the nth element of the list will contain the
integer n and no name. For a LinkOS/ZPL printer, each element will contain an ^FN number and a variable name if
present. If the format contains multiple ^FNs with the same number, only the last one will be in the result.
SeeFieldDescriptionDatafor an example of how variable fields look. - See Also:
-