Drawing labeled Code 128, Code 39, UPC-A and EAN-13 linear barcodes, and barcodes drawn from top to bottom and from bottom to top.
Example_36.pdf — the PDF this example creates.
package examples;
import java.io.*;
import com.pdfjet.*;
import com.pdfjet.fonts.*;
import com.pdfjet.barcodes.*;
/**
* Example_36.java
* This example draws a Code 128, a Code 39, a UPC-A and an EAN-13 barcode,
* each next to a label, and then barcodes drawn from top to bottom and from
* bottom to top.
*/
public class Example_36 {
public Example_36() throws Exception {
PDF pdf = new PDF(
new BufferedOutputStream(new FileOutputStream("Example_36.pdf")));
pdf.setCompliance(Compliance.PDF_UA_1);
pdf.setTitle("Linear Barcodes");
Font f1 = new Font(pdf, IBMPlexSans.Regular);
f1.setSize(12f);
Font f2 = new Font(pdf, IBMPlexSans.SemiBold);
f2.setSize(12f);
Page page = new Page(pdf, Letter.PORTRAIT);
TextLine text = new TextLine(f2, "Linear Barcodes");
text.setStructureType(StructElem.H1);
text.setFontSize(22f);
text.setLocation(70f, 80f);
text.drawOn(page);
String[] labels = {
"Code 128",
"Code 39",
"UPC-A",
"EAN-13",
"GS1-128",
};
String[] notes = {
"Letters, digits and symbols",
"Upper case letters and digits",
"11 digits, the check digit is added",
"12 digits, the check digit is added",
"GS1 data: a GTIN and a batch number",
};
Barcode[] barcodes = {
new Barcode(Barcode.CODE_128, "Hellö, World!"),
new Barcode(Barcode.CODE_39, "WIKIPEDIA"),
new Barcode(Barcode.UPC_A, "51234567890"),
new Barcode(Barcode.EAN_13, "051234567890"),
new Barcode(Barcode.GS1_128, "(01)09506000134352(10)ABC123"),
};
// UPC-A and EAN-13 need wider bars for the digits under them.
float[] moduleLengths = {0.75f, 0.75f, 1f, 1f, 0.75f};
float y = 130f;
for (int i = 0; i < barcodes.length; i++) {
new TextLine(f2, labels[i]).setLocation(70f, y + 15f).drawOn(page);
TextLine note = new TextLine(f1, notes[i]);
note.setFontSize(10f);
note.setTextColor(Color.dimgray);
note.setLocation(70f, y + 32f);
note.drawOn(page);
Barcode barcode = barcodes[i];
barcode.setLocation(290f, y);
barcode.setModuleLength(moduleLengths[i]);
barcode.setFont(f1);
float[] xy = barcode.drawOn(page);
y = xy[1] + 30f;
}
// The same barcodes can be drawn from top to bottom and from bottom to top.
new TextLine(f2, "Vertical barcodes").setLocation(70f, y + 15f).drawOn(page);
Barcode barcode = new Barcode(Barcode.CODE_128, "G86513JVW0C");
barcode.setLocation(70f, y + 35f);
barcode.setModuleLength(0.75f);
barcode.setDirection(Direction.TOP_TO_BOTTOM);
barcode.setFont(f1);
float[] xy = barcode.drawOn(page);
barcode = new Barcode(Barcode.CODE_39, "CODE39");
barcode.setLocation(xy[0] + 60f, y + 35f);
barcode.setModuleLength(0.75f);
barcode.setDirection(Direction.BOTTOM_TO_TOP);
barcode.setFont(f1);
xy = barcode.drawOn(page);
barcode = new Barcode(Barcode.EAN_13, "051234567890");
barcode.setLocation(xy[0] + 60f, y + 35f);
barcode.setModuleLength(1f);
barcode.setDirection(Direction.BOTTOM_TO_TOP);
barcode.setFont(f1);
barcode.drawOn(page);
// ITF-14, the GTIN of a carton, framed by bearer bars.
Page page2 = new Page(pdf, Letter.PORTRAIT);
TextLine heading2 = new TextLine(f2, "Barcodes for Cartons");
heading2.setStructureType(StructElem.H2);
heading2.setFontSize(18f);
heading2.setLocation(70f, 80f);
heading2.drawOn(page2);
TextLine label = new TextLine(f2, "ITF-14");
label.setLocation(70f, 135f);
label.drawOn(page2);
TextLine cartonNote = new TextLine(f1, "The GTIN of a carton, 13 digits and the check digit PDFjet adds, framed by bearer bars");
cartonNote.setFontSize(10f);
cartonNote.setTextColor(Color.dimgray);
cartonNote.setLocation(70f, 152f);
cartonNote.drawOn(page2);
Barcode itf = new Barcode(Barcode.ITF_14, "1540014128876");
itf.setLocation(70f, 175f);
itf.setModuleLength(1f);
itf.setFont(f1);
itf.drawOn(page2);
pdf.complete();
}
public static void main(String[] args) throws Exception {
long time0 = System.currentTimeMillis();
new Example_36();
long time1 = System.currentTimeMillis();
System.out.printf("Example_36 => %4d ms%n", time1 - time0);
}
} // End of Example_36.java
©2026 PDFjet Software.