Skip to content

SotirisSapak/ZPLToJava

Repository files navigation

ZPLToJava

A java library to create ZPL label code with more easy and READABLE way.

IMPLEMENTATION

Is a java and not an android library - module, so you have to download .jar file from the releases section. Import it to your java or android project at app\libs path.
Also, you have to enable .jar files from the libs folder at the app gradle file like this:

dependencies{
  ...
  implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
  implementation files('libs/ZPLJava.jar')
  ...
}

HOW TO USE IT

First things first...
A ZPL label has a base... a label. So you have to create an instance of the label component.

// ZPLJavaLabel() requires 3 parameters:
// 3                -> inchesWidth
// 2                -> inchesHeight
// LabelSize._8DPMM -> A value from LabelSize interface
ZPLJavaLabel label = new ZPLJavaLabel(3, 2, LabelSize._8DPMM); 

Let's say for example we want to add a text in our label

// create the label instance with (width: 2, height: 3, labelSize: 8DPMM)
ZPLJavaLabel mainLabel = new ZPLJavaLabel(2, 3, LabelSize._8DPMM);
// create a new text instance
ZPLJavaText text = new ZPLJavaText();
// give this text an id (optional) - the generated code will include as a comment the id
// of the component that is generated
text.setId("ourFirstText");
// IMPORTANT: give text the label width and height information to do some inner
// calculations
text.setLabelSize(mainLabel.getLabelWidth(), mainLabel.getLabelHeight());
// Include the most basic text!
text.setText("Hello world");
// Give 20 px font size (optional)
text.setFontSize(20);

// in order to place the above text to label, you have to add this in the label by using the
// addComponent(ZPLJavaComponent) method
mainLabel.addComponent(text);

// of course, generate the label code
mainLabel.generateLabelCode();
// Log it!
Log.d("LABEL#TEXT", mainLabel.getLabelCode());

The above java code will generate this ZPL code:

^XA
    
   ^FX ZPL code generated by { ZPLJavaLabel } class
   ^FX Please do not modify any parameter or will change the label appearance
    
   ^FX Label values: 
   ^FX ---------------------------------
   ^FX label total width: 406 {2 inches}
   ^FX label total height: 609 {3 inches}
   ^FX ---------------------------------
    
   ^FX Label structure 
   ^FX ---------------------------------
   ^FX Enable UTF-8 format
   ^CI28
   ^FX  Generate {ourFirstText} component
   ^FO0,0^A0,20^FB406,1,0,L,0^FH_^FDHello world\&^FS
   ^FX  -----------------------------------------------
^XZ

Use this Labelary Online ZPL Viewer http://labelary.com/viewer.html to preview your results!

HINT: Don't forget to change the label configuration at labelary!

So, at this point, we have created a label with a text in top left corner of the screen. In order to place the text at the center of the label you have to calculate the total label width, the text width and the font size.Then you have to ...ahh just kidding!

text.setAlignment(ZPLJavaComponent.POSITION_CENTER);

There are five different positions you can call:

ZPLJavaComponent.POSITION_LEFT    // default
ZPLJavaComponent.POSITION_RIGHT
ZPLJavaComponent.POSITION_CENTER
ZPLJavaComponent.POSITION_BOTTOM
ZPLJavaComponent.POSITION_JUSTIFIED
SUPER IMPORTANT: Use setAlignment() method only after setting the label size by calling setLabelSize() method at your code.

Extending our example, let's say we want to add a new text below the "Hello world" at the END of the label and a barcode with data "123456" at the bottom center of the label! A little tricky or maybe not...

// create the label instance with (width: 2, height: 3, labelSize: 8DPMM)
ZPLJavaLabel mainLabel = new ZPLJavaLabel(2, 3, LabelSize._8DPMM);
// create a new text instance
ZPLJavaText text = new ZPLJavaText();
// give this text an id (optional) - the generated code will include as a comment the id
// of the component that is generated
text.setId("ourFirstText");
// IMPORTANT: give text the label width and height information to do some inner
// calculations
text.setLabelSize(mainLabel.getLabelWidth(), mainLabel.getLabelHeight());
// add alignment to text after setting the label size to the text ... that's why you have
// to add the label total size to any ZPLComponent.
text.setAlignment(ZPLJavaComponent.POSITION_CENTER);
// Include the most basic text!
text.setText("Hello world");
// Give 20 px font size (optional)
text.setFontSize(20);

