You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Javier García Sánchez (JIRA)" <ji...@apache.org> on 2013/07/30 18:53:48 UTC

[jira] [Created] (PDFBOX-1677) PDPage.convertToImage() throw java.lang.IndexOutOfBoundsException

Javier García Sánchez created PDFBOX-1677:
---------------------------------------------

             Summary: PDPage.convertToImage() throw java.lang.IndexOutOfBoundsException
                 Key: PDFBOX-1677
                 URL: https://issues.apache.org/jira/browse/PDFBOX-1677
             Project: PDFBox
          Issue Type: Bug
          Components: PDFReader, PDModel, Utilities
    Affects Versions: 1.8.2, 1.8.1
         Environment: JSF1.1+ PdfBox1.8.2+Tomcat (and Weblogic)
            Reporter: Javier García Sánchez


Sometimes, a random times, the method PDPage.convertToImage() throw an exception like java.lang.IndexOutOfBoundsException. 
I said sometimes, because is a ramdom process which i cannot understand.

This is my code:

public byte[] getImageOutputStreamFromPdfFile (byte[] inputPDF) throws IOException,Exception{
			logger.info("Utils.getImageOutputStreamFromPdfFile (byte[] inputPDF)");
			//resultado
			byte[] result=null;
			
			if (inputPDF!=null && inputPDF.length>0){
				long time = System.currentTimeMillis();
				
				InputStream isPDF = null;
				
				
				//imagen resultado
				ByteArrayOutputStream imageOutputStream = null;
				
				try{
					isPDF = new ByteArrayInputStream(inputPDF);
					imageOutputStream = new ByteArrayOutputStream();
					
					//pdf
					PDDocument document = PDDocument.load(isPDF, true);
					@SuppressWarnings("unchecked")
					List<PDPage> pages = document.getDocumentCatalog().getAllPages();
					List<BufferedImage> imagesList = new ArrayList<BufferedImage>(
							pages.size());
		
					PDPage pDPage = null;
					BufferedImage auxImage = null;
					// width y height final image 
					int maxWidth = 0;
					int totalHeight = 0;
					for (int i = 0; i < pages.size(); i++) {
						pDPage = pages.get(i);
		
						// get Image Page
						auxImage = pDPage.convertToImage();
						
						auxImage.flush();
						// maxWidth
						if (auxImage.getWidth() > maxWidth) {
							maxWidth = auxImage.getWidth();
						}
		
						// totalHeight
						totalHeight += auxImage.getHeight();
		
						// add image to the list
						imagesList.add(i, auxImage);
					}
		
					BufferedImage bImageFromConvert = new BufferedImage(maxWidth,
							totalHeight, BufferedImage.TYPE_INT_ARGB);
		
					Graphics g = bImageFromConvert.getGraphics();
		
					int auxHeightDraw = 0;
					for (BufferedImage img : imagesList) {
						g.drawImage(img, 0, auxHeightDraw, null);
						auxHeightDraw += img.getHeight();
					}
		
					imagesList.clear();
					
					ImageIO.write(bImageFromConvert, ConstantesProperties.EXTENSION_IMAGE, imageOutputStream);
					
										
					result=imageOutputStream.toByteArray();
					
									
					logger.info("Utils.getImageOutputStreamFromPdfFile --> OK: Generation time:"+(System.currentTimeMillis()-time));
					
				}catch (IOException e) {
					
					logger.error("Utils.getImageOutputStreamFromPdfFile-->"+Utils.stackTraceToString(e));
					result=null;
					throw e;
					
				}catch (Exception e) {
					
					logger.error("Utils.getImageOutputStreamFromPdfFile-->"+Utils.stackTraceToString(e));
					result=null;
					throw e;
					
				}finally{
					
					if (isPDF!=null){
						isPDF.close();
					}
					if (imageOutputStream!=null){
						imageOutputStream.close();
					}
					
				}//end finally
			}//end if
			return result;
		}//end getImageOutputStreamFromPdfFile

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira