Need help with pdf-renderer

点点圈 提交于 2019-12-04 15:49:34

Try the following class:

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;
}
}

So call:

PDFFileUtility.getPDFFile(myCipherInputStream);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!