title: Using Overlay in PDFBox 2.0
body: <p>What I am trying to do here is to create text and place it onto a blank page. That page would then be overlayed onto another document and that would then be saved as one document. In 1.8 I was able to create a blank PDPage in a PDF, write text to it as needed, then overlay that PDF with another and then save or view on screen using the code below -</p>
  
  <pre><code>overlayDoc = new PDDocument();
  page = new PDPage();
  overlayDoc.addPage(page);
  overlayObj = new Overlay();
  font = PDType1Font.COURIER_OBLIQUE;
  try {
      contentStream = new PDPageContentStream(overlayDoc, page);
      contentStream.setFont(font, 10);
  }
  catch (Exception e){
      System.out.println("content stream failed");
  }
  </code></pre>
  
  <p>After I created the stream, when I needed to write something to the overlay document's contentStream, I would call this method, give it my x, y coords and tell it what text to write (again, this is in my 1.8 version):</p>
  
  <pre><code>protected void writeString(int x, int y, String text) {
      if (text == null) return;
      try {
          contentStream.moveTo(x, y);
          contentStream.beginText();
          contentStream.drawString(text);  // deprecated. Use showText(String text)
          contentStream.endText();
      }
      catch (Exception e){
          System.out.println(text + " failed. " + e.toString());
      }
  }
  </code></pre>
  
  <p>I would call this method whenever I needed to add text and to wherever I needed to do so. After this, I would close my content stream and then merge the documents together as such:</p>
  
  <pre><code>import org.apache.pdfbox.Overlay;
  Overlay overlayObj = new Overlay();
  
  ....
  
  PDDocument finalDoc = overlayObj.overlay(overlayDoc, originalDoc);
  </code></pre>
  
  <p><em>finalDoc</em> now contains a PDDocument which is my original PDF with text overlayed where needed. I could save it and view it as a BufferedImage on the desktop. The reason I moved to 2.0 was that first off I needed to stay on top of the most recent library and also that I was having issues putting an image onto the page (see <a href="https://stackoverflow.com/questions/32771489/pdfbox-printing-of-document-with-bufferedimage-fails">here</a>).</p>
  
  <p>The issue I am having in this question is that 2.0 no longer has something similar to the <code>org.apache.pdfbox.Overlay</code> class. To confuse me even more is that there are two Overlay classes in 1.8 (<a href="https://pdfbox.apache.org/docs/1.8.8/javadocs/org/apache/pdfbox/Overlay.html" rel="nofollow noreferrer">org.apache.pdfbox.Overlay</a> and <a href="https://pdfbox.apache.org/docs/1.8.8/javadocs/org/apache/pdfbox/util/Overlay.html" rel="nofollow noreferrer">org.apache.pdfbox.util.Overlay</a>) whereas in 2.0 there is only one. The class I need (<code>org.apache.pdfbox.Overlay</code>), or the methods it offers at least, are not present in 2.0 as far as I can tell. I can only find <a href="https://pdfbox.apache.org/docs/2.0.0-SNAPSHOT/javadocs/org/apache/pdfbox/multipdf/Overlay.html" rel="nofollow noreferrer">org.apache.pdfbox.multipdf.Overlay</a>.</p>