title: Is there a way to convert a Binary file that contains multiple images inside to pdf
body: <p>I'm trying to convert a binary file that contains multiple images inside to a pdf doc using java, using itextpdf was the only solutions that I get the converted file in the correct format, but the issue here is that on the output it provide me only one image(the first one), and lost the other ones that are inside the binary file.</p>
  
  <p>I've already prove to use itextpdf in order to add the images in a document also some other solutions like this one : 
  <a href="https://www.mkyong.com/java/how-to-convert-array-of-bytes-into-file/" rel="nofollow noreferrer">https://www.mkyong.com/java/how-to-convert-array-of-bytes-into-file/</a>
  or<br>
  <a href="https://stackoverflow.com/questions/23747982/create-pdf-from-binary-data-in-java">create pdf from binary data in java</a></p>
  
  <p>As I understand the issue in my case is that I've read my binary file and store them on a byte[] and after I've pass the content of the file to a Vector, </p>
  
  <p>I've create a function that get as argument Vector and create a pdf with the images inside, the issue is that it insert only the first image on the pdf, because it can not separate inside the Vector the end of the first image and the start of the second image like in this case (JPEG image files begin with FF D8 and end with FF D9.) :</p>
  
  <p><a href="https://stackoverflow.com/questions/4550296/how-to-identify-contents-of-a-byte-is-a-jpeg">How to identify contents of a byte[] is a jpeg?</a></p>
  
  <pre><code>File imgFront = new File("C:/Users/binaryFile");
  byte[] fileContent;       
  
  Vector&lt;byte[]&gt; records = new Vector&lt;byte[]&gt;();
  
  try {
  
      fileContent = Files.readAllBytes(imgFront.toPath());
      records.add(fileContent);  // add the result on Vector&lt;byte[]&gt;
  
  } catch (IOException e1) {
      System.out.println( e1 );
  }
  </code></pre>
  
  <p>...</p>
  
  <pre><code> public static String ImageToPDF(Vector&lt;byte[]&gt; imageVector, String pathFile) {
          String FileoutputName = pathFile + ".pdf";
          Document document = null;
  
          try {
              FileOutputStream fos = new FileOutputStream(FileoutputName );
              PdfWriter writer = PdfWriter.getInstance(document, fos);
  
              writer.open();
              document.open();  
  
       //loop here the ImageVector in order to get one by one the images, 
       //but I get only the first one 
  
              for (byte[] img : imageVector) {
                  Image image = Image.getInstance(img);
  
                  image.scaleToFit(500, 500); //size
  
                  document.add(image);
              }
              document.close();
              writer.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return FileoutputName ;
  
      }
  </code></pre>
  
  <p>I expect that in the pdf to have all the images inside, not only one.</p>