Control Pages To Merge The component offers various ways to control which pages should be merged
Table of Contents
Introduction
The addFile()
and addDocument()
methods accept a definition of which pages should be imported in their $pages
parameter.
This parameter is very dynamic and can handle various parameter types.
The $pages Parameter
The component will check a pages parameter internally with the \setasign\SetaPDF2\Merger\Merger::_checkPageNumber()
method. The initial $pages
parameter will be used as the $condition
parameter:
Description
null|int|string|array|callback $condition = null
Checks a page number against a condition.
Parameters
- $pageNumber : int
The page number
- $condition : null|int|string|array|callback
Valid conditions are:
- PAGES_XXX constant or null (equal to \setasign\SetaPDF2\Merger\Merger::PAGES_ALL)
- Integer with the valid page number
- String with the valid page number or the valid range (e.g. '10-12')
- Array with all valid page numbers
- Callback with the arguments (int $pageNumber, \setasign\SetaPDF2\Core\Document $document)
Exceptions
The possible constants are defined as following:
Keyword for all pages
Keyword for the first page
Keyword for the last page
Examples
Some examples with the predefined constants:
use setasign\SetaPDF2\Merger\Merger; // Let's merge all pages (default behavior): $merger->addDocument($document, Merger::PAGES_ALL); // Let's merge only the first page: $merger->addDocument($document, Merger::PAGES_FIRST); // Let's merge only the last page: $merger->addDocument($document, Merger::PAGES_LAST);
Other examples with numeric values and ranges:
// Let's merge only page number 2: $merger->addDocument($document, 2); // Let's merge page number 2, 3, 4: $merger->addDocument($document, array(2, 3, 4)); // Let's merge all pages but the first one: $merger->addDocument($document, '2-'); // Let's merge the first 5 pages: $merger->addDocument($document, '1-5');
It is also possible to control the behavior by a callback:
// We use an anonymous function here $even = function($pageNumber, $currentDocument) { return ($pageNumber % 2) == 0; }; $merger->addDocument($document, $even);
To change the page order of an existing document, just use the $pages
parameter to define the new order. A single document instance or path can be passed several times:
$path = 'path/to/document.pdf'; $merger->addFile($path, '11-'); // all pages after page 10 $merger->addFile($path, '1-10'); // the first 10 pages