热敏式 打印机 小票排版

 private int width = 32;//纸张宽度
    private int defaultRightSpacing = 16; //数量开始位置最后的宽度
    private int rightSpacing = defaultRightSpacing; //数量据左边的距离
    private int leftSpacing = width - defaultRightSpacing; //数量据右边的距离
    private int defaultLeftSpacing = width - defaultRightSpacing;
    private String getGoodDetails(List detailsBeen) throws UnsupportedEncodingException {

        StringBuffer sbf = new StringBuffer("");
        for (int i = 0; i < detailsBeen.size(); i++) {
            OrderBean.DetailsBean d = detailsBeen.get(i);
            String name = d.getProductName();
            if(name.length() > 8){
                name = name.substring(0, 8);
            }
            String number = "x" + d.getNumber();
            String price = "" + d.getPayAmount();
            leftSpacing = defaultLeftSpacing - name.getBytes("gbk").length;
            rightSpacing = defaultRightSpacing - price.getBytes("gbk").length - number.getBytes("gbk").length;
            sbf.append(name);
            for (int j = 0; j < leftSpacing; j++) {//添加距左边的位置
                sbf.append(" ");
            }
            sbf.append(number);
//            oneLeftSpacing = sbf.toString().indexOf("x");
            for (int j = 0; j < rightSpacing; j++) {//添加距左边的位置
                sbf.append(" ");
            }
            sbf.append(price);
            sbf.append("\n");
        }
        return sbf.toString();
    }

你可能感兴趣的:(热敏打印机,小票排版)