How to set a OCG layer to be off by default when opening a PDF?

51 views
Skip to first unread message

Ryan

unread,
Feb 16, 2018, 1:29:37 PM2/16/18
to PDFTron PDFNet SDK
Question:

I have a PDF file, that contains layers. One of the layers is set to display by default, but I want to modify the PDF so that the layer is off by default.

Answer:

Given the layer name is in the variable layer_to_turn_off, then the following code will do this.

using (PDFDoc doc = new PDFDoc(filepath))
{
    doc
.InitSecurityHandler();
   
Config config = doc.GetOCGConfig();
   
Obj ocgs = doc.GetOCGs();
   
if (ocgs == null) return;
   
int ocg_sz = ocgs.Size();
   
for(int i = 0; i < ocg_sz; ++i)
    {
       
Obj ocg = ocgs.GetAt(i);
       
Group group = new Group(ocg);
       
string ocg_name = group.GetName();
       
Console.WriteLine(ocg_name);
       
if (ocg_name == layer_to_turn_off) group.SetInitialState(config, false);
   
}
    doc
.Save(outfilepath, SDFDoc.SaveOptions.e_linearized);
}

Reply all
Reply to author
Forward
0 new messages