// create a new text instance
ZPLJavaText textBelowHelloWorld = new ZPLJavaText();
textBelowHelloWorld.setId("textBelowHelloWorld");
textBelowHelloWorld.setLabelSize(mainLabel.getLabelWidth(), mainLabel.getLabelHeight());
textBelowHelloWorld.setAlignment(ZPLJavaComponent.POSITION_CENTER);

// call belowOf(anotherComponent) to place a component below another
textBelowHelloWorld.belowOf(text);

textBelowHelloWorld.setText("The text below the Hello World");
textBelowHelloWorld.setFontSize(14);

// create a new barcode instance
ZPLJavaBarcode barcode = new ZPLJavaBarcode("123456");
barcode.setId("barcodeBottomCenter");
barcode.setLabelSize(mainLabel.getLabelWidth(), mainLabel.getLabelHeight());

barcode.setAlignment(ZPLJavaComponent.POSITION_CENTER);
// can use as many alignments as you want, but in order to work properly you have to add
// one for the vertical axis        -> POSITION_BOTTOM
// and one for the horizontal axis  -> POSITION_CENTER
barcode.setAlignment(ZPLJavaComponent.POSITION_BOTTOM);


// to add quickly multiple components into label call:
mainLabel.addAllComponents(text, textBelowHelloWorld, barcode);

// of course, generate the label code
mainLabel.generateLabelCode();
// Log it!
Log.d("LABEL#TEXT", mainLabel.getLabelCode());

RESULT:

^XA
    
    ^FX ZPL code generated by { ZPLJavaLabel } class
    ^FX Please do not modify any parameter or will change the label appearance
    
    ^FX Label values: 
    ^FX ---------------------------------
    ^FX label total width: 406 {2 inches}
    ^FX label total height: 609 {3 inches}
    ^FX ---------------------------------
    
    ^FX Label structure 
    ^FX ---------------------------------
    ^FX Enable UTF-8 format
    ^CI28
    ^FX  Generate {ourFirstText} component
    ^FO0,0^A0,20^FB406,1,0,C,0^FH_^FDHello world\&^FS
    ^FX  Generate {textBelowHelloWorld} component
    ^FO0,20^A0,14^FB406,1,0,C,0^FH_^FDThe text below the Hello World\&^FS
    ^FX  Generate {barcodeBottomCenter} component
    ^FO53,549^BY 3^BCN,60,N,N,N,N^FD123456^FS
    ^FX  -----------------------------------------------
^XZ

Maybe i want to add some margins at the "Hello world" to be 20px from the top of the label and the barcode to be 20px above the bottom. Simple:

...
...
text.setMarginTop(20);
...
...
barcode.setMarginBottom(20);
...
// prefer to add margins when you have fully created and setting the component...
// add margins at the end of the component's code.

Result:

^XA
    
    ^FX ZPL code generated by { ZPLJavaLabel } class
    ^FX Please do not modify any parameter or will change the label appearance
    
    ^FX Label values: 
    ^FX ---------------------------------
    ^FX label total width: 406 {2 inches}
    ^FX label total height: 609 {3 inches}
    ^FX ---------------------------------
    
    ^FX Label structure 
    ^FX ---------------------------------
    ^FX Enable UTF-8 format
    ^CI28
    ^FX  Generate {ourFirstText} component
    ^FO0,20^A0,20^FB406,1,0,C,0^FH_^FDHello world\&^FS
    ^FX  Generate {textBelowHelloWorld} component
    ^FO0,40^A0,14^FB406,1,0,C,0^FH_^FDThe text below the Hello World\&^FS
    ^FX  Generate {barcodeBottomCenter} component
    ^FO53,529^BY 3^BCN,60,N,N,N,N^FD123456^FS
    ^FX  -----------------------------------------------
^XZ

There are many more methods to play with...so feel free to explore this library!

Important

This library was not tested in real ZPL printer.

Copyright

Copyright 2022 SotirisSapak

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

This is a java library to create ZPL labels by using inner library components.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages