gpt4 book ai didi

java - 有没有办法让这个 "send to printer"Java 方法起作用?

转载 作者:行者123 更新时间:2023-11-30 10:26:23 24 4
gpt4 key购买 nike

public static void sendPdfToPrinter(String epsilon)
{
FileInputStream psStream = null;
try {
psStream = new FileInputStream(epsilon);
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);

// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++)
{
String svcName = services[i].toString();
System.out.println("service found: "+ svcName);
if (svcName.contains("series"))
{
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}

if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);

} catch (Exception pe) {pe.printStackTrace();}
} else {
System.out.println("no printer services found");
}
}

public static void main(String[] args) throws IOException
{
//@SuppressWarnings("unused")
//Testing t = new Testing();
String DEST = ("C:/Users/Brian/Desktop/SO046201R-17/TestingAlpha/6FS-2m.pdf");
sendPdfToPrinter(DEST);
}

我目前正在编写一个程序,它将文本写入一系列 PDF 文件,然后将它们发送到打印机。我已经完成了对 PDF 部分的写入,但是每当我尝试将文件传递给“sendPdfToPrinter”方法时,我都会遇到问题。到目前为止,我已经测试了一台 HP Deskjet 打印机和一台 Canon Inkjet 打印机,但没有成功(前者给出了“Java 文档错误”消息,后者不会将文件添加到队列中)。我想我的问题归结为:

是代码问题还是我使用的打印机问题?有解决方法吗?

使用 Mark 的编辑:

public static void sendPdfToPrinter(String epsilon) throws 
InvalidPasswordException, IOException
{
FileInputStream psStream = null;
try {
psStream = new FileInputStream(epsilon);
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
PDDocument myDoc = PDDocument.load(new File(epsilon));
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services =
PrintServiceLookup.lookupPrintServices(psInFormat, aset);

// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++)
{
String svcName = services[i].toString();
System.out.println("service found: "+ svcName);
if (svcName.contains("series"))
{
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}

if (myPrinter != null) {
PrinterJob job = PrinterJob.getPrinterJob();

try {
job.setPrintService(myPrinter);
job.setPageable(new PDFPageable(myDoc));
job.print();
} catch (PrinterException e) {
// Handle the exception.
}
} else {
System.out.println("no printer services found");
}

}

最佳答案

这可能是因为您的打印机本身不支持 PDF。实际上,我设法用您的代码打印了一个 PDF,但它打印出来的效果就像将 PDF 文件作为文本文件打开一样。

一种解决方案是使用 Apache 的 Java PDF 库:PDFBox .该文件可以像这样加载:

PDDocument myDoc = PDDocument.load(new File(epsilon));

要打印文件,请使用 PrinterJob 而不是 DocPrintJob:

PrinterJob job = PrinterJob.getPrinterJob();

try {
job.setPrintService(myPrinter);
job.setPageable(new PDFPageable(myDoc));
job.print();
} catch (PrinterException e) {
// Handle the exception.
}

关于java - 有没有办法让这个 "send to printer"Java 方法起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45746602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com