PDFPage

Sample C# 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 UWP SDK and PDF Editing & Manipulation Library.

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.PDF;
11using pdftron.SDF;
12
13using PDFNetUniversalSamples.ViewModels;
14using System.Collections.Generic;
15
16namespace PDFNetSamples
17{
18 public sealed class PDFPageTest : Sample
19 {
20 public PDFPageTest() :
21 base("PDFPage", "The sample illustrates how to copy pages from one document to another, how to delete, and re-arrange pages and how to use ImportPages() method for very efficient copy and merge operations.")
22 {
23 }
24
25 public override IAsyncAction RunAsync()
26 {
27 return Task.Run(new System.Action(async () => {
28 WriteLine("--------------------------------");
29 WriteLine("Starting PDFPage Test...");
30 WriteLine("--------------------------------\n");
31 // Sample 1 - Split a PDF document into multiple pages
32 try
33 {
34 WriteLine("_______________________________________________");
35 WriteLine("Sample 1 - Split a PDF document into multiple pages...");
36 string input_file_path = Path.Combine(InputPath, "newsletter.pdf");
37
38 WriteLine("Opening input file " + input_file_path);
39
40 using (PDFDoc in_doc = new PDFDoc(input_file_path))
41 {
42 int page_num = in_doc.GetPageCount();
43 for (int i = 1; i <= page_num; ++i)
44 {
45 using (PDFDoc new_doc = new PDFDoc())
46 {
47 new_doc.InsertPages(0, in_doc, i, i, PDFDocInsertFlag.e_none);
48 String output_file_path = Path.Combine(OutputPath, "newsletter_split_page_" + i + ".pdf");
49 await new_doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_remove_unused);
50 WriteLine("Done. Results saved in " + output_file_path);
51 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
52 }
53 }
54 }
55 }
56 catch (Exception e)
57 {
58 WriteLine(GetExceptionMessage(e));
59 }
60
61 // Sample 2 - Merge several PDF documents into one
62 try
63 {
64 WriteLine("_______________________________________________");
65 WriteLine("Sample 2 - Merge several PDF documents into one...");
66
67 using (PDFDoc new_doc = new PDFDoc())
68 {
69 new_doc.InitSecurityHandler();
70 int page_num = 15;
71 for (int i = 1; i <= page_num; ++i)
72 {
73 String fpath = Path.Combine(OutputPath, "newsletter_split_page_" + i + ".pdf");
74 WriteLine("Opening " + fpath);
75 using (PDFDoc in_doc = new PDFDoc(fpath))
76 {
77 new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDocInsertFlag.e_none);
78 }
79 }
80 String output_file_path = Path.Combine(OutputPath, "newsletter_merge_pages.pdf");
81 await new_doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_remove_unused);
82 WriteLine("Done. Results saved in " + output_file_path);
83 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
84 }
85 }
86 catch (Exception e)
87 {
88 WriteLine(GetExceptionMessage(e));
89 }
90
91 // Sample 3 - Delete every second page
92 try
93 {
94 WriteLine("_______________________________________________");
95 WriteLine("Sample 3 - Delete every second page...");
96
97 string input_file_path = Path.Combine(InputPath, "newsletter.pdf");
98 WriteLine("Opening input file " + input_file_path);
99
100 using (PDFDoc in_doc = new PDFDoc(input_file_path))
101 {
102 in_doc.InitSecurityHandler();
103
104 int page_num = in_doc.GetPageCount();
105 PageIterator itr;
106 while (page_num >= 1)
107 {
108 itr = in_doc.GetPageIterator(page_num);
109 in_doc.PageRemove(itr);
110 page_num -= 2;
111 }
112
113 String output_file_path = Path.Combine(OutputPath, "newsletter_page_remove.pdf");
114 await in_doc.SaveAsync(output_file_path, 0);
115 WriteLine("Done. Results saved in " + output_file_path);
116 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
117 }
118 }
119 catch(Exception e)
120 {
121 WriteLine(GetExceptionMessage(e));
122 }
123
124 // Sample 4 - Inserts a page from one document at different
125 // locations within another document
126 try
127 {
128 WriteLine("_______________________________________________");
129 WriteLine("Sample 4 - Insert a page at different locations...");
130
131 string input_file_path = Path.Combine(InputPath, "newsletter.pdf");
132 WriteLine("Opening input file " + input_file_path);
133
134 PDFDoc in1_doc = new PDFDoc(input_file_path);
135 in1_doc.InitSecurityHandler();
136
137 string input_file_path_2 = Path.Combine(InputPath, "fish.pdf");
138 WriteLine("Opening input file " + input_file_path_2);
139
140 PDFDoc in2_doc = new PDFDoc(input_file_path_2);
141 in2_doc.InitSecurityHandler();
142
143 pdftron.PDF.Page src_page = in2_doc.GetPage(1);
144 int page_num = in1_doc.GetPageCount();
145 for (int i=1; i<page_num; i+=3)
146 {
147 PageIterator it = in1_doc.GetPageIterator(i);
148 in1_doc.PageInsert(it, src_page);
149 }
150;
151 String output_file_path = Path.Combine(OutputPath, "newsletter_page_insert.pdf");
152 await in1_doc.SaveAsync(output_file_path, 0);
153 WriteLine("Done. Results saved in " + output_file_path);
154 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
155
156 in1_doc.Destroy();
157 in2_doc.Destroy();
158 }
159 catch(Exception e)
160 {
161 WriteLine(GetExceptionMessage(e));
162 }
163
164 // Sample 5 - Replicate pages within a single document
165 try
166 {
167 WriteLine("_______________________________________________");
168 WriteLine("Sample 5 - Replicate pages within a single document...");
169
170 string input_file_path = Path.Combine(InputPath, "newsletter.pdf");
171 WriteLine("Opening input file " + input_file_path);
172
173 using (PDFDoc doc = new PDFDoc(input_file_path))
174 {
175 doc.InitSecurityHandler();
176
177 // Replicate the cover page three times (copy page #1 and place it before the
178 // seventh page in the document page sequence)
179 pdftron.PDF.Page cover = doc.GetPage(1);
180 doc.PageInsert(doc.GetPageIterator(7), cover);
181 doc.PageInsert(doc.GetPageIterator(7), cover);
182 doc.PageInsert(doc.GetPageIterator(7), cover);
183
184 // Replicate the cover page two more times by placing it before and after
185 // existing pages.
186 doc.PagePushFront(cover);
187 doc.PagePushBack(cover);
188
189 String output_file_path = Path.Combine(OutputPath, "newsletter_page_clone.pdf");
190 await doc.SaveAsync(output_file_path, 0);
191 WriteLine("Done. Results saved in " + output_file_path);
192 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
193 }
194 }
195 catch(Exception e)
196 {
197 WriteLine(GetExceptionMessage(e));
198 }
199
200 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
201 // in order to preserve shared resources between pages (e.g. images, fonts,
202 // colorspaces, etc.)
203 try
204 {
205 WriteLine("_______________________________________________");
206 WriteLine("Sample 6 - Preserving shared resources using ImportPages...");
207
208 string input_file_path = Path.Combine(InputPath, "newsletter.pdf");
209 WriteLine("Opening input file " + input_file_path);
210
211 using (PDFDoc in_doc = new PDFDoc(input_file_path))
212 {
213 in_doc.InitSecurityHandler();
214 using (PDFDoc new_doc = new PDFDoc())
215 {
216 IList<pdftron.PDF.Page> copy_pages = new List<pdftron.PDF.Page>();
217 for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next())
218 {
219 copy_pages.Add(itr.Current());
220 }
221
222 IList<pdftron.PDF.Page> imported_pages = new_doc.ImportPages(copy_pages);
223 for (int i=0; i!=imported_pages.Count; ++i) {
224 new_doc.PagePushFront((pdftron.PDF.Page)imported_pages[i]); // Order pages in reverse order.
225 // Use PagePushBack() if you would like to preserve the same order.
226 }
227
228 String output_file_path = Path.Combine(OutputPath, "newsletter_import_pages.pdf");
229 await new_doc.SaveAsync(output_file_path, 0);
230 WriteLine("Done. Result saved in " + output_file_path);
231 WriteLine("\nNote that the output file size is less than half the size");
232 WriteLine("of the file produced using individual page copy operations");
233 WriteLine("between two documents");
234 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
235 }
236 }
237 }
238 catch(Exception e)
239 {
240 WriteLine(GetExceptionMessage(e));
241 }
242
243 WriteLine("\n--------------------------------");
244 WriteLine("Done PDFPage Test.");
245 WriteLine("--------------------------------\n");
246 })).AsAsyncAction();
247 }
248 }
249}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales