Hi,
i have a task to renders password protected pdf file into thumbnails image, I did it with out password protected but unsuccessful when a pdf is password protected.
My old code is
<%@page import="com.sun.pdfview.PDFFile, com.sun.pdfview.PDFPage" %>
<%@page import="java.awt.Image, java.awt.Rectangle" %>
<%@page import="java.awt.Graphics2D, java.awt.image.BufferedImage"%>
<%@page import="java.nio.ByteBuffer, java.nio.channels.FileChannel" %>
<%@page import="java.io.*, javax.imageio.ImageIO" %>
<html>
<head><title>Image Rendering</title></head>
<body>
<%
File file = new File("D:\\Tomcat 5.5\\webapps\\ROOT\\PDF_Test\\R-intro.pdf");
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
// draw the first page to an image
PDFPage dpage = pdffile.getPage(0);
//get the width and height for the
doc at the default zoom
Rectangle rect = new Rectangle(0,0, (int)dpage.getBBox().getWidth(), (int)dpage.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);
//generate the image
Image img = dpage.getImage(
rect.width, rect.height, //width & height
rect, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
%>
<%
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(img, 0, 0, null);
ImageIO.write(bufferedImage, "jpg", new File("D:\\Tomcat 5.5\\webapps\\ROOT\\PDF_Test\\Image.jpg" ));
/*response.setContentType("image/png");
OutputStream strm = response.getOutputStream();
ImageIO.write(bufferedImage,"png",strm);
strm.close(); */
%>
<%="Pdf is renders into jpg image which save to location at D:\\Tomcat5.5\\webapps\\ROOT\\PDF_Test\\R-intro.jpg" %>
</body>
</html>
Any suggestion is welcome.