• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

how to renders password procatacted pdf file into image

 
Ranch Hand
Posts: 37
MyEclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags for your code
 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some (presumably newer) versions of the PDFFile class have methods that allow you to pass in passwords. You can find a recent version linked on the AccessingFileFormats page.
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!