title: How do I copy a pdf into another without losing page sizes and meta information?
body: <p>My goal is to take out the second page of a pdf and put the contents of another pdf inside of it. </p>
  
  <p>I have taken a look at other answers and I have written the code to delete the template page(page 2.) However, when I copy the other pages into a new book with the first set I get all sorts of insane cropping. It doesn't come out right. I tried to use the setpagesize option but it still doesn't seem like it is working.</p>
  
  <p>Am I going about this in the wrong fashion?</p>
  
  <pre><code>package com.protocase.itexthelper;
  
  import com.itextpdf.text.Document;
  import com.itextpdf.text.DocumentException;
  import com.itextpdf.text.pdf.PdfContentByte;
  import com.itextpdf.text.pdf.PdfImportedPage;
  import com.itextpdf.text.pdf.PdfReader;
  import com.itextpdf.text.pdf.PdfStamper;
  import com.itextpdf.text.pdf.PdfWriter;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  /**
   *
   * @author davidh
   */
  public class ITextHelper {
  
      public static void deletePage(String fileToPrune, int pageToDelete, String newFilePath) {
  
          PdfReader pdfReader = null;
          try {
              pdfReader = new PdfReader(fileToPrune);
          } catch (IOException ex) {
              Logger.getLogger(ITextHelper.class.getName()).log(Level.SEVERE, null, ex);
          }
  
          if (pdfReader != null) {
              List&lt;Integer&gt; pagesToKeep = new ArrayList&lt;Integer&gt;();
  
              //pdfReader.getPages
              for (int i = 1; i &lt;= pdfReader.getNumberOfPages(); i++) {
                  if (i != pageToDelete) {
                      pagesToKeep.add(i);
                  }
              }
              pdfReader.selectPages(pagesToKeep);
          }
  
          PdfStamper pdfStamper = null;
          try {
              pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(newFilePath));
  
          } catch (FileNotFoundException ex) {
              Logger.getLogger(ITextHelper.class.getName()).log(Level.SEVERE, null, ex);
          } catch (DocumentException | IOException ex) {
              Logger.getLogger(ITextHelper.class.getName()).log(Level.SEVERE, null, ex);
          } finally {
              if (pdfStamper != null) {
                  try {
                      pdfStamper.close();
                  } catch (DocumentException | IOException ex) {
                      Logger.getLogger(ITextHelper.class.getName()).log(Level.SEVERE, null, ex);
                  }
              }
              if (pdfReader != null) {
                  pdfReader.close();
              }
          }
      }
  
  
      // -- begin-
      public static void main(String[] args) {
          String fileToAddTo = "src/com/protocase/itexthelper/examplepdf/BUILD_SHEETS_O4_4USS Rev5.pdf";
          String fileWithPageRemoved = "src/com/protocase/itexthelper/examplepdf/BUILD_SHEETS_O4_4USS Rev5-no page 2.pdf";
          String fileToAppend = "src/com/protocase/itexthelper/examplepdf/example_component_list.pdf";
          String testFile = "src/com/protocase/itexthelper/examplepdf/testfile.pdf";
          deletePage(fileToAddTo, 2, testFile);
          addPDFToPDF(fileWithPageRemoved, fileToAppend, testFile, 2);
      }
  
      /**
       * @param originalFilePath Merge multiple pdf into one pdf
       * @param fileToInsertPath
       *
       * @param list of pdf input stream
       * @param location where you want to insert the page
       * @param outputLocation output stream
       * @throws DocumentException
       * @throws IOException
       */
      public static void addPDFToPDF(String originalFilePath, String fileToInsertPath, String outputLocation, int location) {
          OutputStream out = null;
          try {
              out = new FileOutputStream(new File(outputLocation));
          } catch (FileNotFoundException ex) {
              System.out.println("ITextHelper.addPDFToPDF(): can't find output file location: "+ex);
          }
          Document document = new Document();
          PdfWriter writer = null;
          try {
              writer = PdfWriter.getInstance(document, out);
          } catch (DocumentException ex) {
              System.out.println("ITextHelper.addPDFToPDF(): issue getting writer: "+ex);
          }
  
          document.open();
          if (writer != null) {
  
              PdfContentByte cb = writer.getDirectContent();
  
              PdfReader originalFileReader = null;
              try {
                  originalFileReader = new PdfReader(originalFilePath);
              } catch (IOException ex) {
                  System.out.println("ITextHelper.addPDFToPDF(): can't read original file: " + ex);
              }
              PdfReader fileToAddReader = null;
              try {
                  fileToAddReader = new PdfReader(fileToInsertPath);
              } catch (IOException ex) {
                  System.out.println("ITextHelper.addPDFToPDF(): can't read fileToInsert: " + ex);
              }
  
              if (fileToAddReader != null &amp;&amp; originalFileReader != null) {
                  for (int i = 1; i &lt;= originalFileReader.getNumberOfPages(); i++) {
                      if (i == location) {
                          for (int j = 1; j &lt;= fileToAddReader.getNumberOfPages(); j++) {
                              document.newPage();
  
                              document.setPageSize(fileToAddReader.getPageSize(j));
                              //import the page from source pdf
                              PdfImportedPage page = writer.getImportedPage(fileToAddReader, j);
                              //add the page to the destination pdf
                              cb.addTemplate(page, 0, 0);
                          }
                      }
                      document.newPage();
                      document.setPageSize(originalFileReader.getPageSize(i));
                      PdfImportedPage page = writer.getImportedPage(originalFileReader, i);
                      cb.addTemplate(page, 0, 0);
                  }
              }
          }
          closeOutputStream(out, document);
      }
      // -- end
  
      private static void closeOutputStream(OutputStream out, Document document) {
          if (out != null) {
              try {
                  out.flush();
              } catch (IOException ex) {
                  System.out.println("ITextHelper.closeOutputStream(): can't flush outstream: " + ex);
              }
              document.close();
              try {
                  out.close();
              } catch (IOException ex) {
                  System.out.println("can't close outstream: " + ex);
              }
          }
      }
  
  }
  </code></pre>