Now that we have the page and its dimensions, let's draw some graphical primitives.
// Create a new document:
PdfDocument doc = new PdfDocument();
// Create a new page:
Page page = Page.createInstance(doc);
PageFormat pf = page.getPageFormat();
int page_y = (int) pf.getHeight();
// Get the graphics - note that it's a JPdfGraphics2D:
JPdfGraphics2D g2d = page.getGraphics();
// Now start the creation of 10 new Layers:
g2d.setFont(new Font("Sans", Font.BOLD, 50));
for (int i = 0; i < NUM_LAYERS; i++
{
String layerName = "Layer #" + i;
// Select the layer:
g2d.setLayer(layerName);
// Draw some text on this layer:
g2d.setPaint(colors[i]);
g2d.drawString(layerName, (i * 15), (i * 15) + (page_y / 4));
}
// Important: DO NOT forget to dispose():
g2d.dispose();
// Note we could have hidden any layer via:
// LayerProperty layerProps = doc.getCreatorModule().getLayerProperty("Layer #" + 1);
// layerProps.setVisibleForViewing(false);