我想从txt文档创建java中的打印服务,我需要在打印前设置边距和字体。
方法printjob
工作正常,但结果边距和字体不符合预期。如何解决我的问题?
:(
对不起我的英文:)感谢以前的帮助
public void test() {
FileInputStream inStream = null;
try {
inStream = new FileInputStream("Test.txt");
} catch (FileNotFoundException ffne) {
}
if (inStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(inStream, psInFormat, null);
String printerName = "\\\\\\192.168.1.8\\EPSON L210 Series";
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(new PrinterName(printerName, null));
PrintService[] printServices = PrinterJob.lookupPrintServices();
System.out.println("printServices = " + printServices.length);
System.out.println("printServices = " + printServices);
PrintService defaultPrintservice = printServices[0];
for (PrintService printService1 : printServices) {
System.out.println("printService1 = " + printService1.getName());
if (printService1.getName().contains("192.168.1.8")) {
defaultPrintservice = printService1;
break;
}
}
PrinterJob printerjob = PrinterJob.getPrinterJob();
try {
printerjob.setPrintService(defaultPrintservice);
} catch (PrinterException ex) {
Logger.getLogger(PrintingService.class.getName()).log(Level.SEVERE, null, ex);
}
DocPrintJob job = defaultPrintservice.createPrintJob();
try {
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
SimpleDoc simpleDoc = new SimpleDoc(inStream, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
aset.add(new Copies(1));
aset.add(MediaSizeName.ISO_A4);
aset.add(OrientationRequested.PORTRAIT);
aset.add(Sides.DUPLEX);
aset.add(JobSheets.NONE);
//aset.add((Attribute) DocFlavor.READER.TEXT_PLAIN);
job.print(simpleDoc, aset);
} catch (PrintException ex) {
Logger.getLogger(PrintingService.class.getName()).log(Level.SEVERE, null, ex);
}
}