DynaPDF Manual - Page 457

Previous Page 456   Index   Next Page 458

Function Reference
Page 457 of 818
// 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;
begin
if Level > 32 then Exit; // A maximum recursion depth of 32 should be more than enough.
node.StructSize := sizeof(node);
ocg.StructSize
:= sizeof(ocg);
saved := Parent;
while Next <> nil do begin
node.NextChild := nil;
node.OCG
:= -1;
Next
:= 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)
else
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
saved
:= 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);
end;
end;
end;
end;
procedure TForm1.LoadLayerTree;
var next: IOCN;
begin
FPDF.ImportOCProperties;
// Import the global Optional Content Properties
FPDF.LoadLayerConfig(-1);
// Load the default layer configuration
next := FPDF.GetOCUINode(nil, nil);
// Get the root node
LoadLayerTreeNodes(nil, next, 0);
// Load the children of the root
end;
Although the syntax is slightly different among different programming languages, the general
algorithm stays the same.
 

Previous topic: GetOCUINode

Next topic: GetOpacity, GetOrientation, GetOutputIntent


Start Chat