Skip to main content
Skip table of contents

Generating and displaying bar codes

These examples were written in answer to questions such as:


barcodes

JAVA

JAVA
package sandbox.tables;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.Barcode;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import sandbox.WrapToTest;

@WrapToTest
public class Barcodes {
    public static final String DEST = "results/tables/barcode_table.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Barcodes().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(4);
        table.setWidthPercentage(100);
        for (int i = 0; i < 12; i++) {
            table.addCell(createBarcode(writer, String.format("%08d", i)));
        }
        document.add(table);
        document.close();
    }
    
    public static PdfPCell createBarcode(PdfWriter writer, String code) throws DocumentException, IOException {
        BarcodeEAN barcode = new BarcodeEAN();
        barcode.setCodeType(Barcode.EAN8);
        barcode.setCode(code);
        PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true);
        cell.setPadding(10);
        return cell;
    }
}


barcodeplacement

JAVA

JAVA
/**
 * This code sample was written by Bruno Lowagie in answer to this question:
 * http://stackoverflow.com/questions/30186774/2d-barcode-generation-issue-in-java
 */
package sandbox.barcodes;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BarcodePDF417;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class BarcodePlacement {
    public static final String DEST = "results/barcodes/barcode_placement.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new BarcodePlacement().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        Image img = createBarcode(cb, "This is a 2D barcode", 1, 1);
        document.add(new Paragraph(String.format("This barcode measures %s by %s user units", img.getScaledWidth(), img.getScaledHeight())));
        document.add(img);
        img = createBarcode(cb, "This is NOT a raster image", 3, 3);
        document.add(new Paragraph(String.format("This barcode measures %s by %s user units", img.getScaledWidth(), img.getScaledHeight())));
        document.add(img);
        img = createBarcode(cb, "This is vector data drawn on a PDF page", 1, 3);
        document.add(new Paragraph(String.format("This barcode measures %s by %s user units", img.getScaledWidth(), img.getScaledHeight())));
        document.add(img);
        document.close();
    }
    
    public Image createBarcode(PdfContentByte cb, String text, float mh, float mw) throws BadElementException {
        BarcodePDF417 pf = new BarcodePDF417();
        pf.setText("BarcodePDF417 barcode");
        Rectangle size = pf.getBarcodeSize();
        PdfTemplate template = cb.createTemplate(mw * size.getWidth(), mh * size.getHeight());
        pf.placeBarcode(template, BaseColor.BLACK, mh, mw);
        return Image.getInstance(template);
    }
}


stampbarcode

JAVA

JAVA
/*
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/26325712/itext-add-image-to-existing-document-using-itext-pdfstamper
 * Copyright 2014, iText Group NV
 */
package sandbox.stamper;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.Barcode;
import com.itextpdf.text.pdf.BarcodeEAN;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfTemplate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

/**
 * Example that shows how to watermark a less simple PDF.
 */
@WrapToTest
public class StampBarcode {
    /** The original PDF file. */
    public static final String SRC
        = "resources/pdfs/superman.pdf";
    /** The resulting PDF file. */
    public static final String DEST
        = "results/stamper/add_barcode.pdf";

    /**
     * Fills out an interactive form.
     * @param args  no arguments needed
     * @throws DocumentException
     * @throws IOException 
     */
    public static void main(String[] args)
            throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new StampBarcode().manipulatePdf(SRC, DEST);
    }
    
    /**
     * Fills out and flattens a form with the name, company and country.
     * @param src   the path to the original form
     * @param dest  the path to the filled out form
     * @throws DocumentException
     * @throws IOException 
     */
    public void manipulatePdf(String src, String dest)
            throws DocumentException, IOException {
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
        int n = reader.getNumberOfPages();
        Rectangle pagesize;
        for (int i = 1; i <= n; i++) {
            PdfContentByte over = stamper.getOverContent(i);
            pagesize = reader.getPageSize(i);
            float x = pagesize.getLeft() + 10;
            float y = pagesize.getTop() - 50;       
            BarcodeEAN barcode = new BarcodeEAN();
            barcode.setCodeType(Barcode.EAN8);
            String s = String.valueOf(i);
            s = "00000000".substring(s.length()) + s; 
            barcode.setCode(s);
            PdfTemplate template =
                    barcode.createTemplateWithBarcode(over, BaseColor.BLACK, BaseColor.BLACK);
            over.addTemplate(template, x, y);
        }
        stamper.close();
        reader.close();
    }
}


barcodeintable

JAVA

JAVA
/*
 * This code sample was written by Bruno Lowagie in answer to this question:
 * http://stackoverflow.com/questions/35011232
 */
package sandbox.barcodes;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class BarcodeInTable {
    public static final String DEST = "results/barcodes/barcode_in_table.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new BarcodeInTable().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        
        String code = "675-FH-A12";
        
        PdfContentByte cb = writer.getDirectContent();
        
        PdfPTable table = new PdfPTable(2);
        
        table.addCell("Change baseline:");
        Barcode128 code128 = new Barcode128();
        code128.setBaseline(-1);
        code128.setSize(12);
        code128.setCode(code);
        code128.setCodeType(Barcode128.CODE128);
        Image code128Image = code128.createImageWithBarcode(cb, null, null);
        PdfPCell cell = new PdfPCell(code128Image);
        table.addCell(cell);
        
        table.addCell("Add text and bar code separately:");
        code128 = new Barcode128();
        code128.setFont(null);
        code128.setCode(code);
        code128.setCodeType(Barcode128.CODE128);
        code128Image = code128.createImageWithBarcode(cb, null, null);
        cell = new PdfPCell();
        cell.addElement(new Phrase("PO #: " + code));
        cell.addElement(code128Image);
        table.addCell(cell);
        
        document.add(table);
        
        document.close();
    }
    
}


barcodebackground

JAVA

JAVA
/*
 * Example written in answer to:
 * http://stackoverflow.com/questions/35454039
 */
package sandbox.barcodes;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.Barcode128;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class BarcodeBackground {
    public static final String DEST = "results/barcodes/barcode_background.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new BarcodeBackground().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfContentByte canvas = writer.getDirectContent();
        Barcode128 code128 = new Barcode128();
        code128.setCode("12345XX789XXX");
        code128.setCodeType(Barcode128.CODE128);
        PdfTemplate template = code128.createTemplateWithBarcode(
                canvas, BaseColor.BLACK, BaseColor.BLACK);
        float x = 36;
        float y = 750;
        float w = template.getWidth();
        float h = template.getHeight();
        canvas.saveState();
        canvas.setColorFill(BaseColor.LIGHT_GRAY);
        canvas.rectangle(x, y, w, h);
        canvas.fill();
        canvas.restoreState();
        canvas.addTemplate(template, 36, 750);
        document.close();
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.