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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.tables;

import com.itextpdf.barcodes.BarcodeEAN;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

public class Barcodes {
    public static final String DEST = "./target/sandbox/tables/barcodes.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new Barcodes().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Table table = new Table(UnitValue.createPercentArray(4)).useAllAvailableWidth();

        for (int i = 0; i < 12; i++) {
            table.addCell(createBarcode(String.format("%08d", i), pdfDoc));
        }

        doc.add(table);

        doc.close();
    }

    private static Cell createBarcode(String code, PdfDocument pdfDoc) {
        BarcodeEAN barcode = new BarcodeEAN(pdfDoc);
        barcode.setCodeType(BarcodeEAN.EAN8);
        barcode.setCode(code);

        // Create barcode object to put it to the cell as image
        PdfFormXObject barcodeObject = barcode.createFormXObject(null, null, pdfDoc);
        Cell cell = new Cell().add(new Image(barcodeObject));
        cell.setPaddingTop(10);
        cell.setPaddingRight(10);
        cell.setPaddingBottom(10);
        cell.setPaddingLeft(10);

        return cell;
    }
}


C#

C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Barcodes;
using iText.Kernel.Pdf.Xobject;

namespace iText.Samples.Sandbox.Tables
{
    public class Barcodes
    {
        public static readonly string DEST = "results/sandbox/tables/barcodes.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new Barcodes().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
            Table table = new Table(UnitValue.CreatePercentArray(4)).UseAllAvailableWidth();

            for (int i = 0; i < 12; i++)
            {
                table.AddCell(CreateBarcode(string.Format("{0:d8}", i), pdfDoc));
            }

            doc.Add(table);
            
            doc.Close();
        }

        private static Cell CreateBarcode(string code, PdfDocument pdfDoc)
        {
            BarcodeEAN barcode = new BarcodeEAN(pdfDoc);
            barcode.SetCodeType(BarcodeEAN.EAN8);
            barcode.SetCode(code);

            // Create barcode object to put it to the cell as image
            PdfFormXObject barcodeObject = barcode.CreateFormXObject(null, null, pdfDoc);
            Cell cell = new Cell().Add(new Image(barcodeObject));
            cell.SetPaddingTop(10);
            cell.SetPaddingRight(10);
            cell.SetPaddingBottom(10);
            cell.SetPaddingLeft(10);

            return cell;
        }
    }
}

barcodebackground

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.barcodes;

import com.itextpdf.barcodes.Barcode128;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import java.io.File;

public class BarcodeBackground {
    public static final String DEST = "./target/sandbox/barcodes/barcode_background.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new BarcodeBackground().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

        Barcode128 code128 = new Barcode128(pdfDoc);
        code128.setCode("12345XX789XXX");
        code128.setCodeType(Barcode128.CODE128);
        PdfFormXObject xObject = code128.createFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, pdfDoc);

        float x = 36;
        float y = 750;
        float width = xObject.getWidth();
        float height = xObject.getHeight();

        // Draw the rectangle with the set background color and add the created barcode object.
        PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
        canvas.saveState();
        canvas.setFillColor(ColorConstants.LIGHT_GRAY);
        canvas.rectangle(x, y, width, height);
        canvas.fill();
        canvas.restoreState();
        canvas.addXObjectAt(xObject, 36, 750);

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Barcodes;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;

namespace iText.Samples.Sandbox.Barcodes
{
    public class BarcodeBackground
    {
        public static readonly String DEST = "results/sandbox/barcodes/barcode_background.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new BarcodeBackground().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));

            Barcode128 code128 = new Barcode128(pdfDoc);
            code128.SetCode("12345XX789XXX");
            code128.SetCodeType(Barcode128.CODE128);
            PdfFormXObject xObject = code128.CreateFormXObject(ColorConstants.BLACK,
                ColorConstants.BLACK, pdfDoc);

            float x = 36;
            float y = 750;
            float width = xObject.GetWidth();
            float height = xObject.GetHeight();

            // Draw the rectangle with set background color and add the created barcode object.
            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
            canvas.SaveState();
            canvas.SetFillColor(ColorConstants.LIGHT_GRAY);
            canvas.Rectangle(x, y, width, height);
            canvas.Fill();
            canvas.RestoreState();
            canvas.AddXObjectAt(xObject, 36, 750);

            pdfDoc.Close();
        }
    }
}

