PDF Page Manipulation - Merge, Copy, Delete, Rearrange - PHP Sample Code

Sample code for using Apryse SDK to copy pages from one document to another, delete and rearrange pages, and use ImportPages() method for very efficient copy and merge operations. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14 PDFNet::Initialize($LicenseKey);
15 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
16
17 // Sample 1 - Split a PDF document into multiple pages
18
19 echo nl2br("_______________________________________________\n");
20 echo nl2br("Sample 1 - Split a PDF document into multiple pages...\n");
21 echo nl2br("Opening the input pdf...\n");
22 $in_doc = new PDFDoc($input_path."newsletter.pdf");
23 $in_doc->InitSecurityHandler();
24
25 $page_num = $in_doc->GetPageCount();
26 for ($i = 1; $i <= $page_num; ++$i)
27 {
28 $new_doc = new PDFDoc();
29 $new_doc->InsertPages(0, $in_doc, $i, $i, PDFDoc::e_none);
30 $new_doc->Save($output_path."newsletter_split_page_".$i.".pdf", SDFDoc::e_remove_unused);
31 echo nl2br("Done. Result saved in newsletter_split_page_".$i.".pdf\n");
32 $new_doc->Close();
33 }
34 $in_doc->Close();
35
36 // Sample 2 - Merge several PDF documents into one
37
38 echo nl2br("_______________________________________________\n");
39 echo nl2br("Sample 2 - Merge several PDF documents into one...\n");
40 $new_doc = new PDFDoc();
41 $new_doc->InitSecurityHandler();
42
43 $page_num = 15;
44 for ($i = 1; $i <= $page_num; ++$i)
45 {
46 echo nl2br("Opening newsletter_split_page_".$i.".pdf\n");
47 $in_doc = new PDFDoc($output_path."newsletter_split_page_".$i.".pdf");
48 $new_doc->InsertPages($i, $in_doc, 1, $in_doc->GetPageCount(), PDFDoc::e_none);
49 $in_doc->Close();
50 }
51 $new_doc->Save($output_path."newsletter_merge_pages.pdf", SDFDoc::e_remove_unused);
52 echo nl2br("Done. Result saved in newsletter_merge_pages.pdf\n");
53 $in_doc->Close();
54
55 // Sample 3 - Delete every second page
56
57 echo nl2br("_______________________________________________\n");
58 echo nl2br("Sample 3 - Delete every second page...\n");
59 echo nl2br("Opening the input pdf...\n");
60 $in_doc = new PDFDoc($input_path."newsletter.pdf");
61 $in_doc->InitSecurityHandler();
62
63 $page_num = $in_doc->GetPageCount();
64 while ($page_num>=1)
65 {
66 $itr = $in_doc->GetPageIterator($page_num);
67 $in_doc->PageRemove($itr);
68 $page_num -= 2;
69 }
70
71 $in_doc->Save($output_path."newsletter_page_remove.pdf", 0);
72 echo nl2br("Done. Result saved in newsletter_page_remove.pdf...\n");
73
74 //Close the open document to free up document memory sooner than waiting for the
75 //garbage collector
76 $in_doc->Close();
77
78 // Sample 4 - Inserts a page from one document at different
79 // locations within another document
80
81 echo nl2br("_______________________________________________\n");
82 echo nl2br("Sample 4 - Insert a page at different locations...\n");
83 echo nl2br("Opening the input pdf...\n");
84
85 $in1_doc = new PDFDoc($input_path."newsletter.pdf");
86 $in1_doc->InitSecurityHandler();
87
88 $in2_doc = new PDFDoc($input_path."fish.pdf");
89 $in2_doc->InitSecurityHandler();
90
91 $src_page = $in2_doc->GetPageIterator();
92 $dst_page = $in1_doc->GetPageIterator();
93 $page_num = 1;
94 while ($dst_page->HasNext()) {
95 if ($page_num++ % 3 == 0) {
96 $in1_doc->PageInsert($dst_page, $src_page->Current());
97 }
98 $dst_page->Next();
99 }
100
101 $in1_doc->Save($output_path."newsletter_page_insert.pdf", 0);
102 echo nl2br("Done. Result saved in newsletter_page_insert.pdf...\n");
103
104 //Close the open document to free up document memory sooner than waiting for the
105 //garbage collector
106 $in1_doc->Close();
107 $in2_doc->Close();
108
109 // Sample 5 - Replicate pages within a single document
110
111 echo nl2br("_______________________________________________\n");
112 echo nl2br("Sample 5 - Replicate pages within a single document...\n");
113 echo nl2br("Opening the input pdf...\n");
114 $doc = new PDFDoc($input_path."newsletter.pdf");
115 $doc->InitSecurityHandler();
116
117 // Replicate the cover page three times (copy page #1 and place it before the
118 // seventh page in the document page sequence)
119 $cover = $doc->GetPage(1);
120 $p7 = $doc->GetPageIterator(7);
121 $doc->PageInsert($p7, $cover);
122 $doc->PageInsert($p7, $cover);
123 $doc->PageInsert($p7, $cover);
124
125 // Replicate the cover page two more times by placing it before and after
126 // existing pages.
127 $doc->PagePushFront($cover);
128 $doc->PagePushBack($cover);
129
130 $doc->Save($output_path."newsletter_page_clone.pdf", 0);
131 echo nl2br("Done. Result saved in newsletter_page_clone.pdf...\n");
132 $doc->Close();
133
134 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
135 // in order to preserve shared resources between pages (e.g. images, fonts,
136 // colorspaces, etc.)
137
138 echo nl2br("_______________________________________________\n");
139 echo nl2br("Sample 6 - Preserving shared resources using ImportPages...\n");
140 echo nl2br("Opening the input pdf...\n");
141 $in_doc = new PDFDoc($input_path."newsletter.pdf");
142 $in_doc->InitSecurityHandler();
143
144 $new_doc = new PDFDoc();
145
146 $copy_pages = new VectorPage();
147 for ($itr=$in_doc->GetPageIterator(); $itr->HasNext(); $itr->Next())
148 {
149 $copy_pages->push($itr->Current());
150 }
151
152 $imported_pages = $new_doc->ImportPages($copy_pages);
153 for ($i=0; $i<$imported_pages->size(); ++$i)
154 {
155 $new_doc->PagePushFront($imported_pages->get($i)); // Order pages in reverse order.
156 // Use PagePushBack() if you would like to preserve the same order.
157 }
158
159 $new_doc->Save($output_path."newsletter_import_pages.pdf", 0);
160 PDFNet::Terminate();
161 echo nl2br("Done. Result saved in newsletter_import_pages.pdf...\n\n"
162 ."Note that the output file size is less than half the size\n"
163 ."of the file produced using individual page copy operations\n"
164 ."between two documents\n");
165?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales