title: How can I add a background color to my (pdf-) text using iText to create it with Java
body: <p>At first: My knowledge with frameworks which you can use for pdf creation isn't the best, please keep this in mind when answering. </p>
  
  <p><strong>I needed a framework/library with which I can create pdf-files in java and (important!) place text at certain x and y coordinates. After a lot of research I experienced that I can realize this with iText.</strong> </p>
  
  <p>Here is a simple code snippet that basically shows what I'm doing right now with my text in iText. You can simply copy it into your programming environment, all you need is the iText jar (downloadable for you here: <a href="http://sourceforge.net/projects/itext/files/latest/download?source=files" rel="nofollow">http://sourceforge.net/projects/itext/files/latest/download?source=files</a>)</p>
  
  <pre><code>import java.io.FileOutputStream;
  
  import com.itextpdf.text.Document;
  import com.itextpdf.text.pdf.BaseFont;
  import com.itextpdf.text.pdf.PdfContentByte;
  import com.itextpdf.text.pdf.PdfWriter;
  
  public class PDFTesting {
  
  public static void main(String[] args) {
      Document document = new Document();
      try {
          PdfWriter writer = PdfWriter.getInstance(document,
                  new FileOutputStream("output.pdf"));
          document.open();
          PdfContentByte cb = writer.getDirectContent();
  
          int x = 100; 
          int y = 500; 
  
          String text = "Hello"; 
  
          // now we can place content elements on the page
          BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
          cb.saveState();
          cb.beginText();
          cb.moveText(x, y);
          cb.setFontAndSize(bf, 12);
          cb.showText(text);
          cb.endText();
          cb.restoreState();
  
      } catch (Exception e) {
      }
  
      document.close();
  }
  }
  </code></pre>
  
  <p>I have added possibilities to add my text from my datasources to this method and it's really working great. The result looks very promising to me and so is only one task for me left at the moment: </p>
  
  <p><strong>I need to add specfic background colors to my text (not font color!) that I'm moving and placing in the method shown above.</strong>  </p>
  
  <p>My research didn't provide me any beginner-friendly information about this task so I would be really happy if you could help me solve this.</p>
  
  <p>If possible: <strong>can you modify my example in a way that adds a background color to the added example-text?</strong> I guess me and others (who may be reading this thread in the future having the same problem) would benefit most from this. </p>
  
  <p>If you need further information or have additional suggestions for me please feel free to contact me as well. </p>
  
  <p>Thanks for every answer and thought you're sharing with me. </p>