用PDFBox將PDF轉圖片,將PDF每個頁面各自轉成其對應圖片
用到了一下三個庫
pdfbox-1.8.10.jar
commons-logging-1.2.jar
fontbox-1.8.10.jar
下面是例子
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public class pdfimg2 {
public static void main(String[] args) throws IOException {
String filename = args[0];
String savefile = args[1];
PDDocument doc = PDDocument.load(filename);
List pages = doc.getDocumentCatalog().getAllPages();
Iterator iter = pages.iterator();
int i = 1;
while (iter.hasNext()) {
PDPage page = (PDPage) iter.next();
// BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_ARGB, 2 * 72); //支持透明背景
BufferedImage image = page.convertToImage(); //默認白色背景
File outFile = new File(savefile + i + ".png");
ImageIO.write(image, "png", outFile);
i++;
}
doc.close();
}
}
編譯
javac -cp .;pdfbox-1.8.10.jar;commons-logging-1.2.jar;fontbox-1.8.10.jar pdfimg2.java -encoding utf-8
執行
java -cp .;pdfbox-1.8.10.jar;commons-logging-1.2.jar;fontbox-1.8.10.jar pdfimg2 "test.pdf" "p"
完