1
此刻我正嘗試在java中打印一些pdf。使用的api是2.0版本中的Apache PDF Box。轉換圖像後,我將它們寫入磁盤以節省內存。在接下來的步驟中,我再次閱讀它們並在圖像頭部寫入標題。之後我再寫一遍。爲了使用它們,我再次分開閱讀它們。當我看到他們最後一次我得到以下異常每200-300圖片:從磁盤讀取圖像時出現ArrayOutOfBoundsException
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);
怎麼樣來解決這個問題
在此先感謝
丹尼爾Brenzel主任
編輯
什麼我忘了說我manualy添加標題的圖片:
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();
這裏是鏈接到文件(可能與每個頁面一起):
Image wich throw the exception
PDF file wich is used (generated by Word2007)
您可以附加導致此異常的文件之一,還是隨機發生? – haraldK
@haraldK我認爲這是隨機發生的,因爲我已經用多個來源的多個文件進行了嘗試,但我會附上一些示例文件。 – finder2
@RobertoDeLaParra有沒有什麼不同?該值在此位置不被使用 – finder2