title: Merge two PDF pages into one showing different halves of each
body: <p>I have a folder of single paged PDF files where I want to merge pairs of two into a new single paged PDF so that always the lower half of each page is not shown. It contains useless information that's why I need to blank it.</p>
  <p><a href="https://i.stack.imgur.com/o6Z7l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o6Z7l.png" alt="enter image description here" /></a></p>
  <p>My code is now this but I get it wrong because I need to rotate only one of the input page. But now it seems there are both pages rotated:</p>
  <p>(If I could just <em><strong>move</strong></em> one half it would be great, too)</p>
  <pre><code>private void clearLowerHalfOfPage(PDDocument doc) throws IOException {
      PDPage page = doc.getPage(0);
      PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
      contentStream.setNonStrokingColor(Color.WHITE);
      contentStream.addRect(0, 0, 600, 440);
      contentStream.fill();
      contentStream.close();
    }
  
    public void testMerge() throws IOException {
      File path = new File(&quot;/Users/me/Documents/PDF/&quot;);
      List&lt;File&gt; files = Arrays.asList(path.listFiles());
      for (int i = 0; i &lt; files.size() - 1; i += 2) {
        PDDocument doc1 = PDDocument.load(files.get(i));
        PDDocument doc2 = PDDocument.load(files.get(i + 1));
  
        clearLowerHalfOfPage(doc2);
        clearLowerHalfOfPage(doc1);
  
        PDPage page = doc2.getPage(0);
        page.setRotation(180);
  
        Map&lt;Integer, PDDocument&gt; mapping = new HashMap&lt;&gt;();
        mapping.put(1, doc1);
        Overlay overlayObj = new Overlay();
        overlayObj.setInputPDF(doc2);
  
        PDDocument pdDocument = overlayObj.overlayDocuments(mapping);
        pdDocument.save(new File(path, &quot;x.pdf&quot;));
  
        pdDocument.close();
        overlayObj.close();
        doc1.close();
        doc2.close();
      }
    }
  </code></pre>
  <p>How can I achieve this construction?</p>