pdf png 轉換超清

需求

將pdf拆分成多個圖片 且清晰

依賴


<dependency>
  <groupId>com.kenai.nbpwr</groupId>
  <artifactId>com-sun-pdfview</artifactId>
  <version>1.0.5-201003191900</version>
  <type>nbm</type>
</dependency>

具體代碼

package com.example.demo.utils;

import com.example.demo.pojo.PdfImg;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;

/**
 *  <p>Title : Pdf2Image </p>
 *  <p>Description : PDF轉換png圖片格式</p>
 *
 * @author huifer
 * @date 2019年1月2日
 */
public class Pdf2Image {

    private static final int BUFFER_SIZE = 2 * 1024;


    public static void main(String[] args) throws Exception {
        String read = "D:\\qfile\\科技查新報告(智慧排澇).pdf";
        ArrayList<File> strings = pdf2img(read);
        String newF = "E:\\mck\\PDF2\\fa\\" + read.split("\\\\")[read.split("\\\\").length - 1]
                + ".zip";
        PdfImg pdfImg = new PdfImg();
        pdfImg.setPdf(read);
        pdfImg.setZip(newF);
        System.out.println(pdfImg);

        FileOutputStream fos2 = new FileOutputStream(new File(newF));
        toZip(strings, fos2);
    }

    public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
        long start = System.currentTimeMillis();
        ZipOutputStream zos = null;
        byte[] buf = new byte[BUFFER_SIZE];
        try {
            zos = new ZipOutputStream(out);
            for (File srcFile : srcFiles) {
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                FileInputStream in = new FileInputStream(srcFile);
                while ((len = in.read(buf)) != -1) {
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                in.close();
            }
            long end = System.currentTimeMillis();
            System.out.println("壓縮完成,耗時:" + (end - start) + " ms");
            // 刪除壓縮列表中的文件
            for (int i = 0; i < srcFiles.size(); i++) {
                srcFiles.get(i).delete();
            }

        } catch (Exception e) {
            throw new RuntimeException("zip error from ZipUtils", e);
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static ArrayList<File> pdf2img(String read) {
        ArrayList<File> ps = new ArrayList<>();
        try {
            String filePath = read;
            String[] split = filePath.split("\\\\");
            File file = new File(filePath);
            RandomAccessFile raf;
            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);
            int num = pdffile.getNumPages();
            for (int i = 0; i < num; i++) {
                PDFPage page = pdffile.getPage(i);

                int width = (int) page.getBBox().getWidth();
                int height = (int) page.getBBox().getHeight();

                Rectangle rect = new Rectangle(0, 0, width, height);
                int rotation = page.getRotation();
                Rectangle rect1 = rect;
                if (rotation == 90 || rotation == 270) {
                    rect1 = new Rectangle(0, 0, rect.height, rect.width);
                }

                BufferedImage img = (BufferedImage) page.getImage(
                        rect.width *2, rect.height*2,
                        rect1,
                        null,
                        true,
                        true
                );

                File file1 = new File(
                        "E:\\mck\\PDF2\\fa\\" + split[split.length - 1] + "-" + i + ".png");
                ps.add(file1);
                ImageIO.write(img, "png", file1);
            }
        } catch (FileNotFoundException e1) {
            System.err.println(e1.getLocalizedMessage());
        } catch (IOException e) {
            System.err.println(e.getLocalizedMessage());
        }
        return ps;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章