Hi ,

I'm writing simple Java program render PDF file to Images by using  Java PDF
render  library. Code is given below and attach to the mail,


import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.imageio.ImageIO;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

public class RenderPDF2Image {

    public static void main(String[] args) {

        File file = new File("file.pdf");
        RandomAccessFile randomFile;

        try {

            randomFile = new RandomAccessFile(file, "r");
            FileChannel channel = randomFile.getChannel();
            ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY,
0, channel.size());
            PDFFile pdffile = new PDFFile(buffer);
            int numberOfPages = pdffile.getNumPages();

            for (int i = 0; i < numberOfPages; i++) {

                PDFPage page = pdffile.getPage(i);
                int pageWidth = (int) page.getBBox().getWidth();
                int pageHeight = (int) page.getBBox().getHeight();
                Rectangle rectangle = new Rectangle(0, 0, pageWidth,
pageHeight);
                Rectangle rectangle2DClip = rectangle;

                BufferedImage image;
                image = (BufferedImage) page.getImage(rectangle.width,
rectangle.height, rectangle2DClip, null, true, true);

                // Output PDF to png
                ImageIO.write(image, "png", new File("image" + i + ".png"));
                // Output PDF to gif
                ImageIO.write(image, "gif", new File("image" + i + ".gif"));
                // Output PDF to jpg
                ImageIO.write(image, "jpg", new File("image" + i + ".jpg"));
            }

        } catch (FileNotFoundException e1) {

            System.out.println(e1.getLocalizedMessage());

        } catch (IOException e) {

            System.out.println(e.getLocalizedMessage());

        }
    }
}




charith
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to