Layers
editAdd 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;- A group added hidden is off when the document is opened: IsVisible reads and changes its state in the default configuration.
- An annotation belongs to a group with PdfAnnotation.OptionalContent.
- PdfPageMode.UseOptionalContent opens the list of layers when the document is opened.
- A SetOptionalContentState action turns groups on or off, for a link or a button: see Actions.
Configurations
The default configuration (DefaultConfiguration) gives the state of the groups when the document is opened:
- BaseState is the state of the groups the configuration does not list; IsVisible and SetVisible read and change the state of a group.
- SetLocked keeps the user from changing a group.
- AddRadioButtonGroup makes groups behave as radio buttons: turning one on turns the others off.
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.