一些背景对这个问题可以在Check printing with Java/JSP被发现和Create a "print-only" PDF with itext如何打开与飞碟/ iText的创建PDF文件使用的打印对话框
我已经能够与打印成功创建和打开PDF利用iText-2.0.8对话框和下面的代码:
String outputFile = "firstdoc.pdf";
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));
writer.setOpenAction(new PdfAction(PdfAction.PRINTDIALOG));
document.open();
document.add(new Paragraph("TEST"));
document.close();
我也已经能够使用的飞碟使用下面的代码生成从XHTML一个PDF:
String inputFile = "firstdoc.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
不过,我似乎无法得到两个一起工作......
我想创建一个使用的飞碟在第二代码块的PDF 和我想设置该PDF的公开行动为PdfAction.PRINTDIALOG
。
如何获得这两组代码一起工作,使飞碟创建的PDF最初打开一个打印对话框?
非常感谢,您的文章帮助我解决了我的问题。但是,如果您只想打印PDF,则无需创建第二个PDF文件。我在下面做了以避免创建2个PDF文件: OutputStream os = new FileOutputStream(outputFile); ByteArrayOutputStream outputStreamForPrinter = new ByteArrayOutputStream(); PdfReader reader = new PdfReader(os.toByteArray()); PdfStamper stamper = new PdfStamper(reader,outputStreamForPrinter); – 2014-10-15 12:01:01