- 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.
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 = $document->getCatalog()->getPages(); $pageCount = $pages->count(); // or $pageCount = count($pages);
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
To create a new page the class offers you a helper method:
Create 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.
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
prepent() 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: