if (!$doc->HasOC()) { echo nl2br("The document does not contain 'Optional Content'\n"); } else { $init_cfg = $doc->GetOCGConfig(); $ctx = new Context($init_cfg); $pdfdraw = new PDFDraw(); $pdfdraw->SetImageSize(1000, 1000); $pdfdraw->SetOCGContext($ctx); // Render the page using the given OCG context. $page = $doc->GetPage(1); // Get the first page in the document. $pdfdraw->Export($page, $output_path . "pdf_layers_default.png"); // Disable drawing of content that is not optional (i.e. is not part of any layer). $ctx->SetNonOCDrawing(false); // Now render each layer in the input document to a separate image. $ocgs = $doc->GetOCGs(); // Get the array of all OCGs in the document. if ($ocgs != null) { $sz = $ocgs->Size(); for ($i = 0; $i < $sz; ++$i) { $ocg = new Group($ocgs->GetAt($i)); $ctx->ResetStates(false); $ctx->SetState($ocg, true); $fname = $output_path . "pdf_layers_" . $ocg->GetName() . ".png"; echo nl2br($fname . "\n"); $pdfdraw->Export($page, $fname); } } // Now draw content that is not part of any layer... $ctx->SetNonOCDrawing(true); $ctx->SetOCDrawMode(Context::e_NoOC);