Skip to main content
Skip table of contents

Manipulating the order of Optional Content Groups (layers) in Mako

📌 Overview

This article explains how to manipulate the order of Optional Content Groups (OCGs, also known as layers) in a PDF using the Mako SDK. This is useful when you need to change the stacking order of layers as presented in PDF viewers such as Adobe Acrobat.

The solution demonstrates how the CEDLVectOrderEntry (or COrderEntry) interacts with IOptionalContentGroup to reorder layers programmatically.


📗 Instructions

  1. Load the PDF document containing OCGs.

  2. Identify the OCGs you wish to reorder. Each OCG typically represents a layer in the PDF.

  3. Use the CEDLVectOrderEntry/COrderEntry classes to manipulate the order of these groups. The order in the vector determines the stacking order in the viewer.

  4. Save the modified PDF and verify the new order in a PDF viewer.


⌨️ Sample Code

Below is a simple C# example that demonstrates moving a layer titled "Robot 11" to the top of the layer stack.

C#
// 1) Get the document's Optional Content and its default configuration
IOptionalContent oc = doc.getOptionalContent();
IOptionalContentConfiguration cfg = oc.getDefaultConfiguration();   // default UI config

// 2) Fetch the current "Order" array (this drives the layer tree shown in viewers)
CEDLVectOrderEntry order = cfg.getOrder();

// 3) Find the IOptionalContentGroupReference for the target Robot 11 its group name
CEDLVectIOptionalContentGroup groups = oc.getGroups();
IOptionalContentGroupReference targetRef = IOptionalContentGroupReference.Null();
for (uint i = 0; i < groups.size(); i++)
{
    var g = groups[i];
    if (g.getName() == "Robot 11") { targetRef = g.getReference(); break; }
}
if (targetRef == null)
    throw new InvalidOperationException("Layer 'Robot 11' not found.");

// 4) Locate the matching COrderEntry in the order vector
int foundIdx = -1;
for (uint i = 0; i < order.size(); i++)
{
    COrderEntry entry = order[i];
    if (entry.isGroup && entry.groupRef.equals(targetRef))
    {
        foundIdx = (int)i;
        break;
    }
}
if (foundIdx < 0)
    throw new InvalidOperationException("'Robot 11' not present in the current order.");

// 5) Move that entry to the top of the order vector
COrderEntry toMove = order[(uint)foundIdx];
order.erase((uint)foundIdx);   // removes the original position
order.insert(0, toMove);          // inserts at the top

// 6) Write the modified order back to the configuration
cfg.setOrder(order);

🎨 Results

After running the code, the layer titled "Robot 11" will appear at the top of the layer list in PDF viewers that support OCGs (such as Adobe Acrobat). You can use the provided sample PDF ( Roland the Roland Color Library Robot.pdf) to test this behaviour.


☑️ Conclusion

Manipulating the order of Optional Content Groups in Mako is straightforward using the provided API. By adjusting the order vector, you can control how layers are presented to end users.


📚 Additional Resources

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.