I am new to PDFNet SDK and still trying to piece together how some of the PDF functionality works. I am impressed by the breadth and depth of the SDK but am trying to understand how the SDK works with layers (aka Optional Content Groups or OCGs). In particular, I am trying to determine how one can detect which layers are non-visible (OFF) so that they and their content can be removed from the PDF. I have initially focused on the classes in the pdftron.PDF.OCG Namespace, especially the Group class. This allows me to iterate the groups and get basic information such as name and current state (ON/OFF), as seen in the code snippet below:
Config init_cfg = doc.GetOCGConfig();
Context ctx = new Context(init_cfg);
Obj ocgs = doc.GetOCGs(); // Get the array of all OCGs in the document.
if (ocgs != null)
{
int i, sz = ocgs.Size();
for (i = 0; i < sz; ++i)
{
Group ocg = new Group(ocgs.GetAt(i));
bool ocgState = ocg.GetCurrentState(ctx); // check if ocg is OFF or ON
if (!ocgState) // layer is not visible
{
// need calls here to delete layer and content
}
}
}
So far, so good. However, I do not see any way to remove layers and the associated content within these classes. Am I missing something? If not, is there another way in the SDK to accomplish removing layers?
Thanks in advance!
Okay, so after much more studying of the SDK samples and much experimentation, I have found one approach which works so far with the PDFs I have tried. The solution is based on the ElementEdit sample, coupled with the Context.SetNonOCDrawing(), Context.SetOCDrawMode(), and Element.IsOCVisible() methods to leave out page elements which are not visible based on the ON/OFF state of the layer. The end result is the removal of the content for layers which are off, with the layer itself still left behind but now empty. Not perfect, but good enough for my purposes. Anyway, here is the code I came up with based on the SDK sample: