为什么不打印到我的打印机?

时间:2015-04-26 19:02:57

标签: java printing

我需要以编程方式生成String打印到我的打印机。但是,每当我运行此代码时,打印机都不会打印任何内容。

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class PrintMain {
    public static void main(String[] args) {
        PrintService[] ps = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE,null);
        String[] psNames = new String[ps.length];
        for(int i = 0;i < ps.length;i++) {
            psNames[i] = ps[i].getName();
        }
        String choiceString = (String)JOptionPane.showInputDialog(new JFrame(),"What printer do you want to use?","Printer",JOptionPane.PLAIN_MESSAGE,null,psNames,null);
        PrintService choice = null;
        for(int i = 0;i < psNames.length;i++) {
            if (choiceString.equals(psNames[i])) {
                choice = ps[i];
            }
        }
        if(choice!=null) {
            DocPrintJob job = choice.createPrintJob();
            String text = "This is a test string. A method call would usually generate text for this, but TL;DR.";
            InputStream stream = new  = ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
            Doc doc = new SimpleDoc(stream,DocFlavor.INPUT_STREAM.AUTOSENSE,null);
            try {
                PrintJobWatcher watch = new PrintJobWatcher();
                job.addPrintJobListener(watch);
                PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                pras.add(new Copies(1));
                job.print(doc,pras);
                watch.waitForDone();
            } catch(PrintException e1) {
                JOptionPane.showMessageDialog(new JFrame(),"A PrintException has occurred. Message: "+e1.getMessage());
            } finally {
                try {
                    stream.close();
                } catch(IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

这是我的PrintJobWatcher课程:

import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;

public class PrintJobWatcher implements PrintJobListener {
    public boolean dataTransComplete=false;
    @Override public void printDataTransferCompleted(PrintJobEvent arg0) {
        dataTransComplete=true;
    }
    @Override public void printJobCanceled(PrintJobEvent arg0) {}
    @Override public void printJobCompleted(PrintJobEvent arg0) {}
    @Override public void printJobFailed(PrintJobEvent arg0) {}
    @Override public void printJobNoMoreEvents(PrintJobEvent arg0) {}
    @Override public void printJobRequiresAttention(PrintJobEvent arg0) {}
    public void waitForDone() {
        System.out.println("Waiting...");
        try {
            while(!dataTransComplete) {
                wait();
            }
        } catch(InterruptedException e) {}
        System.out.println("Done!");
    }
}

我无法弄清楚为什么String没有打印出来。我做错了什么?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。用它来打印东西:

int row = View_Table.getSelectedRow();
            String Table_click=(View_Table.getModel().getValueAt(row, 0).toString());

            query = "SELECT DocumentFile FROM tourClient WHERE ClientID = '"+Table_click+"' ";
             PreparedStatement stm = db.getconn().prepareStatement(query);
            rs = stm.executeQuery();
             if(rs.next()){


                byte[] fileBytes = rs.getBytes("DocumentFile");




             }  

它甚至为打印机设置打开了一个小对话框。