Java怎么把PDF转化成图片
5个回答
2019-06-24
展开全部
用Spire.Pdf for Java api可以把PDF转成图片,也可以把图片转成PDF,或者是PDF转到其他格式。下面是转图片的一个例子:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.spire.pdf.PdfDocument;
public class PDFToImage {
public static void main(String[] args) throws IOException {
//加载PDF文件
PdfDocument doc = new PdfDocument();
doc.loadFromFile("ToImage.pdf");
//保存PDF的每一页到图片
BufferedImage image;
for (int i = 0; i < doc.getPages().getCount(); i++) {
image = doc.saveAsImage(i);
File file = new File( String.format("ToImage-img-%d.png", i));
ImageIO.write(image, "PNG", file);
}
doc.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class PdfToJpgTest {
public static void setup() throws IOException {
File file = new File("c:\\4.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);
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,
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:/pdfImg.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 {
PdfToJpgTest.setup();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}
public static void setup() throws IOException {
File file = new File("c:\\4.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);
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,
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:/pdfImg.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 {
PdfToJpgTest.setup();
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
啊我不会用Java诶,我用的都是"sanpdf",因为它都是只有三个步骤,就是你下载后,首页选“PDF转图片”,然后添加文件,点击开始转换就可以啦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
怎么把PDF转成图片
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询