新手,问个幼稚问题,怎样得到默认打印机中的默认的纸张大小,他的宽度...
发布网友
发布时间:2024-03-05 17:57
我来回答
共4个回答
热心网友
时间:2024-07-30 10:51
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.JFileChooser;
import java.awt.Frame;
import java.io.File;
import java.io.FileInputStream;
import java.util.*;
public class PrinterTest {
public static void main(String args[]){
JFileChooser fileChooser = new JFileChooser();
int state = fileChooser.showOpenDialog(null);
if(state == fileChooser.APPROVE_OPTION){
File file = fileChooser.getSelectedFile(); //获取选择的文件
//构建打印请求属性集
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用打印服务
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
//定位默认的打印服务
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//显示打印对话框
PrintService service = ServiceUI.printDialog(null, 200, 200, printService,
defaultService, flavor, pras);
if (service != null){
try {
DocPrintJob job = service.createPrintJob(); //创建打印作业
FileInputStream fis = new FileInputStream(file); //构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das); //建立打印文件格式
job.print(doc, pras);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
楼上的也真是的,将javadoc中的代码粘贴过来就混弄人家啊?
热心网友
时间:2024-07-30 10:50
DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
PrintRequestAttributeSet aset = new HashPrintRequestHashAttributeSet();
aset.add(MediaSizeName.ISO_A4);
PrintService[] pservices =
PrintServiceLookup.lookupPrintServices(flavor, aset);
if (pservices.length > 0) {
DocPrintJob pj = pservices[0].createPrintJob();
// InputStreamDoc is an implementation of the Doc interface //
Doc doc = new InputStreamDoc("test.ps", flavor);
try {
pj.print(doc, aset);
} catch (PrintException e) {
}
}
//几个属性一看就明白。
热心网友
时间:2024-07-30 10:57
你家电脑不是国产的?
热心网友
时间:2024-07-30 10:56
恩
是的