私のコードは、すべてのPDFファイルから画像を抽出していません。それはいくつかのPDFファイルのために良い作品です。私を助けてください。 ExtractImages.java私のコードはすべてのPDFファイルから画像を抽出していません
package pdttotextconvertor;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
/**
* Extracts images from a PDF file.
*/
public class ExtractImages {
/**
* PDF to extract images from
*/
public static final String SOURCE_PDF = "C:/Latest Maven Code/pdttotextconvertor/src/main/resources/LC DPF example 1.pdf";
/**
* Parses a PDF and extracts all the images.
*
* @param filename the source PDF
* @param destination the directory to save images
*/
public void extractImages(String filename, String destination)
throws IOException, DocumentException {
System.out.println("Processing PDF at " + filename);
System.out.println("Saving images to " + destination);
PdfReader reader = new PdfReader(filename);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
ImageRenderListener listener = new ImageRenderListener(destination + "/Img%s.%s");
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
parser.processContent(i, listener);
}
reader.close();
}
/**
* Main method.
*
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args) throws IOException, DocumentException {
String sourcePDF = SOURCE_PDF;
String destination = "target/images";
if (args.length > 0) {
sourcePDF = args[0];
if (args.length > 1) {
destination = args[1];
}
}
new ExtractImages().extractImages(sourcePDF, destination);
}
}
ImageRenderListener.java http://www.filedropper.com/lcdpfexample23
は、事前に感謝を助けてくださいで動作していない
package scannedPdfConvertor;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.PdfImageObject;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Saves images to a directory.
*
* @author mnguyen
*/
public class ImageRenderListener implements RenderListener {
/**
* The directory path to store images.
*/
protected String path;
/**
* Creates a RenderListener that will look for images.
*/
public ImageRenderListener(String path) {
this.path = path;
}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock()
*/
public void beginTextBlock() {
}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock()
*/
public void endTextBlock() {
}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
* com.itextpdf.text.pdf.parser.ImageRenderInfo)
*/
public void renderImage(ImageRenderInfo renderInfo) {
try {
String filename;
FileOutputStream os;
PdfImageObject image = renderInfo.getImage();
if (image == null) {
return;
}
filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType());
System.out.println("Writing image to file: " + filename);
os = new FileOutputStream(filename);
os.write(image.getImageAsBytes());
os.flush();
os.close();
} catch (IOException e) {
Logger.getLogger(ImageRenderListener.class.getName()).log(Level.SEVERE, null, e);
}
}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#renderText(
* com.itextpdf.text.pdf.parser.TextRenderInfo)
*/
public void renderText(TextRenderInfo renderInfo) {
}
}
のPDFファイルへのリンク: はここに私のコードです。
あなたのpdfファイルを共有してください。あなたの質問がiTextについてのものであれば、pdfboxで質問にタグを付けないでください。 –
あなたが助けを求めているので、「私のコードは時々動作しません」とコメントを外したコードを投稿するだけでいいわけではありません。あなたの問題がどこで発生するのか、あなたは何か考えていますか?あなたのログには情報が含まれていますか?あなたのコードはエラーなく完了しますか?など –
私の推測では、OPはラスターイメージのためにベクトルイメージを間違えるか(彼のコードは* Image XObjects *のみを抽出する)、iTextでサポートされていないイメージに遭遇する(私はわからないが、JBIG2サポートされていません; JBIG2をiTextでPDFに変換することはできますが、私はそこに道があるとは思わない)。 –