Created
November 6, 2022 07:19
-
-
Save HashRaygoza/2dc78b0acbe95fdd2dd0f77f2eeb1412 to your computer and use it in GitHub Desktop.
Generación de codigos de barras en iText 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.barrasitext; | |
import java.io.File; | |
import com.itextpdf.barcodes.Barcode128; | |
import com.itextpdf.barcodes.Barcode39; | |
import com.itextpdf.barcodes.BarcodeCodabar; | |
import com.itextpdf.barcodes.BarcodeEAN; | |
import com.itextpdf.barcodes.BarcodeInter25; | |
import com.itextpdf.barcodes.BarcodeMSI; | |
import com.itextpdf.barcodes.BarcodePostnet; | |
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.IOException; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author david | |
*/ | |
public class BarrasiText { | |
public static void main(String[] args) { | |
try { | |
File archivo = new File("codigoBarras.pdf"); | |
PdfDocument pdf = new PdfDocument(new PdfWriter(archivo)); | |
try ( Document document = new Document(pdf)) { | |
document.add(new Paragraph("Formato Code39")); | |
Barcode39 barcode39 = new Barcode39(pdf); | |
barcode39.setCode("123456789"); | |
PdfFormXObject form39 = barcode39.createFormXObject(pdf); | |
Image img39 = new Image(form39); | |
document.add(img39); | |
document.add(new Paragraph("Formato Code128")); | |
Barcode128 barcode128 = new Barcode128(pdf); | |
barcode128.setCode("Hola mundo"); | |
PdfFormXObject form128 = barcode128.createFormXObject(pdf); | |
Image img128 = new Image(form128); | |
document.add(img128); | |
document.add(new Paragraph("Formato CodeEAN de 13 digitos")); | |
BarcodeEAN barcodeEAN = new BarcodeEAN(pdf); | |
barcodeEAN.setCode("1111111111111"); | |
PdfFormXObject formEAN = barcodeEAN.createFormXObject(pdf); | |
Image imgEAN = new Image(formEAN); | |
document.add(imgEAN); | |
document.add(new Paragraph("Formato CodeEAN de 8 digitos")); | |
BarcodeEAN barcodeEAN2 = new BarcodeEAN(pdf); | |
barcodeEAN2.setCode("1111111111111"); | |
PdfFormXObject formEAN2 = barcodeEAN2.createFormXObject(pdf); | |
Image imgEAN2 = new Image(formEAN2); | |
document.add(imgEAN2); | |
document.add(new Paragraph("Formato CodeMSI")); | |
BarcodeMSI barcodeMSI = new BarcodeMSI(pdf); | |
barcodeMSI.setCode("2222222222"); | |
PdfFormXObject formMSI = barcodeMSI.createFormXObject(pdf); | |
Image imgMSI = new Image(formMSI); | |
document.add(imgMSI); | |
document.add(new Paragraph("Formato POSTNET")); | |
BarcodePostnet barcodePostnet = new BarcodePostnet(pdf); | |
barcodePostnet.setCode("2222222222"); | |
PdfFormXObject formPostnet = barcodePostnet.createFormXObject(pdf); | |
Image imgPostnet = new Image(formPostnet); | |
document.add(imgPostnet); | |
document.add(new Paragraph("Formato Codabar")); | |
BarcodeCodabar barcodeCodabar = new BarcodeCodabar(pdf); | |
barcodeCodabar.setCode("A22222222A"); | |
PdfFormXObject formCodabar = barcodeCodabar.createFormXObject(pdf); | |
Image imgCodabar = new Image(formCodabar); | |
document.add(imgCodabar); | |
document.add(new Paragraph("Formato Interleaved 2 of 5")); | |
BarcodeInter25 barcodeInter25 = new BarcodeInter25(pdf); | |
barcodeInter25.setCode("99999999"); | |
PdfFormXObject formCodeInter25 = barcodeInter25.createFormXObject(pdf); | |
Image imgCodeInter25 = new Image(formCodeInter25); | |
document.add(imgCodeInter25); | |
} | |
} catch (IOException ex) { | |
Logger.getLogger(BarrasiText.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment