将 Java 框架保存为 Microsoft Word 或 PDF 文档?

发布于 2024-08-09 17:59:17 字数 384 浏览 1 评论 0原文

我正在开发一个计费程序 - 现在,当您单击相应的按钮时,它会生成一个框架,显示各种费用等,基本上是一张发票。有没有办法让用户可以选择将该框架保存为文档,Microsoft WordMicrosoft WorksPDF

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

同展鸳鸯锦 2024-08-16 17:59:17

一种方法是将帧保存为图像,您可以通过使用以下语法将其转换为图像来实现。

BufferedImage myImage = new BufferedImage(size.width,size.height,
                            BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = myImage.createGraphics();

myComponent.paint(g2);

然后您可以保存该图像并将其传递到 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.

BufferedImage myImage = new BufferedImage(size.width,size.height,
                            BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = myImage.createGraphics();

myComponent.paint(g2);

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).

酒中人 2024-08-16 17:59:17
  1. BufferedImage 中绘制 JFrameJFramepaint() 方法
  2. 将图像保存为 jpg 或 png 或任何图像格式
  3. 获取一些 pdf 库并创建一个空白 pdf(例如 iText)
  4. 将图像插入 PDF文档
  5. 保存 - 完成
  1. Paint JFrame in a BufferedImage. paint() method of JFrame
  2. Save the image as jpg or png or whatever image format
  3. Take some pdf library and create a blank pdf (e.g. iText)
  4. Insert the image into the PDF document
  5. Save it - done
甜味拾荒者 2024-08-16 17:59:17

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).

勿忘初心 2024-08-16 17:59:17

我可能会从稍微不同的方向来看待这个问题,而不是问如何将 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).

岛歌少女 2024-08-16 17:59:17

您可能想查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文