Sample Java 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. Learn more about our Android SDK and PDF Editing & Manipulation Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.pdf.PDFDoc;
13import com.pdftron.pdf.Page;
14import com.pdftron.pdf.PageIterator;
15import com.pdftron.sdf.SDFDoc;
16
17import java.util.ArrayList;
18
19public class PDFPageTest extends PDFNetSample {
20
21 private static OutputListener mOutputListener;
22
23 private static ArrayList<String> mFileList = new ArrayList<>();
24
25 public PDFPageTest() {
26 setTitle(R.string.sample_pdfpage_title);
27 setDescription(R.string.sample_pdfpage_description);
28 }
29
30 @Override
31 public void run(OutputListener outputListener) {
32 super.run(outputListener);
33 mOutputListener = outputListener;
34 mFileList.clear();
35 printHeader(outputListener);
36
37 // Sample 1 - Split a PDF document into multiple pages
38 mOutputListener.println("_______________________________________________");
39 mOutputListener.println("Sample 1 - Split a PDF document into multiple pages...");
40 mOutputListener.println("Opening the input pdf...");
41 try (PDFDoc in_doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath())) {
42 in_doc.initSecurityHandler();
43
44 int page_num = in_doc.getPageCount();
45 for (int i = 1; i <= page_num; ++i) {
46 try (PDFDoc new_doc = new PDFDoc()) {
47 new_doc.insertPages(0, in_doc, i, i, PDFDoc.InsertBookmarkMode.NONE, null);
48 String fname = "newsletter_split_page_" + i + ".pdf";
49 new_doc.save(Utils.createExternalFile(fname, mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
50 mOutputListener.println("Done. Result saved in newsletter_split_page_" + i + ".pdf");
51 }
52 }
53 } catch (Exception e2) {
54 mOutputListener.printError(e2.getStackTrace());
55 }
56
57 // Sample 2 - Merge several PDF documents into one
58 mOutputListener.println("_______________________________________________");
59 mOutputListener.println("Sample 2 - Merge several PDF documents into one...");
60 try (PDFDoc new_doc = new PDFDoc()) {
61 new_doc.initSecurityHandler();
62
63 int page_num = 15;
64 for (int i = 1; i <= page_num; ++i) {
65 mOutputListener.println("Opening newsletter_split_page_" + i + ".pdf");
66 String fname = "newsletter_split_page_" + i + ".pdf";
67 try (PDFDoc in_doc = new PDFDoc(Utils.createExternalFile(fname, mFileList).getAbsolutePath())) {
68 new_doc.insertPages(i, in_doc, 1, in_doc.getPageCount(), PDFDoc.InsertBookmarkMode.NONE, null);
69 }
70 }
71 new_doc.save(Utils.createExternalFile("newsletter_merge_pages.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.REMOVE_UNUSED, null);
72 mOutputListener.println("Done. Result saved in newsletter_merge_pages.pdf");
73 } catch (Exception e2) {
74 mOutputListener.printError(e2.getStackTrace());
75 }
76
77 // Sample 3 - Delete every second page
78 mOutputListener.println("_______________________________________________");
79 mOutputListener.println("Sample 3 - Delete every second page...");
80 mOutputListener.println("Opening the input pdf...");
81 try (PDFDoc in_doc = new PDFDoc(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath())) {
82 in_doc.initSecurityHandler();
83
84 int page_num = in_doc.getPageCount();
85 while (page_num >= 1) {
86 PageIterator itr = in_doc.getPageIterator(page_num);
87 in_doc.pageRemove(itr);
88 page_num -= 2;
89 }
90
91 in_doc.save(Utils.createExternalFile("newsletter_page_remove.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null);
92 mOutputListener.println("Done. Result saved in newsletter_page_remove.pdf...");
93
94 } catch (Exception e2) {
95 mOutputListener.printError(e2.getStackTrace());
96 }
97
98 // Sample 4 - Inserts a page from one document at different
99 // locations within another document
100 mOutputListener.println("_______________________________________________");
101 mOutputListener.println("Sample 4 - Insert a page at different locations...");
102 mOutputListener.println("Opening the input pdf...");
103 try (PDFDoc in1_doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath()))) {
104 in1_doc.initSecurityHandler();
105 try (PDFDoc in2_doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "fish.pdf").getAbsolutePath()))) {
106 in2_doc.initSecurityHandler();
107
108 PageIterator src_page_itr = in2_doc.getPageIterator();
109 Page src_page = src_page_itr.next();
110 PageIterator dst_page_itr = in1_doc.getPageIterator();
111 int page_num = 1;
112 while (dst_page_itr.hasNext()) {
113 if (page_num++ % 3 == 0) {
114 in1_doc.pageInsert(dst_page_itr, src_page);
115 }
116 dst_page_itr.next();
117 }
118
119 in1_doc.save(Utils.createExternalFile("newsletter_page_insert.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null);
120 mOutputListener.println("Done. Result saved in newsletter_page_insert.pdf...");
121 }
122 } catch (Exception e) {
123 mOutputListener.printError(e.getStackTrace());
124 }
125
126 // Sample 5 - Replicate pages within a single document
127 mOutputListener.println("_______________________________________________");
128 mOutputListener.println("Sample 5 - Replicate pages within a single document...");
129 mOutputListener.println("Opening the input pdf...");
130 try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath()))) {
131 doc.initSecurityHandler();
132
133 // Replicate the cover page three times (copy page #1 and place it before the
134 // seventh page in the document page sequence)
135 Page cover = doc.getPage(1);
136 PageIterator p7 = doc.getPageIterator(7);
137 doc.pageInsert(p7, cover);
138 doc.pageInsert(p7, cover);
139 doc.pageInsert(p7, cover);
140
141 // Replicate the cover page two more times by placing it before and after
142 // existing pages.
143 doc.pagePushFront(cover);
144 doc.pagePushBack(cover);
145
146 doc.save(Utils.createExternalFile("newsletter_page_clone.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null);
147 mOutputListener.println("Done. Result saved in newsletter_page_clone.pdf...");
148
149 } catch (Exception e2) {
150 mOutputListener.printError(e2.getStackTrace());
151 }
152
153 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
154 // in order to preserve shared resources between pages (e.g. images, fonts,
155 // colorspaces, etc.)
156 mOutputListener.println("_______________________________________________");
157 mOutputListener.println("Sample 6 - Preserving shared resources using ImportPages...");
158 mOutputListener.println("Opening the input pdf...");
159 try (PDFDoc in_doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath()))) {
160 in_doc.initSecurityHandler();
161
162 try (PDFDoc new_doc = new PDFDoc()) {
163
164 Page[] copy_pages = new Page[in_doc.getPageCount()];
165 int j = 0;
166 for (PageIterator itr = in_doc.getPageIterator(); itr.hasNext(); j++) {
167 copy_pages[j] = itr.next();
168 }
169
170 Page[] imported_pages = new_doc.importPages(copy_pages);
171 for (int i = 0; i < imported_pages.length; ++i) {
172 new_doc.pagePushFront(imported_pages[i]); // Order pages in reverse order.
173 // Use pushBackPage() if you would like to preserve the same order.
174 }
175
176 new_doc.save(Utils.createExternalFile("newsletter_import_pages.pdf", mFileList).getAbsolutePath(), SDFDoc.SaveMode.NO_FLAGS, null);
177
178 mOutputListener.println("Done. Result saved in newsletter_import_pages.pdf...");
179 mOutputListener.println();
180 mOutputListener.println("Note that the output file size is less than half the size");
181 mOutputListener.println("of the file produced using individual page copy operations");
182 mOutputListener.println("between two documents");
183 }
184 } catch (Exception e1) {
185 mOutputListener.printError(e1.getStackTrace());
186 }
187
188 for (String file : mFileList) {
189 addToFileList(file);
190 }
191 printFooter(outputListener);
192 }
193
194}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample
10import com.pdftron.android.pdfnetsdksamples.R
11import com.pdftron.android.pdfnetsdksamples.util.Utils
12import com.pdftron.pdf.PDFDoc
13import com.pdftron.pdf.Page
14import com.pdftron.sdf.SDFDoc
15import java.util.*
16
17class PDFPageTest : PDFNetSample() {
18 init {
19 setTitle(R.string.sample_pdfpage_title)
20 setDescription(R.string.sample_pdfpage_description)
21 }
22
23 override fun run(outputListener: OutputListener?) {
24 super.run(outputListener)
25 mOutputListener = outputListener
26 mFileList.clear()
27 printHeader(outputListener!!)
28
29 // Sample 1 - Split a PDF document into multiple pages
30 try {
31 mOutputListener!!.println("_______________________________________________")
32 mOutputListener!!.println("Sample 1 - Split a PDF document into multiple pages...")
33 mOutputListener!!.println("Opening the input pdf...")
34 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { in_doc ->
35 in_doc.initSecurityHandler()
36
37 val page_num = in_doc.pageCount
38 for (i in 1..page_num) {
39 PDFDoc().use { new_doc ->
40 new_doc.insertPages(0, in_doc, i, i, PDFDoc.InsertBookmarkMode.NONE, null)
41 val fname = "newsletter_split_page_$i.pdf"
42 new_doc.save(Utils.createExternalFile(fname, PDFPageTest.mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
43 mOutputListener!!.println("Done. Result saved in newsletter_split_page_$i.pdf")
44 }
45 }
46 }
47 } catch (e2: Exception) {
48 mOutputListener!!.printError(e2.stackTrace)
49 }
50
51 // Sample 2 - Merge several PDF documents into one
52 try {
53 mOutputListener!!.println("_______________________________________________")
54 mOutputListener!!.println("Sample 2 - Merge several PDF documents into one...")
55 PDFDoc().use { new_doc ->
56 new_doc.initSecurityHandler()
57
58 val page_num = 15
59 for (i in 1..page_num) {
60 mOutputListener!!.println("Opening newsletter_split_page_$i.pdf")
61 val fname = "newsletter_split_page_$i.pdf"
62 PDFDoc(Utils.createExternalFile(fname, PDFPageTest.mFileList).absolutePath).use { in_doc ->
63 new_doc.insertPages(i, in_doc, 1, in_doc.pageCount, PDFDoc.InsertBookmarkMode.NONE, null)
64 }
65 }
66 new_doc.save(Utils.createExternalFile("newsletter_merge_pages.pdf", mFileList).absolutePath, SDFDoc.SaveMode.REMOVE_UNUSED, null)
67 mOutputListener!!.println("Done. Result saved in newsletter_merge_pages.pdf")
68 }
69 } catch (e2: Exception) {
70 mOutputListener!!.printError(e2.stackTrace)
71 }
72
73 // Sample 3 - Delete every second page
74 try {
75 mOutputListener!!.println("_______________________________________________")
76 mOutputListener!!.println("Sample 3 - Delete every second page...")
77 mOutputListener!!.println("Opening the input pdf...")
78 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { in_doc ->
79 in_doc.initSecurityHandler()
80
81 var page_num = in_doc.pageCount
82 while (page_num >= 1) {
83 val itr = in_doc.getPageIterator(page_num)
84 in_doc.pageRemove(itr)
85 page_num -= 2
86 }
87
88 in_doc.save(Utils.createExternalFile("newsletter_page_remove.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null)
89 mOutputListener!!.println("Done. Result saved in newsletter_page_remove.pdf...")
90
91 }
92 } catch (e2: Exception) {
93 mOutputListener!!.printError(e2.stackTrace)
94 }
95
96 // Sample 4 - Inserts a page from one document at different
97 // locations within another document
98 try {
99 mOutputListener!!.println("_______________________________________________")
100 mOutputListener!!.println("Sample 4 - Insert a page at different locations...")
101 mOutputListener!!.println("Opening the input pdf...")
102
103 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { in1_doc ->
104 in1_doc.initSecurityHandler()
105
106 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "fish.pdf")!!.absolutePath).use { in2_doc ->
107 in2_doc.initSecurityHandler()
108
109 val src_page_itr = in2_doc.pageIterator
110 val src_page = src_page_itr.next()
111 val dst_page_itr = in1_doc.pageIterator
112 var page_num = 1
113 while (dst_page_itr.hasNext()) {
114 if (page_num++ % 3 == 0) {
115 in1_doc.pageInsert(dst_page_itr, src_page)
116 }
117 dst_page_itr.next()
118 }
119
120 in1_doc.save(Utils.createExternalFile("newsletter_page_insert.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null)
121 mOutputListener!!.println("Done. Result saved in newsletter_page_insert.pdf...")
122 }
123 }
124 } catch (e2: Exception) {
125 mOutputListener!!.printError(e2.stackTrace)
126 }
127
128 // Sample 5 - Replicate pages within a single document
129 try {
130 mOutputListener!!.println("_______________________________________________")
131 mOutputListener!!.println("Sample 5 - Replicate pages within a single document...")
132 mOutputListener!!.println("Opening the input pdf...")
133 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { doc ->
134 doc.initSecurityHandler()
135
136 // Replicate the cover page three times (copy page #1 and place it before the
137 // seventh page in the document page sequence)
138 val cover = doc.getPage(1)
139 val p7 = doc.getPageIterator(7)
140 doc.pageInsert(p7, cover)
141 doc.pageInsert(p7, cover)
142 doc.pageInsert(p7, cover)
143
144 // Replicate the cover page two more times by placing it before and after
145 // existing pages.
146 doc.pagePushFront(cover)
147 doc.pagePushBack(cover)
148
149 doc.save(Utils.createExternalFile("newsletter_page_clone.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null)
150 mOutputListener!!.println("Done. Result saved in newsletter_page_clone.pdf...")
151 }
152 } catch (e2: Exception) {
153 mOutputListener!!.printError(e2.stackTrace)
154 }
155
156 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
157 // in order to preserve shared resources between pages (e.g. images, fonts,
158 // colorspaces, etc.)
159 try {
160 mOutputListener!!.println("_______________________________________________")
161 mOutputListener!!.println("Sample 6 - Preserving shared resources using ImportPages...")
162 mOutputListener!!.println("Opening the input pdf...")
163 PDFDoc(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath).use { in_doc ->
164 in_doc.initSecurityHandler()
165
166 PDFDoc().use { new_doc ->
167 val copy_pages = arrayOfNulls<Page>(in_doc.pageCount)
168 var j = 0
169 val itr = in_doc.pageIterator
170 while (itr.hasNext()) {
171 copy_pages[j] = itr.next()
172 j++
173 }
174
175 val imported_pages = new_doc.importPages(copy_pages)
176 for (i in imported_pages.indices) {
177 new_doc.pagePushFront(imported_pages[i]) // Order pages in reverse order.
178 // Use pushBackPage() if you would like to preserve the same order.
179 }
180
181 new_doc.save(Utils.createExternalFile("newsletter_import_pages.pdf", mFileList).absolutePath, SDFDoc.SaveMode.NO_FLAGS, null)
182
183 mOutputListener!!.println("Done. Result saved in newsletter_import_pages.pdf...")
184 mOutputListener!!.println()
185 mOutputListener!!.println("Note that the output file size is less than half the size")
186 mOutputListener!!.println("of the file produced using individual page copy operations")
187 mOutputListener!!.println("between two documents")
188 }
189 }
190 } catch (e1: Exception) {
191 mOutputListener!!.printError(e1.stackTrace)
192 }
193
194 for (file in mFileList) {
195 addToFileList(file)
196 }
197 printFooter(outputListener)
198 }
199
200 companion object {
201
202 private var mOutputListener: OutputListener? = null
203
204 private val mFileList = ArrayList<String>()
205 }
206
207}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales