0
我想將字符串"Hello world"
打印爲PDF。這是我的代碼:無法從Java打印到PDF打印機
import java.io.ByteArrayInputStream;
import java.io.InputStream;
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.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
public class App {
public static void main(String[] args) throws Exception {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService service = ServiceUI.printDialog(null, 200, 200, services, null, flavor, pras);
// prints the famous hello world! plus a form feed
InputStream is = new ByteArrayInputStream("hello world!\f".getBytes("UTF8"));
Doc doc = new SimpleDoc(is, flavor, null);
DocPrintJob job = service.createPrintJob();
job.print(doc, pras);
}
}
當顯示「打印」對話框時,選擇「Foxif Reader PDF Printer」。打印作業似乎已提交,但我不知道在哪裏可以找到打印的PDF文件。理論上,打印機應詢問PDF文件的位置(因爲從Paint和Notepad打印時會詢問)。但沒有對話。這份工作是沉默的。
如何使我的代碼在打印到PDF時顯示標準Windows「另存爲」對話框?
嘗試此首選項[這裏] http://stackoverflow.com/questions/2885173/java-how-to-create-and-write-to-a-file – user3562918