Speed up page thumb generation

Hi,

I have an Android application that needs to generate thumbnails of every page in a lot of pdf-files.
It all works perfectly, except that it is too slow. It takes about 600-700ms per page, which means a pdf document of 140 page takes 1 minute and 38 seconds to generate pdf’s, which is too slow.
Is there anything I can do to improve the performance of my code ?

`

final float maxDimension = 320;

PDFDoc pdfDoc = null;
PDFDraw pdfDraw = null;
try
{

//encoder parameters!
ObjSet objSet = new ObjSet();
Obj encoder_param = objSet.createDict();
encoder_param.putNumber(“Quality”, sQuality);

Log.d(getClass().getSimpleName(), "Preparing file " + pdfPath );

pdfDoc = new PDFDoc(pdfPath);
pdfDraw = new PDFDraw();
pdfDraw.setDrawAnnotations(false);
pdfDraw.setImageSmoothing(false);
pdfDraw.setCaching(false);
int pageCount = pdfDoc.getPageCount();

File thumbFile;
Page page = null;
for(int pageNr = 1 ; pageNr <= pageCount; pageNr++)
{
//create thumbnail path
thumbFile = new File(getThumbnailPath(pdfPathWithoutExt, pageNr));
Log.d(“PdfPreparer”, "Preparing page " + pageNr + " in file : "+ thumbFile.toString());

if (thumbFile.exists())
{
//the thumbnail already
continue;
}

long start = System.currentTimeMillis();

try
{
//get the page to render it to a bitmap
page = pdfDoc.getPage(pageNr);

float w = (float) page.getPageWidth();
float h = (float) page.getPageHeight();

if (w > h && w > 0)
{
h = h * ((float)maxDimension / w);
w = maxDimension;
}
else if (h > 0)
{
w = w * ((float)maxDimension / h);
h = maxDimension;
}

pdfDraw.setImageSize((int)w, (int)h);
pdfDraw.export(page, thumbFile.getPath(), “PNG8”, encoder_param);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if (page != null) { page = null; }
}
Log.d(“PDF”, “Decoging page took: " + (System.currentTimeMillis() - start) + " milliseconds”);
}
}
catch(Exception e)
{
Log.e(“PdfPreparer”,e.getMessage(), e);
}
finally
{
if (pdfDoc != null)
{
try{
pdfDoc.close();
}catch (PDFNetException e){
Log.e(“PdfPreparer”,e.getMessage(), e);
}
}
if (pdfDraw != null)
{
try{
pdfDraw.destroy();
}catch (PDFNetException e){
Log.e(“PdfPreparer”,e.getMessage(), e);
}
}
}

`

Hi Jelle,

Have you found a solution to this? If yes could you share what you did?
Le lundi 17 juin 2013 09:08:07 UTC+3, Jelle Nagels a écrit :

Hi,

I have an Android application that needs to generate thumbnails of every page in a lot of pdf-files.
It all works perfectly, except that it is too slow. It takes about 600-700ms per page, which means a pdf document of 140 page takes 1 minute and 38 seconds to generate pdf's, which is too slow.
Is there anything I can do to improve the performance of my code ?

final float maxDimension = 320;
  
PDFDoc pdfDoc = null;
PDFDraw pdfDraw = null;
try
{
  
  //encoder parameters!
  ObjSet objSet = new ObjSet();
  Obj encoder_param = objSet.createDict();
  encoder_param.putNumber("Quality", sQuality);
  
  Log.d(getClass().getSimpleName(), "Preparing file " + pdfPath );
  
  pdfDoc = new PDFDoc(pdfPath);
  pdfDraw = new PDFDraw();
  pdfDraw.setDrawAnnotations(false);
  pdfDraw.setImageSmoothing(false);
  pdfDraw.setCaching(false);
  int pageCount = pdfDoc.getPageCount();
  
  File thumbFile;
  Page page = null;
  for(int pageNr = 1 ; pageNr <= pageCount; pageNr++)
  {
    //create thumbnail path
    thumbFile = new File(getThumbnailPath(pdfPathWithoutExt, pageNr));
    Log.d("PdfPreparer", "Preparing page " + pageNr + " in file : "+ thumbFile.toString());
    
    if (thumbFile.exists())
    {
      //the thumbnail already
      continue;
    }
    
    long start = System.currentTimeMillis();
    
    try
    {
      //get the page to render it to a bitmap
      page = pdfDoc.getPage(pageNr);
      
      float w = (float) page.getPageWidth();
      float h = (float) page.getPageHeight();

      if (w > h && w > 0)
      {
        h = h * ((float)maxDimension / w);
        w = maxDimension;
      }
      else if (h > 0)
      {
        w = w * ((float)maxDimension / h);
        h = maxDimension;
      }
      
      pdfDraw.setImageSize((int)w, (int)h);
      pdfDraw.export(page, thumbFile.getPath(), "PNG8", encoder_param);
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      if (page != null) { page = null; }
    }
    Log.d("PDF", "Decoging page took: " + (System.currentTimeMillis() - start) + " milliseconds");
  }
}
catch(Exception e)
{
  Log.e("PdfPreparer",e.getMessage(), e);
}
finally
{
  if (pdfDoc != null)
  {
    try{
      pdfDoc.close();
    }catch (PDFNetException e){
      Log.e("PdfPreparer",e.getMessage(), e);
    }
  }
  if (pdfDraw != null)
  {
    try{
      pdfDraw.destroy();
    }catch (PDFNetException e){
      Log.e("PdfPreparer",e.getMessage(), e);
    }
  }
}