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 Server SDK and PDF Editing & Manipulation Library.

1//
2// Copyright (c) 2001-2024 by Apryse Software 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
14namespace PDFPageTestCS
15{
16 /// <summary>
17 /// Summary description for Class1.
18 /// </summary>
19 class Class1
20 {
21 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
22 static Class1() {}
23
24 /// <summary>
25 /// The main entry point for the application.
26 /// </summary>
27 [STAThread]
28 static void Main(string[] args)
29 {
30 PDFNet.Initialize(PDFTronLicense.Key);
31
32 // Relative path to the folder containing test files.
33 string input_path = "../../../../TestFiles/";
34 string output_path = "../../../../TestFiles/Output/";
35
36 // Sample 1 - Split a PDF document into multiple pages
37 try
38 {
39 Console.WriteLine("_______________________________________________");
40 Console.WriteLine("Sample 1 - Split a PDF document into multiple pages...");
41 Console.WriteLine("Opening the input pdf...");
42
43 using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf"))
44 {
45 in_doc.InitSecurityHandler();
46
47 int page_num = in_doc.GetPageCount();
48 for (int i = 1; i <= page_num; ++i)
49 {
50 using (PDFDoc new_doc = new PDFDoc())
51 {
52 new_doc.InsertPages(0, in_doc, i, i, PDFDoc.InsertFlag.e_none);
53 new_doc.Save(output_path + "newsletter_split_page_" + i + ".pdf", SDFDoc.SaveOptions.e_remove_unused);
54 Console.WriteLine("Done. Result saved in newsletter_split_page_" + i + ".pdf");
55 }
56 }
57 }
58 }
59 catch (Exception e)
60 {
61 Console.WriteLine("Exception caught:\n{0}", e);
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(output_path + "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(output_path + "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 }
90
91
92 // Sample 3 - Delete every second page
93 try
94 {
95 Console.WriteLine("_______________________________________________");
96 Console.WriteLine("Sample 3 - Delete every second page...");
97 Console.WriteLine("Opening the input pdf...");
98
99 using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf"))
100 {
101 in_doc.InitSecurityHandler();
102
103 int page_num = in_doc.GetPageCount();
104 PageIterator itr;
105 while (page_num>=1)
106 {
107 itr = in_doc.GetPageIterator(page_num);
108 in_doc.PageRemove(itr);
109 page_num -= 2;
110 }
111
112 in_doc.Save(output_path + "newsletter_page_remove.pdf", 0);
113 }
114 Console.WriteLine("Done. Result saved in newsletter_page_remove.pdf...");
115 }
116 catch(Exception e)
117 {
118 Console.WriteLine("Exception caught:\n{0}", e);
119 }
120
121 // Sample 4 - Inserts a page from one document at different
122 // locations within another document
123 try
124 {
125 Console.WriteLine("_______________________________________________");
126 Console.WriteLine("Sample 4 - Insert a page at different locations...");
127 Console.WriteLine("Opening the input pdf...");
128
129 using (PDFDoc in1_doc = new PDFDoc(input_path + "newsletter.pdf"))
130 using (PDFDoc in2_doc = new PDFDoc(input_path + "fish.pdf"))
131 {
132 in1_doc.InitSecurityHandler();
133 in2_doc.InitSecurityHandler();
134
135 Page src_page = in2_doc.GetPage(1);
136 PageIterator dst_page = in1_doc.GetPageIterator(1);
137 int page_num = 1;
138 while (dst_page.HasNext()) {
139 if (page_num++ % 3 == 0) {
140 in1_doc.PageInsert(dst_page, src_page);
141 }
142 dst_page.Next();
143 }
144
145 in1_doc.Save(output_path + "newsletter_page_insert.pdf", 0);
146 Console.WriteLine("Done. Result saved in newsletter_page_insert.pdf...");
147 }
148 }
149 catch(Exception e)
150 {
151 Console.WriteLine("Exception caught:\n{0}", e);
152 }
153
154 // Sample 5 - Replicate pages within a single document
155 try
156 {
157 Console.WriteLine("_______________________________________________");
158 Console.WriteLine("Sample 5 - Replicate pages within a single document...");
159 Console.WriteLine("Opening the input pdf...");
160 using (PDFDoc doc = new PDFDoc(input_path + "newsletter.pdf"))
161 {
162 doc.InitSecurityHandler();
163
164 // Replicate the cover page three times (copy page #1 and place it before the
165 // seventh page in the document page sequence)
166 Page cover = doc.GetPage(1);
167 PageIterator p7 = doc.GetPageIterator(7);
168 doc.PageInsert(p7, cover);
169 doc.PageInsert(p7, cover);
170 doc.PageInsert(p7, cover);
171
172 // Replicate the cover page two more times by placing it before and after
173 // existing pages.
174 doc.PagePushFront(cover);
175 doc.PagePushBack(cover);
176
177 doc.Save(output_path + "newsletter_page_clone.pdf", 0);
178 Console.WriteLine("Done. Result saved in newsletter_page_clone.pdf...");
179 }
180 }
181 catch(Exception e)
182 {
183 Console.WriteLine("Exception caught:\n{0}", e);
184 }
185
186 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
187 // in order to preserve shared resources between pages (e.g. images, fonts,
188 // colorspaces, etc.)
189 try
190 {
191 Console.WriteLine("_______________________________________________");
192 Console.WriteLine("Sample 6 - Preserving shared resources using ImportPages...");
193 Console.WriteLine("Opening the input pdf...");
194 using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf"))
195 {
196 in_doc.InitSecurityHandler();
197 using (PDFDoc new_doc = new PDFDoc())
198 {
199 ArrayList copy_pages = new ArrayList();
200 for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next()) {
201 copy_pages.Add(itr.Current());
202 }
203
204 ArrayList imported_pages = new_doc.ImportPages(copy_pages);
205 for (int i=0; i!=imported_pages.Count; ++i) {
206 new_doc.PagePushFront((Page)imported_pages[i]); // Order pages in reverse order.
207 // Use PagePushBack() if you would like to preserve the same order.
208 }
209
210 new_doc.Save(output_path + "newsletter_import_pages.pdf", 0);
211 Console.WriteLine("Done. Result saved in newsletter_import_pages.pdf...");
212 Console.WriteLine();
213 Console.WriteLine("Note that the output file size is less than half the size");
214 Console.WriteLine("of the file produced using individual page copy operations");
215 Console.WriteLine("between two documents");
216 }
217 }
218 }
219 catch(Exception e)
220 {
221 Console.WriteLine("Exception caught:\n{0}", e);
222 }
223 PDFNet.Terminate();
224 }
225 }
226}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales