使用这个 java api:http ://docs.oracle.com/javase/6/docs/api/javax/print/package-summary.html
我已尝试根据需要修改此代码,目前我有:
import java.io.FileInputStream;
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 java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
public class pdfPrinter {
public static String CompletefileName; // calls from FileUploadController
public static void main(String args[]) {
FileInputStream psStream = null;
try {
psStream = new FileInputStream(CompletefileName); // this calls a location where the pdf file is stored
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null); // unsure what psStream is, not explained in the api, want to use this to print pdfs, will look at more
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
aset.add(new Copies(5)); //sets copies, will add this as a vairable so users can change this
aset.add(Sides.DUPLEX); // same as above
if (myPrinter != null) { // having issues setting myPrinter
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {
pe.printStackTrace();
}
} else {
System.out.println("no printer services found"); //error msg
}
}
}
目前这无法编译,因为我不知道如何myPrinter
在我的机器上设置默认打印机,这也将用于 jsf Web 应用程序,我可以通过在网页上按一下按钮来运行它吗?
这是打印pdf的正确方法吗?任何建议或教程都会非常有帮助