Bar codes

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

1st November 2015
admin-marketing

Switch code for this example

Barcodes.java
  1. package sandbox.tables;
  2.  
  3. import com.itextpdf.text.BaseColor;
  4. import com.itextpdf.text.Document;
  5. import com.itextpdf.text.DocumentException;
  6. import com.itextpdf.text.pdf.Barcode;
  7. import com.itextpdf.text.pdf.BarcodeEAN;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15.  
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class Barcodes {
  20.     public static final String DEST = "results/tables/barcode_table.pdf";
  21.  
  22.     public static void main(String[] args) throws IOException,
  23.             DocumentException {
  24.         File file = new File(DEST);
  25.         file.getParentFile().mkdirs();
  26.         new Barcodes().createPdf(DEST);
  27.     }
  28.    
  29.     public void createPdf(String dest) throws IOException, DocumentException {
  30.         Document document = new Document();
  31.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.         PdfPTable table = new PdfPTable(4);
  34.         table.setWidthPercentage(100);
  35.         for (int i = 0; i < 12; i++) {
  36.             table.addCell(createBarcode(writer, String.format("%08d", i)));
  37.         }
  38.         document.add(table);
  39.         document.close();
  40.     }
  41.    
  42.     public static PdfPCell createBarcode(PdfWriter writer, String code) throws DocumentException, IOException {
  43.         BarcodeEAN barcode = new BarcodeEAN();
  44.         barcode.setCodeType(Barcode.EAN8);
  45.         barcode.setCode(code);
  46.         PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true);
  47.         cell.setPadding(10);
  48.         return cell;
  49.     }
  50. }
BarcodePlacement.java
  1. /**
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/30186774/2d-barcode-generation-issue-in-java
  4.  */
  5. package sandbox.barcodes;
  6.  
  7. import com.itextpdf.text.BadElementException;
  8. import com.itextpdf.text.BaseColor;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.Rectangle;
  14. import com.itextpdf.text.pdf.BarcodePDF417;
  15. import com.itextpdf.text.pdf.PdfContentByte;
  16. import com.itextpdf.text.pdf.PdfTemplate;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class BarcodePlacement {
  26.     public static final String DEST = "results/barcodes/barcode_placement.pdf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new BarcodePlacement().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         PdfContentByte cb = writer.getDirectContent();
  39.         Image img = createBarcode(cb, "This is a 2D barcode", 1, 1);
  40.         document.add(new Paragraph(String.format("This barcode measures %s by %s user units", img.getScaledWidth(), img.getScaledHeight())));
  41.         document.add(img);
  42.         img = createBarcode(cb, "This is NOT a raster image", 3, 3);
  43.         document.add(new Paragraph(String.format("This barcode measures %s by %s user units", img.getScaledWidth(), img.getScaledHeight())));
  44.         document.add(img);
  45.         img = createBarcode(cb, "This is vector data drawn on a PDF page", 1, 3);
  46.         document.add(new Paragraph(String.format("This barcode measures %s by %s user units", img.getScaledWidth(), img.getScaledHeight())));
  47.         document.add(img);
  48.         document.close();
  49.     }
  50.    
  51.     public Image createBarcode(PdfContentByte cb, String text, float mh, float mw) throws BadElementException {
  52.         BarcodePDF417 pf = new BarcodePDF417();
  53.         pf.setText("BarcodePDF417 barcode");
  54.         Rectangle size = pf.getBarcodeSize();
  55.         PdfTemplate template = cb.createTemplate(mw * size.getWidth(), mh * size.getHeight());
  56.         pf.placeBarcode(template, BaseColor.BLACK, mh, mw);
  57.         return Image.getInstance(template);
  58.     }
  59. }
