使用JAVA在点阵打印机上使用连续纸打印而不中断分页

时间:2014-08-20 14:56:32

标签: java printing

这是我的代码,用于获取用户的帐号并将其设置为要打印的纸张。

import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JOptionPane;



/**
 *
 * @author Govinda
 */
public class ChequePrint{

    public static void main(String[] args) {

        // variable defnations

        String accNum;
        Long cheqNum;
        String solName;
        int qtyCheq;
        String holderName;

        //getting account number from user
        accNum = JOptionPane.showInputDialog(null, "Enter Account Number:", "Cheque Print",
        JOptionPane.QUESTION_MESSAGE);

        //setting default values to test
        cheqNum = 12345678912l;
        qtyCheq = 5;
        holderName = "Govinda Thapa";
        solName="Thapathali";

        //generating cheque
        GetCheque newCheque = new GetCheque();

        //setting printing values on cheque

        newCheque.setAccountHolderName(holderName);
        newCheque.setAccountNumber(accNum);
        newCheque.setNumberOfCheque(qtyCheq);
        newCheque.setChequeNumber(cheqNum);
        newCheque.setBranchName(solName);



        //Now setting paper and printer job
        Double width = 8.5 * 72.0;
        Double height =  4.5 * 72.0 * qtyCheq;
        double margin = 0;
        Paper paper = new Paper();
        paper.setSize(width, height);
        paper.setImageableArea(margin, margin, width - (margin * 2), height - (margin * 2));
        paper.setImageableArea(0, 0, width, height);
        PageFormat pf = new PageFormat();
        //Paper paper = pf.getPaper();
        pf.setPaper(paper);
        pf.setOrientation(PageFormat.PORTRAIT);
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(newCheque,pf);

        try {
            job.print();
        }
        catch(PrinterException pe) {
            System.out.println("error Occured" + pe.getMessage());
        }



    }

}

这是我的打印功能,可以打印所需的支票数。

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Govinda
 */
public class GetCheque implements Printable{

    // variable defnations
    //
    private String accountNumber;
    private long chequeNumber;
    private String branchName;
    private int numberOfCheque;
    private String accountHolderName;

    public GetCheque(){

        /*    this.accountHolderName="";
        this.accountNumber="";
        this.branchName="";
        this.numberOfCheque=0;
        this.chequeNumber=0l;*/


    }
    public String getAccountNumber() {
        return accountNumber;
    }

    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    public long getChequeNumber() {
        return chequeNumber;
    }

    public void setChequeNumber(long chequeNumber) {
        this.chequeNumber = chequeNumber;
    }

    public String getBranchName() {
        return branchName;
    }

    public void setBranchName(String branchName) {
        this.branchName = branchName;
    }

    public int getNumberOfCheque() {
        return numberOfCheque;
    }

    public void setNumberOfCheque(int numberOfCheque) {
        this.numberOfCheque = numberOfCheque;
    }

    public String getAccountHolderName() {
        return accountHolderName;
    }

    public void setAccountHolderName(String accountHolderName) {
        this.accountHolderName = accountHolderName;
    }

    //printing preferences
    private double xCoor = 0;
    private double yCoor = 0;
    private double margin = 0.25 * 72.0;
    private double marginX = 0.5 * 72.0;
    private Font mainFont = new Font("Roman PS", Font.PLAIN, 9);    


    // Print method to print 

    @Override
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

        if(pageIndex > 0) {
            return Printable.NO_SUCH_PAGE;
        }


        //graphics.setFont(mainFont);
        //int charHeight = graphics.getFontMetrics().getHeight();

        /*        //graphics.translate(0,0);
        //for(int i=0;i<numberOfCheque;i++) {
        graphics.drawString(String.valueOf(chequeNumber), (int )(xCoor + marginX), (int)(yCoor + margin));
        yCoor=yCoor+(4*charHeight);
        yCoor=yCoor+charHeight;
        graphics.drawString(branchName, (int)(xCoor + marginX), (int)(yCoor + margin));
        yCoor=yCoor+charHeight;
        graphics.drawString(accountNumber, (int)(xCoor + marginX), (int)(yCoor + margin));
        yCoor=yCoor+charHeight;
        graphics.drawString(accountHolderName, (int)(xCoor + marginX), (int)(yCoor + margin));
        yCoor=yCoor+(3*charHeight);
        chequeNumber = chequeNumber + 1;

        //}*/


        Graphics2D g2d = (Graphics2D)graphics;
        g2d.setFont(mainFont);

        int charHeight = g2d.getFontMetrics().getHeight();

        // Perform rendering
        g2d.translate(0f,0f);

        for(int i=0;i<numberOfCheque;i++) {

        g2d.drawString(String.valueOf(chequeNumber), (float)(xCoor + marginX), (float)(yCoor + margin));
        yCoor=yCoor+(4*charHeight);
        yCoor=yCoor+charHeight;
        g2d.drawString(branchName, (float)(xCoor + marginX), (float)(yCoor + margin));
        yCoor=yCoor+charHeight;
        g2d.drawString(accountNumber, (float)(xCoor + marginX), (float)(yCoor + margin));
        yCoor=yCoor+charHeight;
        g2d.drawString(accountHolderName, (float)(xCoor + marginX), (float)(yCoor + margin));
        yCoor=yCoor+(3*charHeight);
        chequeNumber = chequeNumber + 1;

        }

        return 0;
    }


}

我设置页面的高度相对于要打印的支票I hvae的数量。但它没有用。我需要做什么才能在DOT Matrix打印机上的连续纸张中打印所需数量的支票。

此代码通常可以打印最多5个检查(从页面的一半开始打印),但是如果我将qtyCheque设置为更高的数字,如10或15,那么它根本不打印。

如何从纸张的开头开始打印并将纸张高度设置为相对于要打印的支票数量?

感谢您的回答,但是如何编写我自己的打印机驱动程序,以及如何针对其原始驱动程序安装它?我可以将其安装为不同的组件,还是可以在我的代码中?我只需要打印文本和数字,这应该是支持反斜杠字符,如\ n,\ t等。现在你可以帮我吗?我已经尝试了给定的选项,但问题是变量chequeNumber增加而不打印,然后它会打印内容增加chequeNumber。所以我想尝试你的另一个想法,即为打印机制作驱动程序。我应该怎么做才能为自己制作打印机驱动程序?如果可能,请详细解释。

*

*

1 个答案:

答案 0 :(得分:1)

Java Printing API无法很好地处理连续纸张。你有两个选择:

  1. 收集5份表格的所有信息,并将它们作为单页打印在一起。
  2. 将页面大小缩小为单个表单,并始终按顺序打印其中的5个(因此有人可以将它们从打印机中取出)。
  3. 请注意,您应始终尝试以5种形式(即两次穿孔之间的数字)批量打印。否则,撕掉将是一个问题。

    如果无法使其工作,则大多数点阵打印机接受通过并行设备发送的命令。这样,您可以编写自己的打印机驱动程序,它允许您打印单行文本(无需打扰页面等)并发送换页符以移动到下一个穿孔。

    修改

    至于编写打印机驱动程序:除非您需要打印精美的彩色图形,否则您可以使用简单的&#34;写入与转义码混合的文本&#34;到打印机。

    检查您的打印机手册。应该有一个关于escape sequencesPCL的部分。例如,将"\x1bEHello\x1bF\r\n"发送到打印机将打印单词&#34; Hello&#34;以粗体显示,然后转到下一行。

    以下是常见转义序列的非常好的概述:http://whitefiles.org/b1_s/1_free_guides/fg2cd/pgs/c03c_prntr_cds.htm