I extracted a tiff image from scanned pdf (that is the every page as image) in the following way:
...
PdfReader reader = new PdfReader("source.pdf");
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
MyImageRenderListener listener = new MyImageRenderListener("destination.jpg");
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
parser.processContent(i, listener);
}
...
code of MyImageRenderListener.class:
class MyImageRenderListener implements RenderListener {
protected String path = "";
public MyImageRenderListener(String path) {
this.path = path;
}
public void beginTextBlock() {
}
public void endTextBlock() {
}
public void renderImage(ImageRenderInfo renderInfo) {
try {
String filename;
FileOutputStream os;
PdfImageObject image = renderInfo.getImage();
PdfName filter = (PdfName) image.get(PdfName.FILTER);
if (PdfName.CCITTFAXDECODE.equals(filter)) {
BufferedImage bufferedImage = image.getBufferedImage();
ImageIO.write(bufferedImage, "jpg", new FileOutputStream(filename));// save tif image as jpg
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void renderText(TextRenderInfo renderInfo) {
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…