title: For Loop is slow - Java
body: <p>I basically want to load a pdf using Apache pdfbox and convert it into a List of base64 for each page.</p>
  
  <p>I tried the following code but it's extremely slow. I dont need to convert into an image, I just want to convert into base64 to pass to the frontend</p>
  
  <pre><code>PDDocument document = PDDocument.loadNonSeq(new File("Random.pdf"), null);
  @SuppressWarnings("unchecked")
  List&lt;PDPage&gt; pdPages = document.getDocumentCatalog().getAllPages();
  int page = 0;
  List&lt;String&gt; base64DocumentPages = new ArrayList&lt;&gt;();
  for (PDPage pdPage : pdPages)
  { 
      ++page;           
      BufferedImage img = pdPage.convertToImage(BufferedImage.TYPE_INT_RGB, 300); // this is slow
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      ImageIOUtil.writeImage(img, ".png", os);
      String base64Page = Base64.getEncoder().encodeToString(os.toByteArray());
      base64DocumentPages.add(URLEncoder.encode(base64Page, "UTF-8"));
  }
  document.close();
  </code></pre>
  
  <p>I'm using PDFBOX to loop the pages but I can use anything if you know anything better.</p>
  
  <p>PS: I really need the Base64 data of the pages separated in some sort of array</p>