将 Java 框架保存为 Microsoft Word 或 PDF 文档?
我正在开发一个计费程序 - 现在,当您单击相应的按钮时,它会生成一个框架,显示各种费用等,基本上是一张发票。有没有办法让用户可以选择将该框架保存为文档,Microsoft Word、Microsoft Works 或 PDF?
I am working on a billing program - right now when you click the appropriate button it generates a frame that shows the various charges etc, basically an invoice. Is there a way to give the user an option of saving that frame as a document, either Microsoft Word, Microsoft Works or PDF?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一种方法是将帧保存为图像,您可以通过使用以下语法将其转换为图像来实现。
然后您可以保存该图像并将其传递到 jasper 报告中。然后,您可以从 JasperPrint 对象保存为几种不同的格式,包括 pdf。更好但类似的方法是将 Graphics 上下文传递到 JasperReports(jasper 中有一个渲染器可以执行此操作,并且质量要好得多)。
One approach would be to save the frame as an image, you can do that by using the following syntax to convert it to an image.
you can then save this image and pass it into a jasper report. From the JasperPrint object you can then save in a few different formats, including pdf. A better but similar approach would be to pass the Graphics context into JasperReports(there is a renderer to do this in jasper, and the quality is much better).
BufferedImage
中绘制JFrame
。JFrame
的paint()
方法JFrame
in aBufferedImage
.paint()
method ofJFrame
我宁愿使用 iText 这样的 Java 库来生成 PDF,而不是生成 Word 文档文档(更便携),或者更好的是 JasperReport 报告库可以按照bigbrother82<的建议以多种格式(PDF、XML、HTML、CSV、XLS、RTF、TXT)输出报告/a> 在评论中。在我看来,这比使用图像更干净,特别是对于打印(甚至没有提到您的发票可能是多页文档)。
Instead of generating a word document, I'd rather use a Java library like iText to produce a PDF document (more portable) or, even better, the JasperReport report library that can output reports in a wide range of formats (PDF, XML, HTML, CSV, XLS, RTF, TXT) as suggested by bigbrother82 in a comment. This looks cleaner to me than using an image, especially for printing (not even mentioning that your invoice may be a multi-page document).
我可能会从稍微不同的方向来看待这个问题,而不是问如何将 GUI 表单按原样放入 PDF 或 Word 文档中,而是问如何将该内容放入 Word/PDF 文档中。
这个问题的答案是 Apache FOP。生成一个 XSL-FO 文件并要求 FOP 将其转换为 RTF 文档(带有 . DOC 扩展名)或 PDF。
通常,可以通过生成一个包含需要打印的数据的 XML 文件来完成此操作。然后使用 XSLT 将该 XML 转换为 XSL-FO。然而,我发现使用模板语言(例如 Freemarker)直接生成 XSL-FO 文件更容易。
I'd likely look at this from a slightly different direction and instead of asking how to splat the GUI form as-is into a PDF or word document I'd ask how to get that content into a Word/PDF document.
The answer to that question is Apache FOP. Generate a XSL-FO file and ask FOP to convert it into a RTF document (with a .DOC extension) or a PDF.
Normally one does this by generating an XML file containing the data you need printed. Then use an XSLT to convert that XML to XSL-FO. I however found it easier to generate a XSL-FO file directly using a templating language (such as Freemarker).
您可能想查看 Docmosis 的在线演示,该演示为用户提供了请求选项前面的文档。该演示会进行下载,但它可以将文档定向到框架中,然后将其留给浏览器显示。这种工作方式(正如其他人所提到的)是从不同的角度看待问题并预先决定什么格式,而不是事后然后尝试保存帧内容。
You might want to look at the online demo for Docmosis as an example which gives the user the options for requesting the document up front. That demo does a download, but it could direct the document into a frame instead and leave it to the browser to display. This style of working (as metioned by others) is looking at the problem from a different angle and deciding up front what format, rather than after the fact and then trying to save the frame contents.