barcodeintable

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.barcodes;

import com.itextpdf.barcodes.Barcode128;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

public class BarcodeInTable {
    public static final String DEST = "./target/sandbox/barcodes/barcode_in_table.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new BarcodeInTable().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        String code = "675-FH-A12";

        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        table.addCell("Change baseline:");

        Barcode128 code128 = new Barcode128(pdfDoc);

        // If the value is positive, the text distance under the bars. If zero or negative,
        // the text distance above the bars.
        code128.setBaseline(-1);
        code128.setSize(12);
        code128.setCode(code);
        code128.setCodeType(Barcode128.CODE128);
        Image code128Image = new Image(code128.createFormXObject(pdfDoc));

        // Notice that in iText5 in default PdfPCell constructor (new PdfPCell(Image img))
        // this image does not fit the cell, but it does in addCell().
        // In iText there is no constructor (new Cell(Image img)),
        // so the image adding to the cell can be done only using method add().
        Cell cell = new Cell().add(code128Image);
        table.addCell(cell);
        table.addCell("Add text and bar code separately:");

        code128 = new Barcode128(pdfDoc);

        // Suppress the barcode text
        code128.setFont(null);
        code128.setCode(code);
        code128.setCodeType(Barcode128.CODE128);

        // Let the image resize automatically by setting it to be autoscalable.
        code128Image = new Image(code128.createFormXObject(pdfDoc)).setAutoScale(true);
        cell = new Cell();
        cell.add(new Paragraph("PO #: " + code));
        cell.add(code128Image);
        table.addCell(cell);

        doc.add(table);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Barcodes;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Barcodes
{
    public class BarcodeInTable
    {
        public static readonly String DEST = "results/sandbox/barcodes/barcode_in_table.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new BarcodeInTable().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
            String code = "675-FH-A12";

            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            table.AddCell("Change baseline:");

            Barcode128 code128 = new Barcode128(pdfDoc);

            // If value is positive, the text distance under the bars. If zero or negative,
            // the text distance above the bars.
            code128.SetBaseline(-1);
            code128.SetSize(12);
            code128.SetCode(code);
            code128.SetCodeType(Barcode128.CODE128);
            Image code128Image = new Image(code128.CreateFormXObject(pdfDoc));

            // Notice that in iText5 in default PdfPCell constructor (new PdfPCell(Image img))
            // this image does not fit the cell, but it does in addCell().
            // In iText7 there is no constructor (new Cell(Image img)),
            // so the image adding to the cell can be done only using method add().
            Cell cell = new Cell().Add(code128Image);
            table.AddCell(cell);
            table.AddCell("Add text and bar code separately:");

            code128 = new Barcode128(pdfDoc);
            
            // Suppress the barcode text
            code128.SetFont(null);
            code128.SetCode(code);
            code128.SetCodeType(Barcode128.CODE128);

            // Let the image resize automatically by setting it to be autoscalable.
            code128Image = new Image(code128.CreateFormXObject(pdfDoc)).SetAutoScale(true);
            cell = new Cell();
            cell.Add(new Paragraph("PO #: " + code));
            cell.Add(code128Image);
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }
    }
}


barcodeplacement

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.barcodes;

import com.itextpdf.barcodes.BarcodePDF417;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;

