Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set iText barcode width?

I need to set width of barcode generated by iText. I am using this code:

Barcode128 code128 = new Barcode128();
code128.setCode("P662130002");
code128.setBarHeight(80f); // great! but what about width???

java.awt.Image awtImage = code128.createAwtImage(Color.WHITE, Color.BLACK);

Is there any way how to set the width without scaling resulting image?

like image 672
Firzen Avatar asked Apr 19 '13 09:04

Firzen


1 Answers

use this to set width for BarCode

code128.setX(10.0f) ;//  Sets the minimum bar width.

Method signature

public void setX(float x)

Sets the minimum bar width.

Parameters:
    x - the minimum bar width

EDIT

public class BarCodeTest {

    public static void main(String[] a) throws FileNotFoundException, DocumentException {
        Barcode128 code128 = new Barcode128();
        code128.setCode("P662130002");
        code128.setBarHeight(80f); // great! but what about width???
        code128.setX(1f);
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("hello.pdf"));
        document.open();

        PdfContentByte cb = writer.getDirectContent();

        Image barcodeimage = code128.createImageWithBarcode(cb, Color.BLACK, Color.WHITE);
        document.add(barcodeimage);

        document.close();

    }

}

Use minimum value for setX(1f); can see the difference.

like image 140
NPKR Avatar answered Nov 10 '22 18:11

NPKR