Layers

Table of Contents

  1. Introduction
    1. Example

      Introduction

      The PDF specification comes with a feature called Optional Content that will allow you to group content. A groups visibility can be controlled in various ways. The Core component offers classes to handle them at a very low level. A common way to turn a visibility on and off is the Layer pannel in a Reader application.

      The Merger component will import optional content groups by default, too. It can be controlled by the last parameter of the  addFile() and addDocument() methods.

      Example

      The example will merge two document that make use of layers while leaving the layer information of the second document. The original documents can be seen here and here

      PHP
      <?php
      require_once('library/SetaPDF/Autoload.php');
      
      $merger = new \SetaPDF_Merger();
      
      // add a file with layer definition
      $merger->addFile('files/pdfs/layers/rect+circle+triangle.pdf');
      // add a file with layer definition but do not import the content groupd
      $merger->addFile(array(
          'filename' => 'files/pdfs/layers/rect+circle+polygon.pdf',
          'copyLayers' => false
      ));
      
      // merge them together
      $merger->merge();
      
      // get the resulting document instance
      $document = $merger->getDocument();
      // open the optional content panel by default
      $document->getCatalog()->setPageMode(\SetaPDF_Core_Document_PageMode::USE_OC);
      // add a writer
      $document->setWriter(new \SetaPDF_Core_Writer_Http('outline-1.pdf', true));
      // save and finish
      $document->save()->finish();