title: Merging PDF using Itext
body: <p>I am encountering an issue while merging two PDFs generated out of IText.</p>
  
  <p>Code snippet of PDF Creation:</p>
  
  <p>Document configuration is as below:</p>
  
  <pre><code>iText_xls_2_pdf = new Document(PageSize.A4.rotate(),10,0,30,0);
  </code></pre>
  
  <p>Using this configuration 2 PDFs are created.</p>
  
  <p>After creation of these PDFs, they need to be merged as single pdf.</p>
  
  <p>Below code is used to merge the PDFs:</p>
  
  <pre><code>Document document = new Document(PageSize.A4.rotate(),10,0,30,0);
  try {
      List&lt;InputStream&gt; pdfs = streamOfPDFFiles;
      List&lt;PdfReader&gt; readers = new ArrayList&lt;PdfReader&gt;();
      int totalPages = 0;
      Iterator&lt;InputStream&gt; iteratorPDFs = pdfs.iterator();
      // Create Readers for the pdfs.
      while (iteratorPDFs.hasNext()) {
          InputStream pdf = iteratorPDFs.next();
          PdfReader pdfReader = new PdfReader(pdf);
          readers.add(pdfReader);
          totalPages += pdfReader.getNumberOfPages();
      }
      // Create a writer for the outputstream
      PdfWriter writer = PdfWriter.getInstance(document, outputStream);
      document.open();
      BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
              BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
      PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
      // data
      PdfImportedPage page;
      int currentPageNumber = 0;
      int pageOfCurrentReaderPDF = 0;
      Iterator&lt;PdfReader&gt; iteratorPDFReader = readers.iterator();
      // Loop through the PDF files and add to the output.
      while (iteratorPDFReader.hasNext()) {
          PdfReader pdfReader = iteratorPDFReader.next();
          // Create a new page in the target for each source page.
          while (pageOfCurrentReaderPDF &lt; pdfReader.getNumberOfPages()) {
              document.newPage();
              pageOfCurrentReaderPDF++;
              currentPageNumber++;
              page = writer.getImportedPage(pdfReader,
                      pageOfCurrentReaderPDF);
              cb.addTemplate(page, 0, 0);
              // Code for pagination.
              if (paginate) {
                  cb.beginText();
                  cb.setFontAndSize(bf, 9);
                  cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
                          + currentPageNumber + " of " + totalPages, 520,
                          5, 0);
                  cb.endText();
              }
          }
          pageOfCurrentReaderPDF = 0;
      }
      outputStream.flush();
      document.close();
      outputStream.close();
  } catch (Exception e) {
      e.printStackTrace();
  } finally {
      if (document.isOpen())
          document.close();
      try {
          if (outputStream != null)
              outputStream.close();
      } catch (IOException ioe) {
          ioe.printStackTrace();
      }
  }
  </code></pre>
  
  <p>This creates the merged PDF. but Because I have used .rotate in Document configuration, Merged pdf comes in landscape format. How could I avoid this.</p>