我需要從純文本文件創(chuàng)建一個(gè) pdf 文件。我認(rèn)為最簡(jiǎn)單的方法是讀取這些文件并將它們打印到 PDF 打印機(jī)。我的問(wèn)題是,如果我打印到 pdf 打印機(jī),結(jié)果將是一個(gè)空的 pdf 文件。如果我打印到 Microsoft XPS Document Writer,文件將以純文本格式創(chuàng)建,而不是 oxps 格式。我會(huì)對(duì)兩步或三步的解決方案感到滿意。(例如,先轉(zhuǎn)換為 xps,然后使用 ghostscript 或類似方法轉(zhuǎn)換為 pdf)。我嘗試了幾種 pdf 打印機(jī),例如:CutePDF、Microsoft PDF writer、Bullzip PDF。每個(gè)人的結(jié)果都是一樣的。環(huán)境為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); }}
添加回答
舉報(bào)
0/150
提交
取消