我有一个使用 iText 库生成的 PDF 文档,然后我使用 ByteArrayOutputStream 将文档保存在内存中进行打印,但它没有打印任何内容。关于为什么不打印的任何想法?您可以在下面找到代码,并提前致谢。
ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter.getInstance(document, byteArr);
document.open();
/*
* Adding data and tables to the document
*/
document.close();
DocFlavor docType = DocFlavor.BYTE_ARRAY.AUTOSENSE;
byte[] byteStream = byteArr.toByteArray();// fetch content in byte array;
// byteArr is the ByteArrayOutputStream object
// Tried using InputStream but did not work as well.
Doc documentToBePrinted = new SimpleDoc(byteStream, docType, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService services = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = services.createPrintJob();
try {
job.print(documentToBePrinted, aset);
System.out.println("Donee");
}
catch (Exception pe)
{
pe.printStackTrace();
}
byteArr = null;
}