Add Barcode and QR Code to PDF with iText
Add Barcode 39 to PDF
Code 39 is a variable length, discrete barcode symbology. The code 39 has 43 characters, from uppercase letters (A through Z), numeric digits (0 through 9) and a number of special characters (., -, /, +, $, and space). Here is an example how to create and add a code 39 barcode to a PDF document.
Barcode39 barcode39 = new Barcode39();
barcode39.setCode("123456789");
Image code39Image = barcode39.createImageWithBarcode(cb, null, null);
document.add(code39Image);
Add Barcode 128 to PDF
Code 128 is a high-density barcode symbology. It is used for alphanumeric or numeric-only barcodes. It can encode all 128 characters of ASCII. Here is an example how to create and add a code 128 barcode to a PDF document.
Barcode128 barcode128 = new Barcode128();
barcode128.setCode("memorynotfound.com");
barcode128.setCodeType(Barcode.CODE128);
Image code128Image = barcode128.createImageWithBarcode(cb, null, null);
document.add(code128Image);
Add Barcode EAN to PDF
The International Article Number (EAN) – also known as European Article Number – is a 13-digit barcode symbology. Here is an example how to create and add a EAN Barcode to a PDF document.
BarcodeEAN barcodeEAN = new BarcodeEAN();
barcodeEAN.setCode("3210123456789");
barcodeEAN.setCodeType(Barcode.EAN13);
Image codeEANImage = barcodeEAN.createImageWithBarcode(cb, null, null);
document.add(codeEANImage);
Add QR Code to PDF
QR code – or Quick Response Code – is a type of matrix barcode, or two dimensional barcode. A QR code uses four standardized encoding modes: numeric, alphanumeric, byte/binary and kanji to store data. QR Code has a greater storage capacity compared to the other barcodes. The following is an example how to create and add a QR Code inside a PDF document.
BarcodeQRCode barcodeQRCode = new BarcodeQRCode("https://memorynotfound.com", 1000, 1000, null);
Image codeQrImage = barcodeQRCode.getImage();
codeQrImage.scaleAbsolute(100, 100);
document.add(codeQrImage);
Add Barcode and QR Code to PDF Document
Previously, we evaluated different Barcodes and QR Codes. In this example we put them all together and create a PDF documents containing different barcodes on every page.
package com.memorynotfound.pdf.itext;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
import java.io.IOException;
public class CreateBarcodePdf {
public static void main(String... args) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("barcode.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
Barcode128 barcode128 = new Barcode128();
barcode128.setCode("memorynotfound.com");
barcode128.setCodeType(Barcode.CODE128);
Image code128Image = barcode128.createImageWithBarcode(cb, null, null);
document.add(code128Image);
document.newPage();
Barcode39 barcode39 = new Barcode39();
barcode39.setCode("123456789");
Image code39Image = barcode39.createImageWithBarcode(cb, null, null);
document.add(code39Image);
document.newPage();
BarcodeEAN barcodeEAN = new BarcodeEAN();
barcodeEAN.setCode("3210123456789");
barcodeEAN.setCodeType(Barcode.EAN13);
Image codeEANImage = barcodeEAN.createImageWithBarcode(cb, null, null);
document.add(codeEANImage);
document.newPage();
BarcodeQRCode barcodeQRCode = new BarcodeQRCode("https://memorynotfound.com", 1000, 1000, null);
Image codeQrImage = barcodeQRCode.getImage();
codeQrImage.scaleAbsolute(100, 100);
document.add(codeQrImage);
document.close();
}
}