java 如何设置 iText 条码宽度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16101084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:54:49  来源:igfitidea点击:

How to set iText barcode width?

javaitextbarcode

提问by Firzen

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

我需要设置 iText 生成的条形码的宽度。我正在使用此代码:

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?

有什么方法可以在不缩放结果图像的情况下设置宽度?

回答by NPKR

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.

使用最小值setX(1f);可以看出差异。