title: Removing a blank page from a pdf with itextpdf corrupts my file
body: <p>I am basing my code from this
  <a href="https://github.com/Betel-Flowers/BetelFlowers/blob/master/BetelFlowers-ejb/src/main/java/com/betel/flowers/pdf/util/RemoveBlankPageFromPDF.java" rel="nofollow noreferrer">https://github.com/Betel-Flowers/BetelFlowers/blob/master/BetelFlowers-ejb/src/main/java/com/betel/flowers/pdf/util/RemoveBlankPageFromPDF.java</a>
  or this
  <a href="http://www.rgagnon.com/javadetails/java-detect-and-remove-blank-page-in-pdf.html" rel="nofollow noreferrer">http://www.rgagnon.com/javadetails/java-detect-and-remove-blank-page-in-pdf.html</a></p>
  
  <p>I am trying to use a byte array as input a byte array as output.</p>
  
  <p>This is my code</p>
  
  <pre><code>import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  
  import com.lowagie.text.Document;
  import com.lowagie.text.DocumentException;
  import com.lowagie.text.pdf.PdfCopy;
  import com.lowagie.text.pdf.PdfDictionary;
  import com.lowagie.text.pdf.PdfImportedPage;
  import com.lowagie.text.pdf.PdfName;
  import com.lowagie.text.pdf.PdfReader;
  import com.lowagie.text.pdf.RandomAccessFileOrArray;
  
  public class RemoveBlankPageFromPDF {
  
      // value where we can consider that this is a blank image
      // can be much higher or lower depending of what is considered as a blank page
      public static final int BLANK_THRESHOLD = 160;
  
      public static byte[] removeBlankPdfPages(byte[] fuente) throws IOException, DocumentException{
          PdfReader r = null;
          RandomAccessFileOrArray raf = null;
          Document document = null;
          PdfCopy writer = null;
          ByteArrayOutputStream archivoFinal = new ByteArrayOutputStream();
  
          try {
              r = new PdfReader(fuente);
  
              raf = new RandomAccessFileOrArray(fuente);
              document = new Document(r.getPageSizeWithRotation(1));
              writer = new PdfCopy(document,archivoFinal);
              document.open();
              PdfImportedPage page = null;
  
              for (int i = 1; i &lt;= r.getNumberOfPages(); i++) {
                  PdfDictionary pageDict = r.getPageN(i);
                  PdfDictionary resDict = (PdfDictionary) pageDict.get(PdfName.RESOURCES);
                  boolean noFontsOrImages = true;
                  if (resDict != null) {
                      noFontsOrImages = resDict.get(PdfName.FONT) == null
                              &amp;&amp; resDict.get(PdfName.XOBJECT) == null;
                  }
  
                  if (!noFontsOrImages) {
                      byte bContent[] = r.getPageContent(i, raf);
                      ByteArrayOutputStream bs = new ByteArrayOutputStream();
                      bs.write(bContent);
                      System.out.println("bs size: " + bs.size());
                      if (bs.size() &gt; BLANK_THRESHOLD) {
                          page = writer.getImportedPage(r, i);
                          writer.addPage(page);
                      }
                  }
              }
              System.out.println("Original: " + fuente.length+ " new: " + archivoFinal.toByteArray().length);
              return archivoFinal.toByteArray();
          } finally {
              if (document != null) {
                  document.close();
              }
              if (writer != null) {
                  writer.close();
              }
              if (raf != null) {
                  raf.close();
              }
              if (r != null) {
                  r.close();
              }
          }
      }
  }
  </code></pre>
  
  <p>my pdf gets corrupted i cannot open it after.</p>
  
  <p>Even with a pdf without spaces I get different sizes, it should be the same</p>
  
  <pre><code>Original: 95089 New: 88129
  </code></pre>
  
  <p>That is my output from my las sysout.</p>
  
  <p>I am using itext 2.1.5 and java 1.5 by the way. I cannot upgrade.</p>