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 Xamarin SDK and PDF Editing & Manipulation Library.
1//
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Collections;
8using pdftron;
9using pdftron.Common;
10using pdftron.Filters;
11using pdftron.SDF;
12using pdftron.PDF;
13
14using NUnit.Framework;
15
16namespace MiscellaneousSamples
17{
18 /// <summary>
19 /// Summary description for Class1.
20 /// </summary>
21 [TestFixture]
22 public class PDFPageTest
23 {
24
25 /// <summary>
26 /// The main entry point for the application.
27 /// </summary>
28 [Test]
29 public static void Sample()
30 {
31
32 // Relative path to the folder containing test files.
33 const string input_path = "TestFiles/";
34
35 // Sample 1 - Split a PDF document into multiple pages
36 try
37 {
38 Console.WriteLine("_______________________________________________");
39 Console.WriteLine("Sample 1 - Split a PDF document into multiple pages...");
40 Console.WriteLine("Opening the input pdf...");
41
42 using (PDFDoc in_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "newsletter.pdf")))
43 {
44 in_doc.InitSecurityHandler();
45
46 int page_num = in_doc.GetPageCount();
47 for (int i = 1; i <= page_num; ++i)
48 {
49 using (PDFDoc new_doc = new PDFDoc())
50 {
51 new_doc.InsertPages(0, in_doc, i, i, PDFDoc.InsertFlag.e_none);
52 new_doc.Save(Utils.CreateExternalFile("newsletter_split_page_") + i + ".pdf", SDFDoc.SaveOptions.e_remove_unused);
53 Console.WriteLine("Done. Result saved in newsletter_split_page_" + i + ".pdf");
54 }
55 }
56 }
57 }
58 catch (Exception e)
59 {
60 Console.WriteLine("Exception caught:\n{0}", e);
61 Assert.True(false);
62 }
63
64 // Sample 2 - Merge several PDF documents into one
65 try
66 {
67 Console.WriteLine("_______________________________________________");
68 Console.WriteLine("Sample 2 - Merge several PDF documents into one...");
69
70 using (PDFDoc new_doc = new PDFDoc())
71 {
72 new_doc.InitSecurityHandler();
73 int page_num = 15;
74 for (int i = 1; i <= page_num; ++i)
75 {
76 Console.WriteLine("Opening newsletter_split_page_" + i + ".pdf");
77 using (PDFDoc in_doc = new PDFDoc(Utils.CreateExternalFile("newsletter_split_page_") + i + ".pdf"))
78 {
79 new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDoc.InsertFlag.e_none);
80 }
81 }
82 new_doc.Save(Utils.CreateExternalFile("newsletter_merge_pages.pdf"), SDFDoc.SaveOptions.e_remove_unused);
83 }
84 Console.WriteLine("Done. Result saved in newsletter_merge_pages.pdf");
85 }
86 catch (Exception e)
87 {
88 Console.WriteLine("Exception caught:\n{0}", e);
89 Assert.True(false);
90 }
91
92
93 // Sample 3 - Delete every second page
94 try
95 {
96 Console.WriteLine("_______________________________________________");
97 Console.WriteLine("Sample 3 - Delete every second page...");
98 Console.WriteLine("Opening the input pdf...");
99
100 using (PDFDoc in_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "newsletter.pdf")))
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 in_doc.Save(Utils.CreateExternalFile("newsletter_page_remove.pdf"), 0);
114 }
115 Console.WriteLine("Done. Result saved in newsletter_page_remove.pdf...");
116 }
117 catch(Exception e)
118 {
119 Console.WriteLine("Exception caught:\n{0}", e);
120 Assert.True(false);
121 }
122
123 // Sample 4 - Inserts a page from one document at different
124 // locations within another document
125 try
126 {
127 Console.WriteLine("_______________________________________________");
128 Console.WriteLine("Sample 4 - Insert a page at different locations...");
129 Console.WriteLine("Opening the input pdf...");
130
131 using (PDFDoc in1_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "newsletter.pdf")))
132 using (PDFDoc in2_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "fish.pdf")))
133 {
134 in1_doc.InitSecurityHandler();
135 in2_doc.InitSecurityHandler();
136
137 Page src_page = in2_doc.GetPage(1);
138 PageIterator dst_page = in1_doc.GetPageIterator(1);
139 int page_num = 1;
140 while (dst_page.HasNext()) {
141 if (page_num++ % 3 == 0) {
142 in1_doc.PageInsert(dst_page, src_page);
143 }
144 dst_page.Next();
145 }
146
147 in1_doc.Save(Utils.CreateExternalFile("newsletter_page_insert.pdf"), 0);
148 Console.WriteLine("Done. Result saved in newsletter_page_insert.pdf...");
149 }
150 }
151 catch(Exception e)
152 {
153 Console.WriteLine("Exception caught:\n{0}", e);
154 Assert.True(false);
155 }
156
157 // Sample 5 - Replicate pages within a single document
158 try
159 {
160 Console.WriteLine("_______________________________________________");
161 Console.WriteLine("Sample 5 - Replicate pages within a single document...");
162 Console.WriteLine("Opening the input pdf...");
163 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "newsletter.pdf")))
164 {
165 doc.InitSecurityHandler();
166
167 // Replicate the cover page three times (copy page #1 and place it before the
168 // seventh page in the document page sequence)
169 Page cover = doc.GetPage(1);
170 PageIterator p7 = doc.GetPageIterator(7);
171 doc.PageInsert(p7, cover);
172 doc.PageInsert(p7, cover);
173 doc.PageInsert(p7, cover);
174
175 // Replicate the cover page two more times by placing it before and after
176 // existing pages.
177 doc.PagePushFront(cover);
178 doc.PagePushBack(cover);
179
180 doc.Save(Utils.CreateExternalFile("newsletter_page_clone.pdf"), 0);
181 Console.WriteLine("Done. Result saved in newsletter_page_clone.pdf...");
182 }
183 }
184 catch(Exception e)
185 {
186 Console.WriteLine("Exception caught:\n{0}", e);
187 Assert.True(false);
188 }
189
190 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
191 // in order to preserve shared resources between pages (e.g. images, fonts,
192 // colorspaces, etc.)
193 try
194 {
195 Console.WriteLine("_______________________________________________");
196 Console.WriteLine("Sample 6 - Preserving shared resources using ImportPages...");
197 Console.WriteLine("Opening the input pdf...");
198 using (PDFDoc in_doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "newsletter.pdf")))
199 {
200 in_doc.InitSecurityHandler();
201 using (PDFDoc new_doc = new PDFDoc())
202 {
203 ArrayList copy_pages = new ArrayList();
204 for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next()) {
205 copy_pages.Add(itr.Current());
206 }
207
208 ArrayList imported_pages = new_doc.ImportPages(copy_pages);
209 for (int i=0; i!=imported_pages.Count; ++i) {
210 new_doc.PagePushFront((Page)imported_pages[i]); // Order pages in reverse order.
211 // Use PagePushBack() if you would like to preserve the same order.
212 }
213
214 new_doc.Save(Utils.CreateExternalFile("newsletter_import_pages.pdf"), 0);
215 Console.WriteLine("Done. Result saved in newsletter_import_pages.pdf...");
216 Console.WriteLine();
217 Console.WriteLine("Note that the output file size is less than half the size");
218 Console.WriteLine("of the file produced using individual page copy operations");
219 Console.WriteLine("between two documents");
220 }
221 }
222 }
223 catch(Exception e)
224 {
225 Console.WriteLine("Exception caught:\n{0}", e);
226 Assert.True(false);
227 }
228 }
229 }
230}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales