title: PDFBox: How to draw text on top of a filled rectangle?
body: <p>I am trying to use Java with PDFBox to draw some text to a PDF file, and set a background color for the text.  I know how to draw text and draw filled rectangles, but when I try to draw text in the same position as a rectangle, the text is never shown.  Example:</p>
  
  <pre><code>//draw rectangle
  content.setNonStrokingColor(200, 200, 200); //gray background
  content.fillRect(cursorX, cursorY, 100, 50);
  
  //draw text
  content.setNonStrokingColor(0, 0, 0); //black text
  content.beginText();
  content.setFont(family, fontPt);
  content.moveTextPositionByAmount(cursorX, cursorY);
  content.drawString("Test Data");
  content.endText();
  </code></pre>
  
  <p>The text never shows up. It is always covered by the rectangle. Any ideas for how to make the text draw on top of the rectangle?</p>
  
  <p>EDIT: As Mkl mentioned in answer, the code I provided actually works. My problem ended up being that the code was in a loop, drawing the background for each line, but the background was drawing over the previous line, and not the current line, overwriting previous text. I just needed to alter the order of events in my looping. Should this question be deleted? It seems unlikely that anyone else would find it useful.</p>