PDF Page Manipulation - Merge, Copy, Delete, Rearrange - Java 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//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.*;
8import com.pdftron.common.PDFNetException;
9
10public class PDFPageTest {
11
12 public static void main(String[] args) {
13 PDFNet.initialize(PDFTronLicense.Key());
14
15 // Relative path to the folder containing test files.
16 String input_path = "../../TestFiles/";
17 String output_path = "../../TestFiles/Output/";
18
19 // Sample 1 - Split a PDF document into multiple pages
20 System.out.println("_______________________________________________");
21 System.out.println("Sample 1 - Split a PDF document into multiple pages...");
22 System.out.println("Opening the input pdf...");
23 try (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) {
24 in_doc.initSecurityHandler();
25
26 int page_num = in_doc.getPageCount();
27 for (int i = 1; i <= page_num; ++i) {
28 try (PDFDoc new_doc = new PDFDoc()) {
29 new_doc.insertPages(0, in_doc, i, i, PDFDoc.InsertBookmarkMode.NONE, null);
30 String fname = "newsletter_split_page_" + i + ".pdf";
31 new_doc.save(output_path + fname, SDFDoc.SaveMode.REMOVE_UNUSED, null);
32 // output PDF new_doc
33 System.out.println("Done. Result saved in newsletter_split_page_" + i + ".pdf");
34 }
35 }
36 } catch (Exception e2) {
37 System.out.println(e2);
38 }
39
40 // Sample 2 - Merge several PDF documents into one
41 System.out.println("_______________________________________________");
42 System.out.println("Sample 2 - Merge several PDF documents into one...");
43 try (PDFDoc new_doc = new PDFDoc()) {
44 new_doc.initSecurityHandler();
45
46 int page_num = 15;
47 for (int i = 1; i <= page_num; ++i) {
48 System.out.println("Opening newsletter_split_page_" + i + ".pdf");
49 String fname = "newsletter_split_page_" + i + ".pdf";
50 try (PDFDoc in_doc = new PDFDoc(output_path + fname)) {
51 new_doc.insertPages(i, in_doc, 1, in_doc.getPageCount(), PDFDoc.InsertBookmarkMode.NONE, null);
52 }
53 }
54 new_doc.save(output_path + "newsletter_merge_pages.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
55 System.out.println("Done. Result saved in newsletter_merge_pages.pdf");
56 } catch (Exception e2) {
57 System.out.println(e2);
58 }
59
60 // Sample 3 - Delete every second page
61 System.out.println("_______________________________________________");
62 System.out.println("Sample 3 - Delete every second page...");
63 System.out.println("Opening the input pdf...");
64 try (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) {
65 in_doc.initSecurityHandler();
66
67 int page_num = in_doc.getPageCount();
68 while (page_num >= 1) {
69 PageIterator itr = in_doc.getPageIterator(page_num);
70 in_doc.pageRemove(itr);
71 page_num -= 2;
72 }
73
74 in_doc.save(output_path + "newsletter_page_remove.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
75 System.out.println("Done. Result saved in newsletter_page_remove.pdf...");
76
77 } catch (Exception e2) {
78 System.out.println(e2);
79 }
80
81 // Sample 4 - Inserts a page from one document at different
82 // locations within another document
83 System.out.println("_______________________________________________");
84 System.out.println("Sample 4 - Insert a page at different locations...");
85 System.out.println("Opening the input pdf...");
86 try (PDFDoc in1_doc = new PDFDoc((input_path + "newsletter.pdf"))) {
87 in1_doc.initSecurityHandler();
88 try (PDFDoc in2_doc = new PDFDoc((input_path + "fish.pdf"))) {
89 in2_doc.initSecurityHandler();
90
91 PageIterator src_page_itr = in2_doc.getPageIterator();
92 Page src_page = src_page_itr.next();
93 PageIterator dst_page_itr = in1_doc.getPageIterator();
94 int page_num = 1;
95 while (dst_page_itr.hasNext()) {
96 if (page_num++ % 3 == 0) {
97 in1_doc.pageInsert(dst_page_itr, src_page);
98 }
99 dst_page_itr.next();
100 }
101
102 in1_doc.save(output_path + "newsletter_page_insert.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
103 // output PDF in1_doc
104 System.out.println("Done. Result saved in newsletter_page_insert.pdf...");
105 }
106 } catch (Exception e) {
107 System.out.println(e);
108 }
109
110 // Sample 5 - Replicate pages within a single document
111 System.out.println("_______________________________________________");
112 System.out.println("Sample 5 - Replicate pages within a single document...");
113 System.out.println("Opening the input pdf...");
114 try (PDFDoc 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 Page cover = doc.getPage(1);
120 PageIterator 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", SDFDoc.SaveMode.NO_FLAGS, null);
131 System.out.println("Done. Result saved in newsletter_page_clone.pdf...");
132
133 } catch (Exception e2) {
134 System.out.println(e2);
135 }
136
137 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
138 // in order to preserve shared resources between pages (e.g. images, fonts,
139 // colorspaces, etc.)
140 System.out.println("_______________________________________________");
141 System.out.println("Sample 6 - Preserving shared resources using ImportPages...");
142 System.out.println("Opening the input pdf...");
143 try (PDFDoc in_doc = new PDFDoc((input_path + "newsletter.pdf"))) {
144 in_doc.initSecurityHandler();
145
146 try (PDFDoc new_doc = new PDFDoc()) {
147
148 Page[] copy_pages = new Page[in_doc.getPageCount()];
149 int j = 0;
150 for (PageIterator itr = in_doc.getPageIterator(); itr.hasNext(); j++) {
151 copy_pages[j] = itr.next();
152 }
153
154 Page[] imported_pages = new_doc.importPages(copy_pages);
155 for (int i = 0; i < imported_pages.length; ++i) {
156 new_doc.pagePushFront(imported_pages[i]); // Order pages in reverse order.
157 // Use pushBackPage() if you would like to preserve the same order.
158 }
159
160 new_doc.save(output_path + "newsletter_import_pages.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
161
162 System.out.println("Done. Result saved in newsletter_import_pages.pdf...");
163 System.out.println();
164 System.out.println("Note that the output file size is less than half the size");
165 System.out.println("of the file produced using individual page copy operations");
166 System.out.println("between two documents");
167 }
168 } catch (Exception e1) {
169 System.out.println(e1);
170 }
171
172 PDFNet.terminate();
173 }
174}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales