Foxit PDF SDK for Linux Foxit PDF SDK for Mac Foxit PDF SDK for Windows

How to create PDF layers with Foxit PDF SDK (Java)

PDF Layers, in other words, Optional Content Groups (OCG), are supported in Foxit PDF SDK. Users can selectively view or hide the contents in different layers of a multi-layer PDF document. Multi-layers are widely used in many application domains such as CAD drawings, maps, layered artwork and multi-language document, etc.

In Foxit PDF SDK, a PDF layer is associated with a layer node. To retrieve a layer node, the user should construct a PDF LayerTree object first and then call function LayerTree.getRootNode to get the root layer node of the whole layer tree. Furthermore, you can enumerate all the nodes in the layer tree from the root layer node. Foxit PDF SDK provides APIs to get/set layer data, view or hide the contents in different layers, set layers’ name, add or remove layers, and edit layers.


How to create a PDF layer

import static com.foxit.sdk.pdf.LayerTree;
import static com.foxit.sdk.pdf.LayerNode;
LayerTree layertree = new LayerTree(doc);
LayerNode root = layertree.getRootNode();

How to set layer node information

import com.foxit.sdk.pdf.*;
static void setAllLayerNodesInformation(LayerNode layer_node) throws PDFException {
if (layer_node.hasLayer()) {
LayerPrintData print_data = new LayerPrintData("subtype_print", e_StateON);
LayerZoomData zoom_data = new LayerZoomData(1, 10);
String new_name = String.format("[View_OFF_Print_ON_Export_Undefined]") +  layer_node.getName();
int count = layer_node.getChildrenCount();
for (int i = 0; i < count; i++) {
LayerNode child = layer_node.getChild(i);

How to edit a layer tree

import com.foxit.sdk.pdf.*;
LayerTree layertree = new LayerTree(doc);
LayerNode root = layertree.getRootNode();
int children_count = root.getChildrenCount();
root.removeChild(children_count -1);
LayerNode child = root.getChild(children_count - 2);
LayerNode child0 = root.getChild(0);
child.moveTo(child0, 0);
child.addChild(0, "AddedLayerNode", true);
child.addChild(0, "AddedNode", false);

Updated on May 9, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: