Crionics LOGO

TUTORIALS

 

 How to use jPDF Creator:
Adding PDF Layers

As easy as 1, 2, 3...

 

What PDF Layers are

The concept of Layers isn't new: Photoshop, Illustration and CAD packages have been using Layers to separate content for many years. However, the ability to have this functionality inside a PDF document wasn't met until Acrobat 6.0 was released.

Conceptually, PDF Layers is like a set of transparent pages stacked together. Each layer is independent from every other, and each can be displayed or hidden at user request. Layers provides great flexibility in the choice of rendering quality as it helps to group similar elements together - for instance in a blueprint where walls, windows, furniture, stairs and doors can be presented on different layers.

The potential of Layers goes far beyond CAD applications. For example, a company brochure could be composed with two layers. The page can be blank initially with just the background images reserved for the products; when the PDF is opened, however, a little bit of Javascript checks the language of the user and switches on either the French Layer or the English layer. These two layers contain the appropriate translated text for the product, all in one PDF!

But where are layers located in Acrobat 6 Reader?

When a document has layers (assuming that you have Acrobat 6+ Reader or better) you can see this icon at the bottom left of the screen. Then if you click the Layers tab, you should get something like this:

It is important to note that layer content is backward compatible with previous versions of PDF, in which case they will show on the background.

 

Programming Layers

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);

 

Save the document and run the application

Final step: Save the document to a file and close it in order to release memory.

doc.saveAs("SimpleLayerDemo.pdf");
doc.close();

Note how we saved the document to a new file. We could have used the save() method which saves the update into the existing document with an incremental update.

The full source code of this tutorial is part of the distribution.
Please click on this link to display the generated PDF.


© 2000-2007 Crionics Inc. Report issues with this page