Foxit PDF SDK for Windows

How to create PDF layers with Foxit PDF SDK (C++)

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

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/common/fs_render.h"
#include "include/pdf/fs_pdflayer.h"
using namespace foxit;
using namespace pdf;
 ...
// Assuming PDFDoc doc has been loaded.
LayerTree layertree(doc);
LayerNode root = layertree.GetRootNode();
if (root.IsEmpty()) {
 printf("No layer information!\r\n");
 return ;
} 
...

How to set all the layer nodes information

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
#include "include/common/fs_render.h"
#include "include/pdf/fs_pdflayer.h"
using namespace foxit;
using namespace foxit::common;
using foxit::common::Library;
using namespace pdf;
 ...
// Assuming PDFDoc doc has been loaded.
// Assuming PDFpage page has been loaded and parsed.
void SetAllLayerNodesInformation(LayerNode layer_node) {
 if (layer_node.HasLayer()) {
 layer_node.SetDefaultVisible(true);
 layer_node.SetExportUsage(LayerTree::e_StateUndefined);
 layer_node.SetViewUsage(LayerTree::e_StateOFF);
 LayerPrintData print_data("subtype_print", LayerTree::e_StateON);
 layer_node.SetPrintUsage(print_data);
 LayerZoomData zoom_data(1, 10);
 layer_node.SetZoomUsage(zoom_data);
 WString new_name = WString(L"[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);
 }
}
LayerTree layertree(doc);
LayerNode root = layertree.GetRootNode();
SetAllLayerNodesInformation(root);
...

How to edit layer tree

#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
#include "include/common/fs_render.h"
#include "include/pdf/fs_pdflayer.h"
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
 ...
// Assuming PDFDoc doc has been loaded.
// edit layer tree
PDFDoc doc(input_file);
error_code = doc.Load();
layertree = LayerTree(doc);
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, L"AddedLayerNode", true);
child.AddChild(0, L"AddedNode", false);

Updated on May 8, 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: