Bar codes

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

Barcodes.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.tables;
  9.  
  10. import com.itextpdf.barcodes.BarcodeEAN;
  11. import com.itextpdf.kernel.pdf.PdfDocument;
  12. import com.itextpdf.kernel.pdf.PdfWriter;
  13. import com.itextpdf.layout.Document;
  14. import com.itextpdf.layout.element.Cell;
  15. import com.itextpdf.layout.element.Image;
  16. import com.itextpdf.layout.element.Table;
  17. import com.itextpdf.samples.GenericTest;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19.  
  20. import org.junit.experimental.categories.Category;
  21.  
  22. import java.io.File;
  23.  
  24. @Category(SampleTest.class)
  25. public class Barcodes extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/tables/barcodes.pdf";
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new Barcodes().manipulatePdf(DEST);
  32.     }
  33.  
  34.     @Override
  35.     protected void manipulatePdf(String dest) throws Exception {
  36.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  37.         Document doc = new Document(pdfDoc);
  38.  
  39.         Table table = new Table(4);
  40.         for (int i = 0; i < 12; i++) {
  41.             table.addCell(createBarcode(String.format("%08d", i), pdfDoc));
  42.         }
  43.         doc.add(table);
  44.  
  45.         doc.close();
  46.     }
  47.  
  48.     public static Cell createBarcode(String code, PdfDocument pdfDoc) {
  49.         BarcodeEAN barcode = new BarcodeEAN(pdfDoc);
  50.         barcode.setCodeType(BarcodeEAN.EAN8);
  51.         barcode.setCode(code);
  52.         Cell cell = new Cell().add(new Image(barcode.createFormXObject(null, null, pdfDoc)));
  53.         cell.setPaddingTop(10);
  54.         cell.setPaddingRight(10);
  55.         cell.setPaddingBottom(10);
  56.         cell.setPaddingLeft(10);
  57.         return cell;
  58.     }
  59. }
BarcodeBackground.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This code sample was written by Bruno Lowagie in answer to this question:
  10.  * http://stackoverflow.com/questions/35454039
  11.  */
  12. package com.itextpdf.samples.sandbox.barcodes;
  13.  
  14. import com.itextpdf.barcodes.Barcode128;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  19. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import java.io.File;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. @Category(SampleTest.class)
  28. public class BarcodeBackground extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/barcodes/barcode_background.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new BarcodeBackground().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  40.         PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
  41.         Barcode128 code128 = new Barcode128(pdfDoc);
  42.         code128.setCode("12345XX789XXX");
  43.         code128.setCodeType(Barcode128.CODE128);
  44.         PdfFormXObject xObject = code128.createFormXObject(Color.BLACK, Color.BLACK, pdfDoc);
  45.         float x = 36;
  46.         float y = 750;
  47.         float w = xObject.getWidth();
  48.         float h = xObject.getHeight();
  49.         canvas.saveState();
  50.         canvas.setFillColor(Color.LIGHT_GRAY);
  51.         canvas.rectangle(x, y, w, h);
  52.         canvas.fill();
  53.         canvas.restoreState();
  54.         canvas.addXObject(xObject, 36, 750);
  55.         pdfDoc.close();
  56.     }
  57. }
BarcodeInTable.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This code sample was written by Bruno Lowagie in answer to this question:
  10.  * http://stackoverflow.com/questions/35011232
  11.  */
  12. package com.itextpdf.samples.sandbox.barcodes;
  13.  
  14. import com.itextpdf.barcodes.Barcode128;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.Cell;
  19. import com.itextpdf.layout.element.Image;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class BarcodeInTable extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/barcodes/barcode_in_table.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new BarcodeInTable().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  41.         Document doc = new Document(pdfDoc);
  42.  
  43.         String code = "675-FH-A12";
  44.  
  45.         Table table = new Table(2);
  46.  
  47.         table.addCell("Change baseline:");
  48.         Barcode128 code128 = new Barcode128(pdfDoc);
  49.         code128.setBaseline(-1);
  50.         code128.setSize(12);
  51.         code128.setCode(code);
  52.         code128.setCodeType(Barcode128.CODE128);
  53.         // Notice that in itext5 in default PdfPCell constructor (new PdfPCell(Image img))
  54.         // the image does not fit the cell, but it does in addCell
  55.         Image code128Image = new Image(code128.createFormXObject(pdfDoc));//.setAutoScale(true);
  56.         Cell cell = new Cell().add(code128Image);
  57.         table.addCell(cell);
  58.  
  59.         table.addCell("Add text and bar code separately:");
  60.         code128 = new Barcode128(pdfDoc);
  61.         code128.setFont(null);
  62.         code128.setCode(code);
  63.         code128.setCodeType(Barcode128.CODE128);
  64.         code128Image = new Image(code128.createFormXObject(pdfDoc)).setAutoScale(true);
  65.         cell = new Cell();
  66.         cell.add("PO #: " + code);
  67.         cell.add(code128Image);
  68.         table.addCell(cell);
  69.  
  70.         doc.add(table);
  71.  
  72.         doc.close();
  73.     }
  74. }
BarcodePlacement.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.barcodes;
  9.  
  10. import com.itextpdf.barcodes.BarcodePDF417;
  11. import com.itextpdf.kernel.color.Color;
  12. import com.itextpdf.kernel.pdf.PdfDocument;
  13. import com.itextpdf.kernel.pdf.PdfWriter;
  14. import com.itextpdf.layout.Document;
  15. import com.itextpdf.layout.element.Image;
  16. import com.itextpdf.layout.element.Paragraph;
  17. import com.itextpdf.samples.GenericTest;
  18. import com.itextpdf.test.annotations.type.SampleTest;
  19.  
  20. import org.junit.experimental.categories.Category;
  21.  
  22. import java.io.File;
  23.  
  24. @Category(SampleTest.class)
  25. public class BarcodePlacement extends GenericTest {
  26.     public static final String DEST = "./target/test/resources/sandbox/barcodes/barcode_placement.pdf";
  27.  
  28.     public static void main(String[] args) throws Exception {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new BarcodePlacement().manipulatePdf(DEST);
  32.     }
  33.  
  34.     // We've changed the order of arguments (in comparison with itext5 example) to make it more clear
  35.     public Image createBarcode(float mw, float mh, PdfDocument pdfDoc) {
  36.         BarcodePDF417 barcode = new BarcodePDF417();
  37.         barcode.setCode("BarcodePDF417 barcode");
  38.         return new Image(barcode.createFormXObject(Color.BLACK, pdfDoc)).scale(mw, mh);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  44.         Document doc = new Document(pdfDoc);
  45.  
  46.         Image img = createBarcode(1, 1, pdfDoc);
  47.         doc.add(new Paragraph(String.format("This barcode measures %s by %s user units",
  48.                 img.getImageScaledWidth(), img.getImageScaledHeight())));
  49.         doc.add(img);
  50.         img = createBarcode(3, 3, pdfDoc);
  51.         doc.add(new Paragraph(String.format("This barcode measures %s by %s user units",
  52.                 img.getImageScaledWidth(), img.getImageScaledHeight())));
  53.         doc.add(img);
  54.         img = createBarcode(3, 1, pdfDoc);
  55.         doc.add(new Paragraph(String.format("This barcode measures %s by %s user units",
  56.                 img.getImageScaledWidth(), img.getImageScaledHeight())));
  57.         doc.add(img);
  58.  
  59.         doc.close();
  60.     }
  61. }
StampBarcode.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. package com.itextpdf.samples.sandbox.stamper;
  9.  
  10. import com.itextpdf.barcodes.BarcodeEAN;
  11. import com.itextpdf.kernel.color.Color;
  12. import com.itextpdf.kernel.geom.Rectangle;
  13. import com.itextpdf.kernel.pdf.PdfDocument;
  14. import com.itextpdf.kernel.pdf.PdfReader;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  17. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24.  
  25. /**
  26.  * Example written by Bruno Lowagie in answer to:
  27.  * http://stackoverflow.com/questions/26325712/itext-add-image-to-existing-document-using-itext-pdfstamper
  28.  */
  29. @Category(SampleTest.class)
  30. public class StampBarcode extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/stamper/stamp_barcode.pdf";
  32.     public static final String SRC = "./src/test/resources/pdfs/superman.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new StampBarcode().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  43.         int n = pdfDoc.getNumberOfPages();
  44.         Rectangle pagesize;
  45.         for (int i = 1; i
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now