JavaPDF文件转图片

来源:互联网 发布:recscreen录屏软件 编辑:程序博客网 时间:2024/06/03 14:01

方案一 使用PDFRenderer.jar实现PDF转图片:

1、需要导入PDFRenderer.jar

package lcl;import java.awt.Image;  import java.awt.Rectangle;  import java.awt.image.BufferedImage;   import java.io.File;  import java.io.FileOutputStream;  import java.io.IOException;  import java.io.RandomAccessFile;  import java.nio.ByteBuffer;  import java.nio.channels.FileChannel;  import javax.swing.SwingUtilities;  import com.sun.image.codec.jpeg.JPEGCodec;  import com.sun.image.codec.jpeg.JPEGImageEncoder;  import com.sun.pdfview.PDFFile;  import com.sun.pdfview.PDFPage;   public class PDFRendererTest {      public static void setup() throws IOException {           // load a pdf from a byte buffer          File file = new File("F:\\Eclipse\\workspace\\Test\\pdfPicFile/000000091534588.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);           System.out.println("页数: " + pdffile.getNumPages());           String getPdfFilePath = System.getProperty("user.dir")+"\\pdfPicFile";               System.out.println("getPdfFilePath is  :"+getPdfFilePath);               for (int i = 1; i <= pdffile.getNumPages(); i++) {              // draw the first page to an image              PDFPage page = pdffile.getPage(i);               // get the width and height for the doc at the default zoom              Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()                      .getWidth(), (int) page.getBBox().getHeight());               // generate the image              Image img = 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                      );               BufferedImage tag = new BufferedImage(rect.width, rect.height,                      BufferedImage.TYPE_INT_RGB);              tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height,                      null);                                             FileOutputStream out = new FileOutputStream( getPdfFilePath+"/" + i + ".jpg"); // 输出到文件流            System.out.println("成功保存图片到 :  " +getPdfFilePath+"/" + i + ".jpg");                       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);              encoder.encode(tag); // JPEG编码               out.close();          }       }       public static void main(final String[] args) {          SwingUtilities.invokeLater(new Runnable() {              public void run() {                  try {                      PDFRendererTest.setup();                  } catch (IOException ex) {                      ex.printStackTrace();                  }              }          });      }   } 

这种方案可以实现PDF转换为图片,但是转换的质量有些问题。

方案二 使用PDFbox进行转换:

导入pdfbox-2.0.4.jar,fontbox-2.0.4.jar,commons-logging.jar

package lcl;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.rendering.PDFRenderer;public class PDFbox {public static void pdfToImage(){long begin = System.currentTimeMillis();    File file = new File("F:\\Eclipse\\workspace\\Test\\pdfPicFile/000000091534588.pdf");//    File file = new File("F:\\Eclipse\\workspace\\Test\\pdfPicFile/000000273700188.pdf");    try {       PDDocument doc = PDDocument.load(file);       PDFRenderer renderer = new PDFRenderer(doc);       int pageCount = doc.getNumberOfPages();       for(int i=0; i<pageCount; i++){           BufferedImage image = renderer.renderImage(i, 2.5f);//           BufferedImage image = renderer.renderImageWithDPI(i,296);           ImageIO.write(image,"PNG",new File("F:\\Eclipse\\workspace\\Test\\pdfPicFile/PDFBOX/"+i+".png"));        }   } catch (IOException e) {       e.printStackTrace();   }    long end = System.currentTimeMillis();    System.out.println("耗时:"+(end-begin)*0.001);}public static void main(String[] args) {pdfToImage();}}

这种转换的效果是最好的,但是时间比较长,一张清洗的照片打印需要6s左右,耗时太久。

方案三 使用ghostscript 进行转换(推荐):

我们这里使用的是ghostscript-9.15-linux-x86_64(它是一个安装在linux上的绿色版软件,使用控制台的命令即可实现对PDF转换为图片)

具体步骤是:点击即可下载资源

1、首先将pdf2tif.tar.gz传输到/app目录下(pdf2tif.tar.gz这个是我将这个软件和一些写好的shell脚本进行压缩之后的文件,该文件必须放在/app下,否则里面的脚本不能执行)
2、然后进行解压:
tar -xzvf pdf2tif.tar.gz pdf2tif
3、赋权
chmod -R 777 pdf2tif
4、进入pdf2tif目录 使用脚本命令测试是否部署成功
cd pdf2tif/
./pdf2jpg.sh ./000000091534588.pdf /tmp pp

5、去/tmp目录下检查是否出现图片

原创粉丝点击