public class BarcodePlacement {
    public static final String DEST = "./target/sandbox/barcodes/barcode_placement.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new BarcodePlacement().manipulatePdf(DEST);
    }

    public Image createBarcode(float xScale, float yScale, PdfDocument pdfDoc) {
        BarcodePDF417 barcode = new BarcodePDF417();
        barcode.setCode("BarcodePDF417 barcode");
        PdfFormXObject barcodeObject = barcode.createFormXObject(ColorConstants.BLACK, pdfDoc);
        Image barcodeImage = new Image(barcodeObject).scale(xScale, yScale);
        return barcodeImage;
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Image img = createBarcode(1, 1, pdfDoc);
        doc.add(new Paragraph(String.format("This barcode measures %s by %s user units",
                img.getImageScaledWidth(), img.getImageScaledHeight())));
        doc.add(img);

        img = createBarcode(3, 3, pdfDoc);
        doc.add(new Paragraph(String.format("This barcode measures %s by %s user units",
                img.getImageScaledWidth(), img.getImageScaledHeight())));
        doc.add(img);

        img = createBarcode(3, 1, pdfDoc);
        doc.add(new Paragraph(String.format("This barcode measures %s by %s user units",
                img.getImageScaledWidth(), img.getImageScaledHeight())));
        doc.add(img);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Barcodes;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Barcodes
{
    public class BarcodePlacement
    {
        public static readonly String DEST = "results/sandbox/barcodes/barcode_placement.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new BarcodePlacement().ManipulatePdf(DEST);
        }

        public Image CreateBarcode(float xScale, float yScale, PdfDocument pdfDoc)
        {
            BarcodePDF417 barcode = new BarcodePDF417();
            barcode.SetCode("BarcodePDF417 barcode");
            PdfFormXObject barcodeObject = barcode.CreateFormXObject(ColorConstants.BLACK, pdfDoc);
            Image barcodeImage = new Image(barcodeObject).Scale(xScale, yScale);
            return barcodeImage;
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Image img = CreateBarcode(1, 1, pdfDoc);
            doc.Add(new Paragraph(String.Format("This barcode measures {0:0.0} by {1:0.0} user units",
                img.GetImageScaledWidth(), img.GetImageScaledHeight())));
            doc.Add(img);

            img = CreateBarcode(3, 3, pdfDoc);
            doc.Add(new Paragraph(String.Format("This barcode measures {0:0.0} by {1:0.0} user units",
                img.GetImageScaledWidth(), img.GetImageScaledHeight())));
            doc.Add(img);

            img = CreateBarcode(3, 1, pdfDoc);
            doc.Add(new Paragraph(String.Format("This barcode measures {0:0.0} by {1:0.0} user units",
                img.GetImageScaledWidth(), img.GetImageScaledHeight())));
            doc.Add(img);

            doc.Close();
        }
    }
}


stampbarcode

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.barcodes;

import com.itextpdf.barcodes.BarcodeEAN;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import java.io.File;

public class StampBarcode {
    public static final String DEST = "./target/sandbox/barcodes/stamp_barcode.pdf";
    public static final String SRC = "./src/main/resources/pdfs/superman.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new StampBarcode().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
            PdfPage pdfPage = pdfDoc.getPage(i);
            Rectangle pageSize = pdfPage.getPageSize();
            float x = pageSize.getLeft() + 10;
            float y = pageSize.getTop() - 50;
            BarcodeEAN barcode = new BarcodeEAN(pdfDoc);
            barcode.setCodeType(BarcodeEAN.EAN8);
            barcode.setCode(createBarcodeNumber(i));

            PdfFormXObject barcodeXObject = barcode.createFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, pdfDoc);
            PdfCanvas over = new PdfCanvas(pdfPage);
            over.addXObjectAt(barcodeXObject, x, y);
        }

        pdfDoc.close();
    }

    private static String createBarcodeNumber(int i) {
        String barcodeNumber = String.valueOf(i);
        barcodeNumber = "00000000".substring(barcodeNumber.length()) + barcodeNumber;

        return barcodeNumber;
    }
}

C#

C#
using System;
using System.IO;
using iText.Barcodes;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;

namespace iText.Samples.Sandbox.Barcodes 
{
    public class StampBarcode 
    {
        public static readonly String DEST = "results/sandbox/barcodes/stamp_barcode.pdf";
        public static readonly String SRC = "../../../resources/pdfs/superman.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new StampBarcode().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            
            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++) 
            {
                PdfPage pdfPage = pdfDoc.GetPage(i);
                Rectangle pageSize = pdfPage.GetPageSize();
                float x = pageSize.GetLeft() + 10;
                float y = pageSize.GetTop() - 50;
                BarcodeEAN barcode = new BarcodeEAN(pdfDoc);
                barcode.SetCodeType(BarcodeEAN.EAN8);
                barcode.SetCode(CreateBarcodeNumber(i));
                
                PdfFormXObject barcodeXObject = barcode.CreateFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, pdfDoc);
                PdfCanvas over = new PdfCanvas(pdfPage);
                over.AddXObjectAt(barcodeXObject, x, y);
            }
            
            pdfDoc.Close();
        }

        private static String CreateBarcodeNumber(int i) 
        {
            String barcodeNumber = i.ToString();
            barcodeNumber = "00000000".Substring(barcodeNumber.Length) + barcodeNumber;
            
            return barcodeNumber;
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.