Layers

edit

Add layers

An optional content group is a layer: viewers list the groups and let the user show or hide them. Add a group with AddGroup, and draw its content between BeginOptionalContent and EndMarkedContent:

var notes = document.OptionalContent.AddGroup("Notes");
var draft = document.OptionalContent.AddGroup("Draft watermark", visible: false);
page.CreateContentBuilder()
    .BeginOptionalContent(notes).SetFillColor(PdfColor.FromRgb(0, 0, 1)).Rectangle(0, 0, 50, 50).Fill().EndMarkedContent()
    .BeginOptionalContent(draft).SetFillColor(PdfColor.FromRgb(1, 0, 0)).Rectangle(50, 50, 50, 50).Fill().EndMarkedContent();

// The user cannot hide the notes, and the viewer shows the list of layers
document.OptionalContent.DefaultConfiguration!.SetLocked(notes, locked: true);
document.PageMode = PdfPageMode.UseOptionalContent;

Configurations

The default configuration (DefaultConfiguration) gives the state of the groups when the document is opened:

The other configurations of a file, which viewers can offer, are in Configurations.

Render and flatten layers

The renderer draws the groups that are on in the default configuration, or in a configuration of PdfRenderOptions: see Layers and annotations. The optimizer can remove the hidden content and the groups: see Optimizing.