Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to add some text with a bar code in a table cell using itext as per the following code but it does not show in the pdf file. i tried adding chunks and paragraph. Any help on this would be appreciated.

Barcode128 barcode = new Barcode128();
//barcode.setCodeType(Barcode.EAN8);
barcode.setCode(code);
PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true);

Paragraph paragraph = new Paragraph("Hello World"); 
cell.addElement(paragraph);

cell.setPadding(10);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
532 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are probably confused by text vs. composite mode.

When using the PdfPCell(Image) constructor, you create a cell in text mode. Any subsequent call to addElement(Element) will then switch the cell to composite mode, removing all content previously entered in the constructor.

You'll have to change your code that way:

PdfPCell cell = new PdfPCell();

Barcode128 barcode = new Barcode128();
barcode.setCode(code);
Image barcodeImage = barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY);
cell.addElement(barcodeImage);

Paragraph paragraph = new Paragraph("Hello World"); 
cell.addElement(paragraph);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...