DynaPDF Manual - Page 469

Previous Page 468   Index   Next Page 470

Function Reference
Page 469 of 839
// This algorithm is a bit simplified. In a real world application we must store the OCG handles in
// the tree node so that the layer state can be changed. The Delphi tree control supports the user
// defined pointer Data which can be used for this purpose. However, Data is a pointer and therefore
// we must store the OCG handles in a list before we can set the pointer. In addition, a standard
// tree control does not support the features we need to show or hide layers.
// We assume that the PDF file was already opened.
procedure TForm1.LoadLayerTreeNodes(Parent: TTreeNode; Next: IOCN; Level: Integer);
var node: TOCUINode; ocg: TPDFOCG; lbl: String; prnt, saved: TTreeNode;
if Level > 32 then Exit; // A maximum recursion depth of 32 should be more than enough.
node.StructSize := sizeof(node);
:= sizeof(ocg);
saved := Parent;
while Next <> nil do begin
node.NextChild := nil;
:= -1;
:= FPDF.GetOCUINode(Next, @node);
if node.OCG < 0 then begin
lbl := GetString(node.LabelA, node.LabelW);
if node.NewNode then
prnt := LayerTree.Items.AddNode(nil, Parent, lbl, nil, naAddChild)
prnt := Parent;
LoadLayerTreeNodes(prnt, node.NextChild, Level +1);
Parent := saved; // Important! Restore the parent tree node.
end else begin
if FPDF.GetOCG(node.OCG, ocg) then begin
lbl := GetString(ocg.NameA, ocg.NameW);
if node.NewNode then begin
:= Parent; // Important! Save the parent tree node.
Parent := LayerTree.Items.AddNode(nil, Parent, lbl, nil, naAddChild)
end else
LayerTree.Items.AddNode(nil, Parent, lbl, nil, naAddChild);
procedure TForm1.LoadLayerTree;
var next: IOCN;
// Import the global Optional Content Properties
// Load the default layer configuration
next := FPDF.GetOCUINode(nil, nil);
// Get the root node
LoadLayerTreeNodes(nil, next, 0);
// Load the children of the root
Although the syntax is slightly different among different programming languages, the general
algorithm stays the same.

Previous topic: GetOCUINode

Next topic: GetOpacity, GetOrientation, GetOutputIntent