使用 Itext 从 pdf 检索图像时出错
Error while retrieving images from pdf using Itext
我有一个现有的 PDF
,我想从中检索图像
注意:
在文档中,这是 RESULT
变量
public static final String RESULT = "results/part4/chapter15/Img%s.%s";
我不明白为什么需要这张图片?我只想从我的 PDF
文件中提取图片
所以现在当我使用 MyImageRenderListener listener = new MyImageRenderListener(RESULT);
我收到错误:
results\part4\chapter15\Img16.jpg (The system
cannot find the path specified)
这是我拥有的代码。
package part4.chapter15;
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 {
/** The new document to which we've added a border rectangle. */
public static final String RESOURCE = "resources/pdfs/samplefile.pdf";
public static final String RESULT = "results/part4/chapter15/Img%s.%s";
/**
* Parses a PDF and extracts all the images.
* @param src the source PDF
* @param dest the resulting PDF
*/
public void extractImages(String filename)
throws IOException, DocumentException {
PdfReader reader = new PdfReader(filename);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
MyImageRenderListener listener = new MyImageRenderListener(RESULT);
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 {
new ExtractImages().extractImages(RESOURCE);
}
}
你有两个问题,第一个问题的答案是第二个问题答案的关键。
问题 1:
您指的是:
public static final String RESULT = "results/part4/chapter15/Img%s.%s";
然后你问:为什么需要这张图片?
这个问题是错误的,因为Img%s.%s
不是图像的文件名,而是图像文件名的模式。解析时,iText 将检测 PDF 中的图像。这些图像存储在编号的对象(例如对象 16)中,并且可以以不同的格式(例如 jpg、png、...)导出这些图像。
假设图像存储在对象 16 中并且该图像是 jpg,则模式将解析为 Img16.jpg
。
问题二:
为什么会出现错误:
results\part4\chapter15\Img16.jpg (The system cannot find the path specified)
在您的 PDF 中,对象 16 中存储了一个 jpg。您要求 iText 使用此路径存储该图像:results\part4\chapter15\Img16.jpg
(如我对 问题 1[= 的回答中所述) 83=]).但是:您的工作目录没有子目录 results\part4\chapter15\
,因此会抛出 IOException
(或 FileNotFoundException
?)。
一般问题是什么?
你有 copy/pasted 我为我的书 "iText in Action - Second Edition" 写的 ExtractImages 例子,但是:
- 您没有读过那本书,所以您不知道该代码应该做什么。
- 您并没有告诉 Whosebug 上的读者这个示例依赖于 MyImageRenderer class,这是所有魔法发生的地方。
如何解决你的问题?
选项 1:
像这样更改 RESULT
:
public static final String RESULT = "Img%s.%s";
现在图像将存储在您的工作目录中。
选项 2:
改编MyImageRendererclass,更具体地说是这个方法:
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());
os = new FileOutputStream(filename);
os.write(image.getImageAsBytes());
os.flush();
os.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
iText 在遇到图像时调用此 class。它向此方法传递了一个 ImageRenderInfo
,其中包含有关该图像的大量信息。
在此实现中,我们将图像字节存储为文件。这就是我们创建该文件路径的方式:
String.format(path,
renderInfo.getRef().getNumber(), image.getFileType())
如您所见,存储在 RESULT
中的模式的使用方式是,第一次出现的 %s
被替换为数字,第二次出现时被替换为文件扩展名。
您可以轻松调整此方法,以便将图像存储为 byte[]
在 List
中(如果您需要的话)。
我有一个现有的 PDF
,我想从中检索图像
注意:
在文档中,这是 RESULT
变量
public static final String RESULT = "results/part4/chapter15/Img%s.%s";
我不明白为什么需要这张图片?我只想从我的 PDF
文件中提取图片
所以现在当我使用 MyImageRenderListener listener = new MyImageRenderListener(RESULT);
我收到错误:
results\part4\chapter15\Img16.jpg (The system cannot find the path specified)
这是我拥有的代码。
package part4.chapter15;
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 {
/** The new document to which we've added a border rectangle. */
public static final String RESOURCE = "resources/pdfs/samplefile.pdf";
public static final String RESULT = "results/part4/chapter15/Img%s.%s";
/**
* Parses a PDF and extracts all the images.
* @param src the source PDF
* @param dest the resulting PDF
*/
public void extractImages(String filename)
throws IOException, DocumentException {
PdfReader reader = new PdfReader(filename);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
MyImageRenderListener listener = new MyImageRenderListener(RESULT);
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 {
new ExtractImages().extractImages(RESOURCE);
}
}
你有两个问题,第一个问题的答案是第二个问题答案的关键。
问题 1:
您指的是:
public static final String RESULT = "results/part4/chapter15/Img%s.%s";
然后你问:为什么需要这张图片?
这个问题是错误的,因为Img%s.%s
不是图像的文件名,而是图像文件名的模式。解析时,iText 将检测 PDF 中的图像。这些图像存储在编号的对象(例如对象 16)中,并且可以以不同的格式(例如 jpg、png、...)导出这些图像。
假设图像存储在对象 16 中并且该图像是 jpg,则模式将解析为 Img16.jpg
。
问题二:
为什么会出现错误:
results\part4\chapter15\Img16.jpg (The system cannot find the path specified)
在您的 PDF 中,对象 16 中存储了一个 jpg。您要求 iText 使用此路径存储该图像:results\part4\chapter15\Img16.jpg
(如我对 问题 1[= 的回答中所述) 83=]).但是:您的工作目录没有子目录 results\part4\chapter15\
,因此会抛出 IOException
(或 FileNotFoundException
?)。
一般问题是什么?
你有 copy/pasted 我为我的书 "iText in Action - Second Edition" 写的 ExtractImages 例子,但是:
- 您没有读过那本书,所以您不知道该代码应该做什么。
- 您并没有告诉 Whosebug 上的读者这个示例依赖于 MyImageRenderer class,这是所有魔法发生的地方。
如何解决你的问题?
选项 1:
像这样更改 RESULT
:
public static final String RESULT = "Img%s.%s";
现在图像将存储在您的工作目录中。
选项 2:
改编MyImageRendererclass,更具体地说是这个方法:
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());
os = new FileOutputStream(filename);
os.write(image.getImageAsBytes());
os.flush();
os.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
iText 在遇到图像时调用此 class。它向此方法传递了一个 ImageRenderInfo
,其中包含有关该图像的大量信息。
在此实现中,我们将图像字节存储为文件。这就是我们创建该文件路径的方式:
String.format(path,
renderInfo.getRef().getNumber(), image.getFileType())
如您所见,存储在 RESULT
中的模式的使用方式是,第一次出现的 %s
被替换为数字,第二次出现时被替换为文件扩展名。
您可以轻松调整此方法,以便将图像存储为 byte[]
在 List
中(如果您需要的话)。