StampBarcode.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/26325712/itext-add-image-to-existing-document-using-itext-pdfstamper
  4.  * Copyright 2014, iText Group NV
  5.  */
  6. package sandbox.stamper;
  7.  
  8. import com.itextpdf.text.BaseColor;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.Barcode;
  12. import com.itextpdf.text.pdf.BarcodeEAN;
  13. import com.itextpdf.text.pdf.PdfContentByte;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16. import com.itextpdf.text.pdf.PdfTemplate;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. /**
  24.  * Example that shows how to watermark a less simple PDF.
  25.  */
  26. @WrapToTest
  27. public class StampBarcode {
  28.     /** The original PDF file. */
  29.     public static final String SRC
  30.         = "resources/pdfs/superman.pdf";
  31.     /** The resulting PDF file. */
  32.     public static final String DEST
  33.         = "results/stamper/add_barcode.pdf";
  34.  
  35.     /**
  36.      * Fills out an interactive form.
  37.      * @param args  no arguments needed
  38.      * @throws DocumentException
  39.      * @throws IOException
  40.      */
  41.     public static void main(String[] args)
  42.             throws DocumentException, IOException {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new StampBarcode().manipulatePdf(SRC, DEST);
  46.     }
  47.    
  48.     /**
  49.      * Fills out and flattens a form with the name, company and country.
  50.      * @param src   the path to the original form
  51.      * @param dest  the path to the filled out form
  52.      * @throws DocumentException
  53.      * @throws IOException
  54.      */
  55.     public void manipulatePdf(String src, String dest)
  56.             throws DocumentException, IOException {
  57.         PdfReader reader = new PdfReader(src);
  58.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  59.         int n = reader.getNumberOfPages();
  60.         Rectangle pagesize;
  61.         for (int i = 1; i
BarcodeInTable.java
  1. /*
  2.  * This code sample was written by Bruno Lowagie in answer to this question:
  3.  * http://stackoverflow.com/questions/35011232
  4.  */
  5. package sandbox.barcodes;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Image;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.pdf.Barcode128;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class BarcodeInTable {
  23.     public static final String DEST = "results/barcodes/barcode_in_table.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new BarcodeInTable().createPdf(DEST);
  29.     }
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.        
  35.         String code = "675-FH-A12";
  36.        
  37.         PdfContentByte cb = writer.getDirectContent();
  38.        
  39.         PdfPTable table = new PdfPTable(2);
  40.        
  41.         table.addCell("Change baseline:");
  42.         Barcode128 code128 = new Barcode128();
  43.         code128.setBaseline(-1);
  44.         code128.setSize(12);
  45.         code128.setCode(code);
  46.         code128.setCodeType(Barcode128.CODE128);
  47.         Image code128Image = code128.createImageWithBarcode(cb, null, null);
  48.         PdfPCell cell = new PdfPCell(code128Image);
  49.         table.addCell(cell);
  50.        
  51.         table.addCell("Add text and bar code separately:");
  52.         code128 = new Barcode128();
  53.         code128.setFont(null);
  54.         code128.setCode(code);
  55.         code128.setCodeType(Barcode128.CODE128);
  56.         code128Image = code128.createImageWithBarcode(cb, null, null);
  57.         cell = new PdfPCell();
  58.         cell.addElement(new Phrase("PO #: " + code));
  59.         cell.addElement(code128Image);
  60.         table.addCell(cell);
  61.        
  62.         document.add(table);
  63.        
  64.         document.close();
  65.     }
  66.    
  67. }
BarcodeBackground.java
  1. /*
  2.  * Example written in answer to:
  3.  * http://stackoverflow.com/questions/35454039
  4.  */
  5. package sandbox.barcodes;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.Barcode128;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfTemplate;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class BarcodeBackground {
  22.     public static final String DEST = "results/barcodes/barcode_background.pdf";
  23.    
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new BarcodeBackground().createPdf(DEST);
  28.     }
  29.     public void createPdf(String dest) throws IOException, DocumentException {
  30.         Document document = new Document();
  31.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.         PdfContentByte canvas = writer.getDirectContent();
  34.         Barcode128 code128 = new Barcode128();
  35.         code128.setCode("12345XX789XXX");
  36.         code128.setCodeType(Barcode128.CODE128);
  37.         PdfTemplate template = code128.createTemplateWithBarcode(
  38.                 canvas, BaseColor.BLACK, BaseColor.BLACK);
  39.         float x = 36;
  40.         float y = 750;
  41.         float w = template.getWidth();
  42.         float h = template.getHeight();
  43.         canvas.saveState();
  44.         canvas.setColorFill(BaseColor.LIGHT_GRAY);
  45.         canvas.rectangle(x, y, w, h);
  46.         canvas.fill();
  47.         canvas.restoreState();
  48.         canvas.addTemplate(template, 36, 750);
  49.         document.close();
  50.     }
  51. }
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