Foxit PDF SDK for Windows

How to create PDF layers with Foxit PDF SDK (.NET)

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, 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

using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
 ...
// Assuming PDFDoc doc has been loaded.
// Assuming PDFpage page has been loaded and parsed.
LayerTree layertree = new LayerTree(doc);
LayerNode root = layertree.GetRootNode();
...

How to set all layer node information

using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
...
// Assuming PDFDoc doc has been loaded.
// Assuming PDFpage page has been loaded and parsed.
public static void SetAllLayerNodesInformation(LayerNode layer_node) 
{
 if (layer_node.HasLayer()) 
 {
 layer_node.SetDefaultVisible(true);
 layer_node.SetExportUsage(LayerTree.UsageState.e_StateUndefined);
 layer_node.SetViewUsage(LayerTree.UsageState.e_StateOFF);
 LayerPrintData print_data = new LayerPrintData("subtype_print", LayerTree.UsageState.e_StateON);
 layer_node.SetPrintUsage (print_data);
 LayerZoomData zoom_data = new LayerZoomData(1, 10);
 layer_node.SetZoomUsage(zoom_data);
 string new_name = "[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++) 
 {
 using (LayerNode child = layer_node.GetChild(i))
 {
 SetAllLayerNodesInformation(child);
 }
 }
}
LayerTree layertree = new LayerTree(doc);
LayerNode root = layertree.GetRootNode();
if (root.IsEmpty())
{
 return;
}
SetAllLayerNodesInformation(root);
...

How to traverse the layer tree and set the opposite visible state of every PDF layer

using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
 ...
// Assuming PDFDoc doc has been loaded.
// Assuming PDFpage page has been loaded and parsed.
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: