亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 javax.print 將純文本文件打印到 PDF 打印機會產生一個空文件

使用 javax.print 將純文本文件打印到 PDF 打印機會產生一個空文件

慕勒3428872 2021-08-04 16:37:24
我需要從純文本文件創建一個 pdf 文件。我認為最簡單的方法是讀取這些文件并將它們打印到 PDF 打印機。我的問題是,如果我打印到 pdf 打印機,結果將是一個空的 pdf 文件。如果我打印到 Microsoft XPS Document Writer,文件將以純文本格式創建,而不是 oxps 格式。我會對兩步或三步的解決方案感到滿意。(例如,先轉換為 xps,然后使用 ghostscript 或類似方法轉換為 pdf)。我嘗試了幾種 pdf 打印機,例如:CutePDF、Microsoft PDF writer、Bullzip PDF。每個人的結果都是一樣的。環境為Java 1.7/1.8 Win10private void print() {    try {        DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;        PrintRequestAttributeSet patts = new HashPrintRequestAttributeSet();                           PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, patts);        if (ps.length == 0) {            throw new IllegalStateException("No Printer found");        }        System.out.println("Available printers: " + Arrays.asList(ps));        PrintService myService = null;        for (PrintService printService : ps) {            if (printService.getName().equals("Microsoft XPS Document Writer")) { //                 myService = printService;                break;            }        }        if (myService == null) {            throw new IllegalStateException("Printer not found");        }        myService.getSupportedDocFlavors();        DocPrintJob job = myService.createPrintJob();   FileInputStream fis1 = new FileInputStream("o:\\k\\t1.txt");        Doc pdfDoc = new SimpleDoc(fis1, DocFlavor.INPUT_STREAM.AUTOSENSE, null);        HashPrintRequestAttributeSet pr = new HashPrintRequestAttributeSet();        pr.add(OrientationRequested.PORTRAIT);        pr.add(new Copies(1));        pr.add(MediaSizeName.ISO_A4);        PrintJobWatcher pjw = new PrintJobWatcher(job);        job.print(pdfDoc, pr);        pjw.waitForDone();        fis1.close();    } catch (PrintException ex) {        Logger.getLogger(Docparser.class.getName()).log(Level.SEVERE, null, ex);    } catch (Exception ex) {        Logger.getLogger(Docparser.class.getName()).log(Level.SEVERE, null, ex);    }}
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 316 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號