Java Tutorial/PDF/Barcode

Материал из Java эксперт
Перейти к: навигация, поиск

Barcode 3 of 9 extended

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode39;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    document.add(new Paragraph("Barcode 3 of 9 extended"));
    Barcode39 code39ext = new Barcode39();
    code39ext.setCode("iText in Action");
    code39ext.setStartStopText(false);
    code39ext.setExtended(true);
    document.add(code39ext.createImageWithBarcode(cb, null, null));

    
    
    document.add(p);
    document.close();
  }
}





BarcodeCodabar

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BarcodeCodabar;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    // CODABAR
    document.add(new Paragraph("Codabar"));
    BarcodeCodabar codabar = new BarcodeCodabar();
    codabar.setCode("A123A");
    codabar.setStartStopText(true);
    document.add(codabar.createImageWithBarcode(cb, null, null));

    
    
    document.add(p);
    document.close();
  }
}





Barcode.CODE128_RAW

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.Barcode128;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    // CODE 128
    document.add(new Paragraph("Barcode 128"));
    Barcode128 code128 = new Barcode128();
    code128.setCode("0123456789\uffffMy Raw Barcode (0 - 9)");
    code128.setCodeType(Barcode.CODE128_RAW);
    document.add(code128.createImageWithBarcode(cb, null, null));

    
    
    document.add(p);
    document.close();
  }
}





Barcode.CODE128_UCC

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.Barcode128;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    // it is composed of 3 blocks whith AI 01, 3101 and 10
    Barcode128 uccEan128 = new Barcode128();
    uccEan128.setCodeType(Barcode.CODE128_UCC);
    uccEan128.setCode("(01)00000090311314(10)ABC123(15)060916");
    document.add(uccEan128.createImageWithBarcode(cb, Color.blue, Color.black));
    document.add(p);
    document.close();
  }
}





Barcode.EAN8

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    // EAN 8
    document.add(new Paragraph("Barcode EAN.UCC-8"));
    codeEAN.setCodeType(Barcode.EAN8);
    codeEAN.setBarHeight(codeEAN.getSize() * 1.5f);
    codeEAN.setCode("34569870");
    document.add(codeEAN.createImageWithBarcode(cb, null, null));
    
    
    document.add(p);
    document.close();
  }
}





Barcode Interleaved 2 of 5

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.BarcodeInter25;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    // INTER25
    document.add(new Paragraph("Barcode Interleaved 2 of 5"));
    BarcodeInter25 code25 = new BarcodeInter25();
    code25.setGenerateChecksum(true);
    code25.setCode("41-1200076041-001");
    document.add(code25.createImageWithBarcode(cb, null, null));
    document.add(p);
    document.close();
  }
}





BarcodePDF417

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.BarcodePDF417;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    // PDF417
    document.add(new Paragraph("Barcode PDF417"));
    BarcodePDF417 pdf417 = new BarcodePDF417();
    String text = "this is a test";
    pdf417.setText(text);
    Image img = pdf417.getImage();
    img.scalePercent(50, 50 * pdf417.getYHeight());
    document.add(img);

    
    
    document.add(p);
    document.close();
  }
}





Barcode.PLANET

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.BarcodePostnet;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    document.add(new Paragraph("Barcode Planet"));
    BarcodePostnet codePlanet = new BarcodePostnet();
    codePlanet.setCode("01234567890");
    codePlanet.setCodeType(Barcode.PLANET);
    document.add(codePlanet.createImageWithBarcode(cb, null, null));
    document.add(p);
    document.close();
  }
}





Barcode Postnet

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.BarcodePostnet;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    // POSTNET
    document.add(new Paragraph("Barcode Postnet"));
    BarcodePostnet codePost = new BarcodePostnet();
    document.add(new Paragraph("ZIP"));
    codePost.setCode("01234");
    document.add(codePost.createImageWithBarcode(cb, null, null));
    
    document.add(p);
    document.close();
  }
}





Barcode.SUPP5

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.BarcodeEANSUPP;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    // EANSUPP
    document.add(new Paragraph("ISBN 0-111-11111-1"));
    codeEAN.setCodeType(Barcode.EAN13);
    codeEAN.setCode("1111111111111");
    BarcodeEAN codeSUPP = new BarcodeEAN();
    codeSUPP.setCodeType(Barcode.SUPP5);
    codeSUPP.setCode("55499");
    codeSUPP.setBaseline(-2);
    BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP);
    document.add(eanSupp.createImageWithBarcode(cb, null, Color.blue));
    
    
    document.add(p);
    document.close();
  }
}





Barcode.UPCA

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    // UPC A
    document.add(new Paragraph("Barcode UCC-12 (UPC-A)"));
    codeEAN.setCodeType(Barcode.UPCA);
    codeEAN.setCode("785342304749");
    document.add(codeEAN.createImageWithBarcode(cb, null, null));    
    
    
    document.add(p);
    document.close();
  }
}





Barcode.UPCE

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    // UPC E
    document.add(new Paragraph("Barcode UPC-E"));
    codeEAN.setCodeType(Barcode.UPCE);
    codeEAN.setCode("03456781");
    document.add(codeEAN.createImageWithBarcode(cb, null, null));
    codeEAN.setBarHeight(codeEAN.getSize() * 3f);
    
    
    document.add(p);
    document.close();
  }
}





Change properties of Barcode128

import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode128;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    // Data for the barcode :
    Barcode128 shipBarCode = new Barcode128();
    shipBarCode.setX(0.75f);
    shipBarCode.setN(1.5f);
    shipBarCode.setSize(10f);
    shipBarCode.setTextAlignment(Element.ALIGN_CENTER);
    shipBarCode.setBaseline(10f);
    shipBarCode.setBarHeight(50f);
    shipBarCode.setCode("111111111111");
    document.add(shipBarCode
        .createImageWithBarcode(cb, Color.black, Color.blue));
    
    document.add(p);
    document.close();
  }
}





Create Barcode 128

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.Barcode128;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    // CODE 128
    document.add(new Paragraph("Barcode 128"));
    Barcode128 code128 = new Barcode128();
    code128.setCode("0123456789 hello");
    document.add(code128.createImageWithBarcode(cb, null, null));
    document.add(p);
    document.close();
  }
}





Use Barcode39

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.Barcode39;
import com.lowagie.text.pdf.BarcodeEAN;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(
        "2.pdf"));
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    BarcodeEAN codeEAN = new BarcodeEAN();
    codeEAN.setCode("4512345678906");
    Paragraph p = new Paragraph();
    
    
    // CODE 39
    document.add(new Paragraph("Barcode 3 of 9"));
    Barcode39 code39 = new Barcode39();
    code39.setCode("ITEXT IN ACTION");
    document.add(code39.createImageWithBarcode(cb, null, null));

    
    document.add(p);
    document.close();
  }
}