I think I've got it figured out. I came up with this code to display the layer names and content stream of the first page of a document.
Code:
var doc = PdfReader.Open(fileName);
var page = doc.Pages[0];
var resources = page.Elements.GetDictionary("/Resources");
var properties = resources?.Elements.GetDictionary("/Properties");
if (!(properties is null))
{
foreach (var property in properties)
{
if (property.Value is PdfReference propertyValue &&
propertyValue.Value is PdfDictionary propertyDict)
{
// Write the key and name of the layer
var layerName = propertyDict.Elements.GetString("/Name");
Debug.WriteLine($"Key: {property.Key} Value: {layerName}");
}
}
}
var contents = page.Contents;
var elements = contents.Elements;
foreach (var element in elements)
{
if (element is PdfReference reference && reference.Value is PdfContent content)
{
// Write the entire content stream
Debug.WriteLine(content.Stream);
}
}