Frequently Asked Question:
How to create optional content groups (ocgs) in PDF files
Question
How do I create optional content groups (OCGs) in PDF files?
I want to add OCGs so that my end-users can show/hide certainly content when viewing PDF files in Adobe Acrobat. I realize that in Acrobat optional content groups are called layers.
Answer
This JScript sample shows you how to create multiple optional content groups -- along with some text on each OCG to illustrate how it works -- in a PDF document.
// Create four new optional content groups
OCG1 = QP.NewOptionalContentGroup("Layer 1");
OCG2 = QP.NewOptionalContentGroup("Layer 2");
OCG3 = QP.NewOptionalContentGroup("Layer 3");
OCG4 = QP.NewOptionalContentGroup("Layer 4");
// Select the page that you want the layers to be
// associated with.
QP.SelectPage(1);
// Specify top left corner for starting point
// of all drawing functions.
QP.SetOrigin(1);
// Add OCG 1
QP.NewLayer();
QP.SelectLayer(1);
QP.DrawText(100, 100, "Layer 1");
QP.SetLayerOptional(OCG1);
QP.SetOptionalContentGroupVisible(OCG1, 1);
// Add OCG 2
QP.NewLayer();
QP.SelectLayer(2);
QP.DrawText(200, 100, "Layer 2");
QP.SetLayerOptional(OCG2);
QP.SetOptionalContentGroupVisible(OCG2, 1);
// Add OCG 3
QP.NewLayer();
QP.SelectLayer(3);
QP.DrawText(300, 100, "Layer 3");
QP.SetLayerOptional(OCG3);
QP.SetOptionalContentGroupVisible(OCG3, 1);
// Add OCG 4
QP.NewLayer();
QP.SelectLayer(4);
QP.DrawText(400, 100, "Layer 4");
QP.SetLayerOptional(OCG4);
QP.SetOptionalContentGroupVisible(OCG4, 1);
// Save file to disk with new layers
QP.SaveToFile("layers.pdf");