Sample Java program showing how to add layers to a PDF document and to draw content on the new layers. jPDFProcess can draw onto new or existing layers, on new or existing documents / pages.

// Create a new document
PDFDocument pdf = new PDFDocument();
 
// Add a blank page to the document
PDFPage page = pdf.appendNewPage(8.5 * 72, 11 * 72);
 
// Add new layers
Layer layer1 = pdf.addLayer("Layer 1", Layer.STATE_ON, true);
Layer layer2 = pdf.addLayer("Layer 2", Layer.STATE_ON, true);
 
// Add content to the back layer
Graphics2D g2d = page.createGraphics(layer1);
g2d.setFont (PDFGraphics.HELVETICA.deriveFont(Font.BOLD, 24.0f));
g2d.drawString ("This is a string in layer 1.", 72, 72);
g2d.setColor(Color.red);
g2d.fillOval(216, 72, 10, 20);
g2d.fillOval(246, 72, 10, 20);
g2d.fillOval(276, 72, 10, 20);
 
 
// Add content to the front layer
g2d = page.createGraphics(layer2);
g2d.setFont (PDFGraphics.HELVETICA.deriveFont(Font.BOLD, 24.0f));
g2d.drawString ("This is a string in layer 2.", 72, 144);
g2d.setColor(Color.blue);
g2d.fillOval(216, 144, 10, 20);
g2d.fillOval(246, 144, 10, 20);
g2d.fillOval(276, 144, 10, 20);
 
// you can also add an image here
// g2d.drawImage(myImage, 200, 200, null);
 
pdf.saveDocument("output.pdf");

Download CreateLayers.java

Note: Helvetica® is a trademark Monotype Imaging Inc.