2011-02-08 18 views
19

क्या कोई मुझे एक मुफ्त जावा लाइब्रेरी सुझा सकता है जो पीडीएफ को परिवर्तित कर सकता है और पहले पृष्ठ से थंबनेल छवि (पीएनजी) बना सकता है।जावा में थंबनेल छवि में पीडीएफ कनवर्ट करें

धन्यवाद।

उत्तर

28

आप pdf-renderer को आजमा सकते हैं यह एक शुद्ध जावा समाधान है। निम्नलिखित कोड पहले पृष्ठ की एक छवि बनाता है।

File pdfFile = new File("/path/to/pdf.pdf"); 
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r"); 
FileChannel channel = raf.getChannel(); 
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); 
PDFFile pdf = new PDFFile(buf); 
PDFPage page = pdf.getPage(0); 

// create the image 
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), 
           (int) page.getBBox().getHeight()); 
BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height, 
            BufferedImage.TYPE_INT_RGB); 

Image image = page.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(image, 0, 0, null); 
ImageIO.write(bufferedImage, format, new File("/path/to/image.jpg")); 
9

उत्कृष्ट sdorra, आपके इनपुट के लिए धन्यवाद। मैंने पीडीएफ से सभी पृष्ठों को कन्वर्ट करने के लिए अपना उदाहरण दोबारा काम किया है।

आशा है कि आप में से कुछ लोगों की मदद करेगा।

import java.awt.Graphics2D; 
import java.awt.Image; 
import java.awt.Rectangle; 
import java.awt.image.BufferedImage; 
import java.io.File; 
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 Main { 

    public static void main(String[] args) throws IOException { 
     File pdfFile = new File("c:\\YOURPDF.pdf"); 
     RandomAccessFile raf = new RandomAccessFile(pdfFile, "r"); 
     FileChannel channel = raf.getChannel(); 
     ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); 
     PDFFile pdf = new PDFFile(buf); 

     for (int i=0; i<pdf.getNumPages(); i++) { 
      createImage(pdf.getPage(i), "c:\\PICTURE_" + i + ".jpg"); 
     } 
    } 

    public static void createImage(PDFPage page, String destination) throws IOException{ 
     Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), 
       (int) page.getBBox().getHeight()); 
     BufferedImage bufferedImage = new BufferedImage(rect.width, rect.height, 
         BufferedImage.TYPE_INT_RGB); 

     Image image = page.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(image, 0, 0, null); 
     ImageIO.write(bufferedImage, "JPG", new File(destination)); 
    } 

} 

आप pdf-renderer-1.0.5.jar

The Source for Java Technology Collaboration website

से पुस्तकालय डाउनलोड कर सकते हैं
संबंधित मुद्दे