利用pdfbox和itext包将pdf转换为图片

时间:2022-11-11 21:39:15

先来itext的例子

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 java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import java.awt.color.ColorSpace;

public class PdfToMultiImage {

    public static void main(String args[]) throws IOException {

        File sourceFile = new File("C:\\Users\\luez\\Documents\\Tencent Files\\279436877\\FileRecv\\4172\\日本リビング保証:検査申込書.pdf");
        File destinationFile = new File(sourceFile.getPath().replace(".pdf", ""));
        List<String> result = new ArrayList<String>();
        RandomAccessFile raf = new RandomAccessFile(sourceFile, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
                channel.size());
        PDFFile pdf = new PDFFile(buf);
        int pageNumber = 1;
        for (int i = 1; i <= pdf.getNumPages(); i++) {
              PDFPage page = pdf.getPage(i);
            int width = 1200;
            int height = 1400;
            Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
                    .getWidth(), (int) page.getBBox().getHeight());
            BufferedImage bufferedImage = new BufferedImage(width, height,
                    BufferedImage.TYPE_BYTE_GRAY);
            Image image = page.getImage(width, height, rect, null, true, true);
            Graphics2D bufImageGraphics = bufferedImage.createGraphics();
            bufImageGraphics.drawImage(image, 0, 0, null);
            String destpath=destinationFile + "" + i + ".png";

         //把生成图片放进集合
            result.add(destpath);
            File imageFile = new File(destpath);
            ImageIO.write(bufferedImage, "png", imageFile);
            pageNumber++;
        }
        System.err.println(result);
    }
}


下面是pdfbox的转换

public static List<String> convert2jpg(String filePath) throws Exception {
Properties properties=System.getProperties();
Object encoding = properties.get("file.encoding");
properties.put("file.encoding", "SHIFT-JIS");
List<String> result = new ArrayList<String>();
if(!filePath.substring(filePath.lastIndexOf(".") + 1).toLowerCase().equals("pdf")){
return result;
}

String destFormat = "jpg";
int resolution = 300;

File file = new File(filePath);
String destPath = null;
PDDocument doc = null;
try {
doc = PDDocument.load(file);
if (doc.isEncrypted()) {
// doc.decrypt("TU2016");
return result;
}
List<PDPage> pages = doc.getDocumentCatalog().getAllPages();
if (pages.size() == 1) {
PDPage page = pages.get(0);
// BufferedImage image = page.convertToImage();
BufferedImage image = page.convertToImage(BufferedImage.TYPE_BYTE_GRAY, resolution);
destPath  = filePath.substring(0,filePath.lastIndexOf("."))+"."+destFormat;
File destFile = new File(destPath);
ImageIO.write(image, destFormat, destFile);
result.add(destPath);
} else {
String format = "_%0"+String.valueOf(pages.size()).length()+"d";
for (int i = 0; i < pages.size(); i++) {
PDPage page = pages.get(i);
BufferedImage image = page.convertToImage(BufferedImage.TYPE_BYTE_GRAY,resolution);
destPath  = filePath.substring(0,filePath.lastIndexOf("."))+String.format(format,i+1)+"."+destFormat;
File destFile = new File(destPath);
ImageIO.write(image, destFormat, destFile);
result.add(destPath);
}
}
} finally {
if (doc != null) {
doc.close();
}
properties.put("file.encoding", encoding);
}
return result;
}