- Getting Started
- Memory Usage
- Readers and Writers
- The Document Class
- Page Layout and Mode
- Viewer Preferences
- Document Outline
- Page Labels
- Embedded File Streams
- Colors and Color Spaces
- Page Formats and Boundaries
- Standard and Public Key Encryption
- Fonts and Encodings
- Corrupted Documents
- Reader Enabled Documents
- Refactor Old SetaPDF Code
- API Reference
Pages Accessing pages in the PDF document
Table of Contents
Internally all SetaPDF components have to deal with pages of PDF documents. This process is done via the
SetaPDF_Core_Document_Catalog_Pages class which can be used individually, too.
Get an Instance of the Pages Object
The SetaPDF-Core component offers an easy access to get access to this document via the documents catalog instance:
$document = new SetaPDF_Core_Document(); $pages = $document->getCatalog()->getPages();
A common task is to receive the page count of an existing PDF document.
Pages instance implements the Countable interface, so a
count() method is implemented. You can get the page count this way:
$pages = $document->getCatalog()->getPages(); $pageCount = $pages->count(); // or $pageCount = count($pages);
Get a Page Object
To get access to an existing page you can use the
Get a page.
- $pageNumber : integer
To get access to the last page a simple helper method exists:
The Pages class is optimized in resolving a page object by using the logic of the balanced tree which can be used in a PDF document page tree. For each page it uses the beeline to find the object representing the page. If you know that you will need access to all pages it is faster to ensure all objects with a single run by the following method:
This method makes sure that all pages are read.
It walks the complete page tree to cache/get all page objects in one iteration. This method should be used if all pages of a document should be handled. It is much faster than using the random access.
To delete a page you can use the
You should use this method with care: It will only remove the page object from the page tree. If the page includes e.g. form fields, these fields will not be removed from the document!
Deletes a page.
- $pageNumber : integer
Create a Page
To create a new page the class offers you a helper method:
Create a page.
- $format : string|array
The page format. See constants in SetaPDF_Core_PageFormats and the getFormat() method.
- $orientation : string
The orientation. See constants in SetaPDF_Core_PageFormats.
- $append : boolean
Whether the page should be appended to the page tree or not.
Append or Prepend a Page
A new created page can be appended to the existing pages automatically if the $append parameters of the
create() method is set to true (default). Internally the
create() method calls the
append() method in that case.
If the $append parameter is set to false the page is not attached to the page tree at all. In that case it has to be passed additionally to the
Copy Pages Between Document Instances
Sometimes it is necessary to add a simple existing PDF pages to another PDF document.
This will really only work with simple flat PDF pages. If a page includes foreign structures which refer to other PDF objects or pages in the origin document, ALL objects will be copied to the result document (though they were never needed). This technic should only be used if you have control over the added PDF page. A more robust solution will offer the SetaPDF-Merger component which will handle these situations.
Adding a PDF page from another document instance is possible by passing its page instance to the
prepend() method of the resulting document after calling the flattenInheritedAttributes() on the page instance that should be appended:
The call of
flattenInheritedAttributes() is necessary because of possible internal structures of a PDF page tree. This call will technically modify the page dictionary in both document instances.
If you need to work further on both document instances and you only want the modification to happen in the appended page instance you need to use the
extract() method. This will decouple the objects from the origin document instance: