使用java打印PDF文档而不使用FileInputStream
我使用java中的iText库生成了一个文档,使用以下代码:使用java打印PDF文档而不使用FileInputStream,java,printing,itext,java-print,Java,Printing,Itext,Java Print,我使用java中的iText库生成了一个文档,使用以下代码: Document document = new Document(); document.open(); 我通过向文档中添加表和数据来操作文档。现在我还是想打印文档;我找到了使用以下代码将文档发送到打印机的方法,但使用了输入流: InputStream inputStream = new FileInputStream("C://Housing Report(1).pdf");
Document document = new Document();
document.open();
我通过向文档中添加表和数据来操作文档。现在我还是想打印文档;我找到了使用以下代码将文档发送到打印机的方法,但使用了输入流:
InputStream inputStream = new FileInputStream("C://Housing Report(1).pdf");
Doc doc = new SimpleDoc(inputStream,
DocFlavor.INPUT_STREAM.AUTOSENSE,null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService services =
PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = services.createPrintJob();
try {
job.print(doc, aset);
} catch (Exception pe) {pe.printStackTrace();}
}
我的问题是如何打印iText文档而不必保存它并使用InputStream再次打印它?提前感谢。您不能打印“iText
文档”
,因为它只是一个添加内容的门面,然后将内容转发给侦听器,侦听器会尽快将数据序列化到各自的输出流中。不过,您可以使用ByteArrayOutputStream
将pdf保存在内存中,并将底层字节数组提供给打印服务。@mkl感谢您的澄清。我会尽力让它那样运作。再次感谢!