目前我正在尝试在java中打印一些pdf。使用的api是2.0版本中的Apache PDF Box。转换图像后,我将它们写入磁盘以节省内存。在下一步中,我再次阅读它们并在图像的头部写下标题。之后我又写了一遍。为了使用它们,我再次分开阅读它们。当我最后一次阅读它时,每200-300张图像会出现以下异常:
java.lang.IndexOutOfBoundsException: off < 0 || len < 0 || off+len > b.length || off+len < 0!
at javax.imageio.stream.MemoryCacheImageInputStream.read(MemoryCacheImageInputStream.java:100)
at com.sun.imageio.plugins.common.SubImageInputStream.read(SubImageInputStream.java:61)
at com.sun.imageio.plugins.common.InputStreamAdapter.read(InputStreamAdapter.java:47)
at java.io.SequenceInputStream.read(SequenceInputStream.java:207)
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:238)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1104)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1215)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1330)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1606)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1400)
at my.code.Class.method()
我使用以下代码转换PDF:
final HashMap<Integer, File> images = new HashMap<>();
PDDocument document = PDDocument.load(sourceFile);
PDFRenderer pdfRenderer = new PDFRenderer(document);
final ExecutorService service = Executors.newFixedThreadPool(4);
for (int page = 0; page < document.getNumberOfPages(); ++page)
{
final int finalPageNumber = page;
Runnable r = () -> {
try
{
//Java could only print with 72 dpi so I'll use it
BufferedImage image = pdfRenderer.renderImageWithDPI(finalPageNumber, 72);
File imageFile = new File(sourceFile.getAbsolutePath() + "-" + (finalPageNumber + 1) + ".png");
ImageIO.write(image, "png", imageFile);
image.flush();
images.put(finalPageNumber, imageFile);
}
catch (final IOException e)
{
e.printStackTrace();
}
};
Thread t = new Thread(r, page + "");
t.setName("" + page);
service.submit(t);
}
阅读时使用以下代码:
// example url: /tmp/example.pdf-17.png
Image i = ImageIO.read(url);
如何解决这个问题
提前致谢
Daniel Brenzel
修改
我忘了说我在图片上添加了一个标题:
BufferedImage bimage = new BufferedImage( simple.getValue().getWidth(null),
simple.getValue().getHeight(null) + 50,
BufferedImage.TYPE_INT_ARGB);
Graphics bGr = bimage.createGraphics();
bGr.setColor(Color.WHITE);
bGr.fillRect(0, 0, bimage.getWidth(), bimage.getHeight());
bGr.setFont(new Font("Arial", Font.PLAIN, 15));
bGr.setColor(Color.BLACK);
bGr.drawImage(simple.getValue(), 0, 50, null);
bGr.drawString(entry.getValue(), 20, 20);
try
{
ImageIO.write(bimage, "PNG", new File(simple.getKey().toURI()));
}
catch (IOException | URISyntaxException e)
{
e.printStackTrace();
}
bGr.dispose();
以下是文件的链接(可能会出现在每个页面上):
答案 0 :(得分:0)
我已经解决了我的问题。我没有等待Image的写作。现在我等待ExecutorService的完成,一切正常。
我认为释放资源存在问题,因为我已经在MemoryCacheImageInputStream第100行看到,抛出异常时每件事情都很好。