|
时 间 记 忆 |
<< < 2018 - 3 > >>
日 |
一 |
二 |
三 |
四 |
五 |
六 |
|
|
|
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
|
|
|
|
|
|
Java PDF转换JPG |
[ 2017-3-12 21:33:00 | By: 我家超超会发光 ] |
package com.hljw.cmeav.util;
import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; 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 java.util.List;
import javax.imageio.ImageIO;
import org.icepdf.core.pobjects.Document; import org.icepdf.core.pobjects.Page; import org.icepdf.core.util.GraphicsRenderingHints;
import com.google.common.collect.Lists; 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 PdfToJpgUtils { public static void toJpg(String pdfFile) { try { Document *** = new Document(); ***.setFile(pdfFile); float scale = 2f; float rotation = 0f; File file = new File(pdfFile); String dirJpg = file.getName().toLowerCase().replace(".pdf", ""); String jpgFilePath = file.getParentFile().getAbsolutePath() + File.separator + dirJpg; File jpgdir = new File(jpgFilePath); if (!jpgdir.exists()) { jpgdir.mkdirs(); } for (int i = 0; i < ***.getNumberOfPages(); i++) { BufferedImage image = (BufferedImage) ***.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, scale); RenderedImage rendImage = image; File jpgFile = new File(jpgFilePath + File.separator + i + ".jpg"); if (jpgFile.exists()) { jpgFile.delete(); } ImageIO.write(rendImage, "jpg", jpgFile); image.flush(); } ***.dispose(); } catch (Exception e) { e.printStackTrace(); } } public static List<String> pdfToJpg(String fileName) throws IOException { List<String> list = Lists.newArrayList(); int index = fileName.lastIndexOf("."); String startFile = fileName.substring(0,index); File file = new File(fileName); 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); for (int i = 1; i <= pdffile.getNumPages(); i++) { PDFPage page = pdffile.getPage(i); Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight()); 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(startFile + ".jpg"); // 输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); // JPEG编码 out.close(); list.add(startFile + ".jpg"); } return list; } public static void main(String[] args) throws IOException { pdfToJpg("D:/64bitTomcat/apache-tomcat-7.0.55/webapps/renqiuyiyuan/upload/item_result_images/img/4028803b55585c0f0155587ea7920008.pdf"); } } |
|
|
|
|