1. Home
  2. Foxit PDF SDK for Windows
  3. How to create PDF layers with Foxit PDF SDK (Java)
  1. Home
  2. Foxit PDF SDK for Mac
  3. How to create PDF layers with Foxit PDF SDK (Java)
  1. Home
  2. Foxit PDF SDK for Linux
  3. How to create PDF layers with Foxit PDF SDK (Java)

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.

Example:

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()) {
layer_node.setDefaultVisible(true);
layer_node.setExportUsage(e_StateUndefined);
layer_node.setViewUsage(e_StateOFF);
LayerPrintData print_data = new LayerPrintData("subtype_print", e_StateON);
layer_node.setPrintUsage(print_data);
LayerZoomData zoom_data = new LayerZoomData(1, 10);
layer_node.setZoomUsage(zoom_data);
String new_name = String.format("[View_OFF_Print_ON_Export_Undefined]") +  layer_node.getName();
layer_node.setName(new_name);
}
int count = layer_node.getChildrenCount();
for (int i = 0; i < count; i++) {
LayerNode child = layer_node.getChild(i);
setAllLayerNodesInformation(child);
}
}
...

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 October 23, 2019

Was this article helpful?

Related Articles