⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 barpage.java

📁 Java开发的通用条码打印程序
💻 JAVA
字号:
package barprint.print;

import java.awt.print.PrinterJob;
import java.awt.Font;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.Color;
import java.awt.print.PrinterJob;
import java.awt.print.PrinterException;
import barprint.print.PrintAll;
import barprint.print.PrintLabel;
import java.awt.print.PageFormat;
import barprint.print.PrintObject;
import barprint.print.PagePrint;
import barprint.print.Ptest;
import java.awt.print.Book;
import java.awt.print.Paper;
import barprint.print.PrintLine;
import barprint.print.PrintImage;
import barprint.print.PrintRectangle;
import barprint.Barcodesoft;


/**
 * <p>Title: </p>
 *
 * <p>Description: 条码打印程序</p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class Barpage {
    public Barpage() {
    }

    /**
     * 打印图象生成方法
     * @param strdesttext String
     * @param strpiece String
     * @param strBill String
     * @param bl_YN boolean
     */
    public static void Barpage(String strdesttext,String strpiece,String strBill,boolean bl_YN){
        String strDest,strDestination;
        strDest = getdest(strdesttext);
        strDestination = getDestination(strdesttext);
        Barpage(strDest,strDestination,strpiece,strBill,bl_YN);
    }

    /**
     *
     * @param strDest String          //目的地三字码
     * @param strDestination String   //目的地详细信息
     * @param strBill String          //单号
     * @param strpiece String         //件数
     * @param bl_YN boolean           //是否显示打印对话框
     */
    public  static void Barpage(
            String strDest,
            String strDestination,
            String strBill,
            String strpiece,
            boolean bl_YN ){
        PrintAll PA = new PrintAll();
        String strbilltobarcode;
        strbilltobarcode = Barcodesoft.Code128A(strBill);

        strpiece = ""; //件数不打印

        //黑色的背景
        PrintLabel PLine0 = new PrintLabel(
                "▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄",
                0,
                58);
        PLine0.setFont("Arial", Font.BOLD, 5);
        PA.putPrintO(PLine0);
        PrintLabel PLine1 = new PrintLabel(
                "▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄",
                0,
                62);
        PLine1.setFont("Arial", Font.BOLD, 5);
        PA.putPrintO(PLine1);

        //目的地
        int nStart_text = 0;
        String Pdest_text = strDestination;
        //通过字的长度控制开始位置
        if (Pdest_text.length() == 12) {
            nStart_text = 40;
        }
        if (Pdest_text.length() == 8) {
            nStart_text = 70;
        }
        if (Pdest_text.length() == 15) {
            nStart_text = 10;
        }
        if (Pdest_text.length() == 16) {
            nStart_text = 10;
        }
        if (Pdest_text.length() == 17) {
            nStart_text = 32;
        }
        PrintLabel Pdest = new PrintLabel(Pdest_text, nStart_text, 50);
        Pdest.setColor(Color.BLACK);
        Pdest.setFont("楷体_GB2312", Font.BOLD, 32);
        PA.putPrintO(Pdest);

        //条码
        PrintLabel Pbill = new PrintLabel(strbilltobarcode, 40, 140);
        Pbill.setFont("Code128", Font.PLAIN, 16);
        PA.putPrintO(Pbill);

        //线条
        PrintLabel PLine2 = new PrintLabel(
                "▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄",
                0, 160);
        PLine2.setFont("Arial", Font.BOLD, 5);
        PA.putPrintO(PLine2);

        //Air Waybill No.
        PrintLabel Pair = new PrintLabel("Air Waybill No.", 20, 175);
        Pair.setFont("楷体_GB2312", Font.PLAIN, 10);
        PA.putPrintO(Pair);

        //号码
        PrintLabel Pno = new PrintLabel(getWayBill(strBill), 70, 200);
        Pno.setFont("Arial", Font.BOLD, 20);
        PA.putPrintO(Pno);

        //线条
        PrintLabel PLine3 = new PrintLabel(
                "▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄",
                0, 205);
        PLine3.setFont("Arial", Font.BOLD, 5);
        PA.putPrintO(PLine3);

        //destination
        PrintLabel PDestination = new PrintLabel("Destination", 20, 220);
        PDestination.setFont("楷体_GB2312", Font.PLAIN, 5);
        PA.putPrintO(PDestination);

        PrintLabel PDestination_value = new PrintLabel(strDest, 20, 240);
        PDestination_value.setFont("宋体", Font.BOLD, 20);
        PA.putPrintO(PDestination_value);

        //Total No.of Pieces
        PrintLabel PPieces = new PrintLabel("Total No.of Pieces", 150,
                                            220);
        PPieces.setFont("楷体_GB2312", Font.PLAIN, 10);
        PA.putPrintO(PPieces);

        PrintLabel PPieces_value = new PrintLabel(strpiece,
                                                  150, 240);
        PPieces_value.setFont("Arial", Font.BOLD, 20);
        PA.putPrintO(PPieces_value);

        //线条
        PrintLine PLine4 = new PrintLine(140, 210, 140, 250);
        PLine4.setWidth(1);
        PA.putPrintO(PLine4);
//        PrintLine PLine5 = new PrintLine(0, 250, 295, 250);
//        PLine5.setWidth(1);
//        PA.putPrintO(PLine5);
        PrintLabel PLine5 = new PrintLabel(
                "▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄",
                0, 250);
        PLine5.setFont("Arial", Font.BOLD, 5);
        PA.putPrintO(PLine5);

        //其他
        PrintLabel P1 = new PrintLabel("House No.", 10, 300);
        P1.setFont("Arial", Font.BOLD, 10);
        PA.putPrintO(P1);
        PrintLabel P2 = new PrintLabel("House Total Pcs.", 10, 315);
        P2.setFont("Arial", Font.BOLD, 10);
        PA.putPrintO(P2);
        PrintLabel P3 = new PrintLabel("House Destination", 10, 330);
        P3.setFont("Arial", Font.BOLD, 10);
        PA.putPrintO(P3);
        PrintLabel P4 = new PrintLabel("House Departure", 10, 345);
        P4.setFont("Arial", Font.BOLD, 10);
        PA.putPrintO(P4);

        //    通俗理解就是书、文档
        Book book = new Book();
        //    设置成竖打
        PageFormat pf = new PageFormat();
        pf.setOrientation(PageFormat.PORTRAIT);
        //    通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
        Paper p = new Paper();
        p.setSize(400, 500); //纸张大小
        p.setImageableArea(10, 10, 295, 420); //A4(595 X 842)设置打印区域,其实0,0应该是72,72,因为A4纸的默认X,Y边距是72
        pf.setPaper(p);
        //    把 PageFormat 和 Printable 添加到书中,组成一个页面
        book.append(PA, pf);

        //获取打印服务对象
        PrinterJob job = PrinterJob.getPrinterJob();
        // 设置打印类
        job.setPageable(book);

        try {
            //可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印
            //判断是否显示打印对话框
            if (bl_YN) {
                boolean a = job.printDialog();
                if (a) {
                    job.print();
                }
            } else {
                job.print();
            }
        } catch (PrinterException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @param strwabybill String
     * @return String
     */
    public static String getWayBill(String strwabybill) {
        String strBill = strwabybill.trim();
        if (strBill.length() >= 11) {
            String str_bill = strBill.substring(0, 11);
            str_bill = str_bill.substring(0, 3) + " - " +
                       str_bill.substring(3, 7) +
                       " " + str_bill.substring(7, 11);
            return str_bill;
        }
        return "";

    }

    /**
     *
     * @param strtext String
     * @return String
     */
    public static String getdest(String strtext){
        strtext = strtext.trim();
        return strtext.substring(0, strtext.indexOf("          ")).trim();
    }

    /**
     *
     * @param strtext String
     * @return String
     */
    public static String getDestination(String strtext){
        strtext = strtext.trim();
        return strtext.substring(strtext.indexOf("          ")).
                                   trim();
    }


    /**
     * 通过件数生成运单5位数的后缀
     * @param inum int
     * @return String
     */
    public static String gethouzhui(int inum){
        String strHouzhui = "";
        String strnum = String.valueOf(inum);
        int ilen = strnum.length();
        if (inum <=0 || inum >= 99999) {
            return  null;
        }
        for (int i = 0;i< 5 - ilen;i++){
            strHouzhui = strHouzhui + "0";
        }
        strHouzhui = strHouzhui + strnum;
        return strHouzhui;
    }

    /**
     * 通过输入的运单号码取到操作员希望的打印的件数
     * @param strwaybybill String
     * @return int
     */
    public static int getPieces(String strwaybybill){
        int iPieces=0;
        String strtemp;
        //运单号码不足5位数据
        if (strwaybybill.length() <=5){
            iPieces = 0;
        }
        strtemp = strwaybybill.trim().substring(strwaybybill.length() - 5);
        try{
            iPieces = Integer.parseInt(strtemp);
        }catch (Exception e)
        {
            iPieces = 0;
        }

        return iPieces;

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -