title: Java : PDF page preview error, expected xref
body: <p>I am trying to create a preview for PDF files which are created by Ballasamic Mockups. Around 50% of the time, I am not getting a preview and getting xref missing error. What am I doing wrong?</p>
  
  <p>Error log :</p>
  
  <pre><code>com.sun.pdfview.PDFParseException: Expected 'xref' at start of table
  at com.sun.pdfview.PDFFile.readTrailer(PDFFile.java:974)
  at com.sun.pdfview.PDFFile.parseFile(PDFFile.java:1175)
  at com.sun.pdfview.PDFFile.&lt;init&gt;(PDFFile.java:126)
  at com.sun.pdfview.PDFFile.&lt;init&gt;(PDFFile.java:102)
  </code></pre>
  
  <p>Code :</p>
  
  <pre><code>private byte[] onlyCreatePdfPreview(String path, int attachId) {
      try {
          File file = new File(path);
          RandomAccessFile raf = new RandomAccessFile(file, "r");
          FileChannel channel = raf.getChannel();
          ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
  
          PDFFile pdffile = new com.sun.pdfview.PDFFile(buf);
          PDFPage page = pdffile.getPage(0);
          Rectangle rect = new Rectangle(0, 0,
              (int) page.getBBox().getWidth(),
              (int) page.getBBox().getHeight());
          java.awt.Image img = page.getImage(
              rect.width, rect.height, //width &amp; height
              rect, // clip rect
              null, // null for the ImageObserver
              true, // fill background with white
              true  // block until drawing is done
          );
  
          BufferedImage buffered = toBufferedImage(img);
          buffered = Scalr.resize(buffered, Scalr.Method.ULTRA_QUALITY, 400, 250);
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          ImageIO.write(buffered, "png", baos);
          baos.flush();
          return baos.toByteArray();
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>What am I doing wrong? Thank you. </p>
  
  <p><em>Final Working code</em></p>
  
  <pre><code>      try {
              String pdfPath = zipLocation + String.valueOf(new BigInteger(130, random).toString(32));
              PdfReader reader = new PdfReader(path);
              PdfStamper pdfStamper = new PdfStamper(reader,new FileOutputStream(pdfPath));
              pdfStamper.getWriter().setPdfVersion(PdfWriter.PDF_VERSION_1_4);
              pdfStamper.close();
              reader.close();
       RandomAccessFile raf = new RandomAccessFile(pdfPath, "r");
              FileChannel channel = raf.getChannel();
              ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
               PDFFile pdffile = new com.sun.pdfview.PDFFile(buf);
                  PDFPage page = pdffile.getPage(0);
                  Rectangle rect = new Rectangle(0, 0,
                          (int) page.getBBox().getWidth(),
                          (int) page.getBBox().getHeight());
                  java.awt.Image img = page.getImage(
                          rect.width, rect.height, //width &amp; height
                          rect, // clip rect
                          null, // null for the ImageObserver
                          true, // fill background with white
                          true  // block until drawing is done
                  );
  
                  BufferedImage buffered = toBufferedImage(img);
                  buffered = Scalr.resize(buffered, Scalr.Method.ULTRA_QUALITY, 400, 250);
              ByteArrayOutputStream  baos = new ByteArrayOutputStream();
                  ImageIO.write(buffered, "png", baos);
                  baos.flush();
           return baos.toByteArray();
  }//catch block
  </code></pre>