title: PDF to byte array and vice versa
body: <p>I need to convert pdf to byte array and vice versa.</p>
  
  <p>Can any one help me?</p>
  
  <p>This is how I am converting to byte array</p>
  
  <pre><code>public static byte[] convertDocToByteArray(String sourcePath) {
  
      byte[] byteArray=null;
      try {
          InputStream inputStream = new FileInputStream(sourcePath);
  
  
          String inputStreamToString = inputStream.toString();
          byteArray = inputStreamToString.getBytes();
  
          inputStream.close();
      } catch (FileNotFoundException e) {
          System.out.println("File Not found"+e);
      } catch (IOException e) {
                  System.out.println("IO Ex"+e);
      }
      return byteArray;
  }
  </code></pre>
  
  <p>If I use following code to convert it back to document, pdf is getting created. But it's saying <code>'Bad Format. Not a pdf'</code>.</p>
  
  <pre><code>public static void convertByteArrayToDoc(byte[] b) {          
  
      OutputStream out;
      try {       
          out = new FileOutputStream("D:/ABC_XYZ/1.pdf");
          out.close();
          System.out.println("write success");
      }catch (Exception e) {
          System.out.println(e);
      }
  </code></pre>