Stack Exchange
How do I create barcode image containing text at bottom using iText library
I created a pdf using the iText 2.1.7 library. The Pdf (barcodes.pdf) contains a barcode with some text at the bottom. Additionally I saved this barcode as an image (barcode.png), however then the text at bottom is lost.
How do I create the barcode image which also contains the text at bottom?

String RESULT = "c:/BarCodeQRCodeGenerator/barcodes.pdf";
Document document = new Document(new Rectangle(340, 842));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.open();
PdfContentByte cb = writer.getDirectContent();
document.add(new Paragraph("Barcode 128"));
Barcode128 code128 = new Barcode128();
code128.setCode("1234567890");
Image image = code128.createImageWithBarcode(cb, null, null)
document.add(image);

java.awt.Image rawImage = code128.createAwtImage(Color.BLACK, Color.WHITE);
BufferedImage outImage = new BufferedImage(rawImage.getWidth(null), rawImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
outImage.getGraphics().drawImage(rawImage, 0, 0, null);
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ImageIO.write(outImage, "png", bytesOut);
bytesOut.flush();

byte[] pngImageData = bytesOut.toByteArray();
FileOutputStream fos = new FileOutputStream("c:/BarCodeQRCodeGenerator/barcode.png");
fos.write(pngImageData);
fos.close();



In your code you forgot to close the document. The solution is not so easy using the plain iText classes. Thus I used barcode4j (to test the example you need to download it and put it in your classpath):
Code128Bean code128 = new Code128Bean();
code128.setHeight(15f);
code128.setModuleWidth(0.3);
code128.setQuietZone(10);
code128.doQuietZone(true);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, "image/x-png", 300, BufferedImage.TYPE_BYTE_BINARY, false, 0);
code128.generateBarcode(canvas, "1234567890");
canvas.finish();

//write to png file
FileOutputStream fos = new FileOutputStream("barcode.png");
fos.write(baos.toByteArray());
fos.flush();
fos.close();

//write to pdf
Image png = Image.getInstance(baos.toByteArray());
png.setAbsolutePosition(400, 685);
png.scalePercent(25);

Document document = new Document(new Rectangle(595, 842));
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("barcodes.pdf"));
document.open();
document.add(png);
document.close();

writer.close();

