在点阵打印机上打印时遇到问题

时间:2014-01-30 07:53:28

标签: swing printing

我正在开发java swing桌面应用程序;我需要在点阵打印机上打印纸币,打印件将有名称,地址和表格,其中包含物品,数量,价格等,应根据纸张上的x,y位置打印,字体存储在数据库中。 但是如果我使用以下代码,则在打印中存在重叠/附加字母的问题:

class BillPrint implements ActionListener, Printable
{
    PrintMngt PM=new PrintMngt();
    public int print(Graphics gx, PageFormat pf, int page) throws PrinterException {
        if (page>0){return NO_SUCH_PAGE;}




        Graphics2D g = (Graphics2D)gx; //Cast to Graphics2D object
        g.translate(pf.getImageableX(), pf.getImageableY());                     


        Vector<Vector<Object>> data =PM.getvarientDetail(printID);

        for (int i = 0; i <data.size(); i++) {
            if(data.get(i).get(3).toString().equalsIgnoreCase("DYNAMIC"))
            {
                String bill_no=textField_Trans.getText();
                int TblH,TblL;

                Vector<String> Tbl_HL=PM.getTblHieghtNoLline(printID);
                //PRINT_ID0, QUERY_STATIC1, OBJECT_NAME2, QUERY_TYPE3, X4, Y5, WIDTH6,
                //ALIGN7, FONT8, F_SIZE9, F_STYLE10, SECTION11, LOOPES_NO12, OBJ_FORMAT13, VARIANT_ID14
                TblH=Integer.parseInt(Tbl_HL.get(0).toString());
                TblL=Integer.parseInt(Tbl_HL.get(1).toString());

                int x=Integer.parseInt(data.get(i).get(4).toString());
                int y=Integer.parseInt(data.get(i).get(5).toString());     
                String fName=data.get(i).get(8).toString();
                int fSize=Integer.parseInt(data.get(i).get(9).toString());
                String fStyle=data.get(i).get(10).toString();
                Font font=null;
                if(fStyle.equalsIgnoreCase("Plain"))
                {
                    font = new Font(fName,Font.PLAIN, fSize);
                }
                else if(fStyle.equalsIgnoreCase("Bold"))
                {
                    font = new Font(fName,Font.BOLD, fSize);
                }
                else if(fStyle.equalsIgnoreCase("Italic"))
                {
                    font = new Font(fName,Font.ITALIC, fSize);
                }
                else if(fStyle.equalsIgnoreCase("Bold Italic"))
                {
                    font = new Font(fName,Font.BOLD+ Font.ITALIC, fSize);
                }
                System.out.println("Myqry"+data.get(i).get(1).toString());
                Vector<String> Query_Static=PM.getQuery_Static(data.get(i).get(1).toString(),bill_no);
                for (int j = NoOfProd; j < Query_Static.size(); j++) {
                    g.drawString(Query_Static.get(j).toString(),x,y); 
                    y=y+TblH/TblL;
                    g.setFont(font);

                }

            }
        }
        return PAGE_EXISTS; //Page exists (offsets start at zero!)
    }

    public void actionPerformed(ActionEvent e) {

        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        boolean ok = job.printDialog();
        if (ok) {
            try {


                int ProductCnt= PM.getNoProduct(textField_Trans.getText().toString());//no. of products under given billno
                int TableLine=PM.getTblNoLline(printID);//no. of lines to print
                System.out.println("No of TableLines="+TableLine);
                System.out.println("No of Product="+ProductCnt);

                for (int i = 0; i <(TableLine/ProductCnt); i++) 
                {
                    job.print();
                    NoOfProd=NoOfProd+TableLine;
                }
                NoOfProd=0;

            } catch (PrinterException ex) {
                ex.printStackTrace();

            }
        }
    }//end actionPerformed
}//end BillPrint

我也试过将数据写入.txt文件然后打印它。这里输出是正确的,即字母不重叠,但在这种方法中,我无法为我的数据提供适当的位置。我使用以下方法:

private void printData(){
    File output = new File("E:\\PrintFile1.txt");
    output.setWritable(true);
    String billNo="B1000", patient = "ABC";
    try 
    {
        BufferedWriter out = new BufferedWriter(new FileWriter(output));
        out.write(billNo + "\n");
        out.write(patient + "\n" );
        out.write("\n");
        out.write("\n");           
        out.close();

    }
    catch (java.io.IOException e)
    {
        System.out.println("Failed to write Output");
    }


    FileInputStream textStream = null;
    try 
    {
        textStream = new FileInputStream("E:\\PrintFile1.txt");
    }
    catch (java.io.FileNotFoundException e)
    {
        System.out.println("Error trying to find the print file.");
    }

    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    Doc mydoc = new SimpleDoc(textStream, flavor, null);

    PrintService printer = PrintServiceLookup.lookupDefaultPrintService(); 
    DocPrintJob printJob = printer.createPrintJob();
    try 
    {
        printJob.print(mydoc, null);
    }
    catch (javax.print.PrintException e) 
    {
        JOptionPane.showMessageDialog(this, "Error occured while attempting to print.", "Error!", JOptionPane.ERROR_MESSAGE);
    }
}

1 个答案:

答案 0 :(得分:0)

基本上对于字母中的问题,我只为字符串中的每个字符添加一个空格

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


public class Print implements Printable {

    /*  Just add one space for all charaters   */

    String numero = "Numero Nro :";

    String numeroreplace = numero.replaceAll(".(?=.)", "$0 ");  


    public Print() {
        super();
    } 

    /* The font for you string */
    public int print(Graphics g,PageFormat pf, int page) throws PrinterException{

        Font textFont = new Font(Font.SANS_SERIF,Font.PLAIN,8);

        /* To set the position, you can use for or while if u need it. */

        g.setFont(textFont);
        g.drawString(numeroreplace,350,150);  
    }
}

最后,你需要复制所有这些代码,只需为代码中的所有字符添加一个空格。 注意:你必须从你的主程序打电话。