Pdf-renderer - http://java.net/projects/pdf-renderer
Download the source code using SVN and use ant to build.
After running ant build, copy PDFRenderer.jar from dist folder and set into your project classpath.
You can use this api to generate Image/Thumnail for various PDF pages.
Java 5 supports following image types : "bmp jpeg wbmp gif jpg png" and mime types "image/png image/jpeg image/x-png image/vnd.wap.wbmp image/bmp image/gif".
API will fail to generate image If pdf has unsupported java image type like cmyk embedded.
PDF Renderer is a LGPL licensed pure-java library that makes this very simple:
public class Pdf2Image {
/**
* @param args
*/
public static void main(String[] args) {
File file = new File("c:/sample.pdf");
RandomAccessFile raf;
try {
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
int num=pdffile.getNumPages();
for(int i=0;i<num;i++)
{
PDFPage page = pdffile.getPage(i);
//get the width and height for the doc at the default zoom
int width=(int)page.getBBox().getWidth();
int height=(int)page.getBBox().getHeight();
Rectangle rect = new Rectangle(0,0,width,height);
int rotation=page.getRotation();
Rectangle rect1=rect;
if(rotation==90 || rotation==270)
rect1=new Rectangle(0,0,rect.height,rect.width);
//generate the image
BufferedImage img = (BufferedImage)page.getImage(
rect.width, rect.height, //width & height
rect1, // clip rect
null, // null for the ImageObserver
true, // fill background with white
true // block until drawing is done
);
ImageIO.write(img, "png", new File("c:/"+i+".png"));
}
}
catch (FileNotFoundException e1) {
System.err.println(e1.getLocalizedMessage());
} catch (IOException e) {
System.err.println(e.getLocalizedMessage());
}
}
}
No comments:
Post a Comment