使用iText,在内存上生成一个在磁盘上生成的PDF [英] Using iText, generate on memory a PDF that is generated on disk instead

查看:112
本文介绍了使用iText,在内存上生成一个在磁盘上生成的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Java应用程序生成PDF。 (并且效果很好)问题是PDF在磁盘上生成为:

I'm generating a PDF from a Java application. (And works great) the problem is that the PDF is generated on disk as:

        Document documento = new Document(PageSize.A4, 25, 25, 25, 25);
        PdfWriter writer = PdfWriter.getInstance(documento, new FileOutputStream("/Users/sheldon/Desktop/Registry.pdf"));
        documento.open();

        // Put some images on the PDF
        for( byte[] imagen : imagenes )
        {
            Image hoja = Image.getInstance(imagen);
            hoja.scaleToFit(documento.getPageSize().getHeight(), documento.getPageSize().getWidth());
            documento.add(hoja);
        }

        documento.addTitle("Generated Registry!");

        documento.close();

现在,由于用户将搜索PDF并打印它们,我不需要存储它们在磁盘上。我需要(如果可能的话)在内存中生成它们并使用命令打开(使用acrobat reader)该文档。

Now, as the user will search for the PDF and print them I don't need to store them on disk. I need (if possible) to generate them in memory and use a command to open (with acrobat reader) that document.

这可能吗?任何想法。

如果没有,有什么建议(根据您的经验)。

If not, what suggestions (on your experience) have.

先谢谢你。

编辑:

适用于标准Java桌面应用程序。

Is for an standard Java Desktop Application.

推荐答案

为此,Acrobat需要能够访问另一个进程(Java)的内存。这是不可能的。

For this to work, Acrobat would need to be able to access the memory of another process (Java). This is not possible.

您可能只想将文件写入系统的临时目录。

You might just want to write the files to the system's temporary directory.

如果您的应用程序在Acrobat中打开PDF后保持打开状态,您可能需要考虑使用 File.createTempFile()的组合 File.deleteOnExit() ,在JVM终止时删除文件。

If your application stays open after opening the PDF in Acrobat, you might want to look into using a combination of File.createTempFile() and File.deleteOnExit(), to have the file deleted upon termination of the JVM.

这篇关于使用iText,在内存上生成一个在磁盘上生成的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