我使用的PDF渲染器来查看我的Java应用程序中的PDF文件。它的工作完全正常的PDF文件。

然而,我希望应用程序能够显示加密的PDF文件。该ecrypted文件将被解密的 CipherInputStream ,但我不希望保存在磁盘上的解密数据。在尝试把一个办法可以通过从的 CipherInputStream 以在 PDFFile 构造,而无需将decryted数据写入文件中。

的decryted数据

我也明白,如果有人能链接到PDF,渲染教程帮助下,使我可以更读了。

感谢。

有帮助吗?

解决方案

尝试使用以下类:

import com.sun.pdfview.PDFFile;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class PDFFileUtility {
private static final int READ_BLOCK = 8192;

public static PDFFile getPDFFile(InputStream in) throws IOException {
   ReadableByteChannel bc = Channels.newChannel(in);
   ByteBuffer bb = ByteBuffer.allocate(READ_BLOCK);
    while (bc.read(bb) != -1) {
        bb = resizeBuffer(bb); //get new buffer for read
    }
   return new PDFFile(bb);

}

 private static ByteBuffer resizeBuffer(ByteBuffer in) {
   ByteBuffer result = in;
   if (in.remaining() < READ_BLOCK) {
    result = ByteBuffer.allocate(in.capacity() * 2);
    in.flip();
    result.put(in);
   }
   return result;
}
}

所以呼叫:

PDFFileUtility.getPDFFile(myCipherInputStream);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top