PDFPage

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales