java 循环文件夹读取PDF,并转换成jpg

package pdf;

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 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 PdfToJpgTest {
 public static void setup(File file) throws IOException {
  // load a pdf from a byte buffer
  /*File file = new File(
    "c://xxxxx.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());

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(
     "C:/Users/Bonnie/Desktop/内容发布/img/"+file.getName()+ i + ".jpg"); // 输出到文件流
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   encoder.encode(tag); // JPEG编码

out.close();
  }

// show the image in a frame
  // JFrame frame = new JFrame("PDF Test");
  // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  // frame.add(new JLabel(new ImageIcon(img)));
  // frame.pack();
  // frame.setVisible(true);
 }
  public static void showAllFiles(File dir) throws Exception{
    int pdfCount=0;
      File[] fs = dir.listFiles();
      for(int i=0; i<fs.length; i++){
        if(fs[i].isDirectory()){
            try{
             showAllFiles(fs[i]);
            }catch(Exception e){}
        }else{
         String ffHouZhui=fs[i].getName().substring(fs[i].getName().lastIndexOf(".")+1,fs[i].getName().length());//后缀名
         if(ffHouZhui.equals("pdf")){//pdf文件
          pdfCount++;
          System.out.println(pdfCount+">>>>>>>>>"+fs[i].getName());    
          setup(fs[i]);
         }
        }
      }
   }
 public static void main(final String[] args) {
     //递归显示C盘下所有文件夹及其中文件
        File root = new File("C:/Users/Bonnie/Desktop/内容发布");
     try {
      showAllFiles(root);
     } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
 }

}

备注:com.sun.pdfview,jar包需要下载

时间: 2024-08-23 11:35:54

java 循环文件夹读取PDF,并转换成jpg的相关文章

python 将文件夹内的图片转换成PDF

import os import stringfrom PIL import Imagefrom reportlab.lib.pagesizes import A4, landscapefrom reportlab.pdfgen import canvasimport sys def file_name(file_dir, suffix = ".jpg"): L=[] for root, dirs, files in os.walk(file_dir): for file in fil

Java生成文件夹

Java生成文件夹 1.说明 判断文件夹是否存在,如果不存在就创建该文件夹,并打印其路径:如果存在,打印其路径 2.实现源码 /** * @Title:BuildFolder.java * @Package:com.you.freemarker.model * @Description:创建文件夹 * @author:Youhaidong(游海东) * @date:2014-6-30 下午10:47:06 * @version V1.0 */ package com.you.freemarker

java打开文件夹(含判断操作系统工具类和解压缩工具类)

1.Runtime.getRuntime().exec("explorer D:\\Java"); 2.java.awt.Desktop.getDesktop().open(new File("D:\\Java")); 4.java.awt.Desktop.getDesktop().browse(...) 3. try { String[] cmd = new String[5]; cmd[0] = "cmd"; cmd[1] = "/

解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法

建立好一个Maven项目后,如果Java Resources资源文件下没有src/main/java文件夹,并且在手动创建这个文件时提示“已存在文件”. 这说明,在这个项目配置中已经有了src/main/java这个文件夹,至于为什么不显示,我暂时也还不清楚,希望谁明白了跟我下,谢了.(已解决) 第一种方法: 打开项目的classpath文件:如下 <?xml version="1.0" encoding="UTF-8"?><classpath&g

Java删除文件夹和文件

Java删除文件夹和文件 以前在javaeye看到过关于Java操作文件的一篇文章,写的很好,但找了半天也没找到,就把找到底几篇文章整理一下,做个总结,算是一个学习备份…… 1,验证传入路径是否为正确的路径名(Windows系统,其他系统未使用) Java代码 // 验证字符串是否为正确路径名的正则表达式 private static String matches = "[A-Za-z]:\\\\[^:?\"><*]*"; // 通过 sPath.matches(

java 遍历文件夹里的文件

Java遍历文件夹的2种方法: A.不使用递归: import java.io.File; import java.util.LinkedList; public class FileSystem { public static void main(String[] args) { long a = System.currentTimeMillis(); LinkedList list = new LinkedList(); File dir = new File("c:\\java\\&quo

解决Eclipse建立Maven项目后无src/main/java资源文件夹的办法

建立好一个Maven项目后,如果Java Resources资源文件下没有src/main/java文件夹,并且在手动创建这个文件时提示"已存在文件".这说明,在这个项目配置中已经有了src/main/java这个文件夹. 解决方法: 在项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE system Library,配置JDK路径.

java遍历文件夹

使用递归: import java.io.File; import java.util.ArrayList; public class FileSystem1 { private static ArrayList filelist = new ArrayList(); public static void main(String[] args) { long a = System.currentTimeMillis(); refreshFileList("C:/Users/shibin/Desk

JAVA 遍历文件夹下的所有文件

JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = 0, folderNum = 0; File file = new File(path); if (file.exists()) { LinkedList<File> list = new LinkedList<File>(); File[] files = file.listFile