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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5#include <PDF/PDFNet.h>
6#include <PDF/PDFDoc.h>
7#include <PDF/PDFDocInfo.h>
8#include <iostream>
9#include "../../LicenseKey/CPP/LicenseKey.h"
10
11using namespace std;
12
13using namespace pdftron;
14using namespace SDF;
15using namespace PDF;
16using namespace Filters;
17
18
19int main(int argc, char *argv[])
20{
21 int ret = 0;
22 PDFNet::Initialize(LicenseKey);
23
24 // Relative path to the folder containing test files.
25 string input_path = "../../TestFiles/";
26 string output_path = "../../TestFiles/Output/";
27
28 // Sample 1 - Split a PDF document into multiple pages
29 try
30 {
31 cout << "_______________________________________________" << endl;
32 cout << "Sample 1 - Split a PDF document into multiple pages..." << endl;
33 cout << "Opening the input pdf..." << endl;
34 PDFDoc in_doc((input_path + "newsletter.pdf").c_str());
35 in_doc.InitSecurityHandler();
36
37 int page_num = in_doc.GetPageCount();
38 for (int i=1; i<=page_num; ++i)
39 {
40 PDFDoc new_doc;
41 char fname[256];
42 sprintf(fname, "newsletter_split_page_%d.pdf", i);
43 string output_file(output_path + fname);
44 new_doc.InsertPages(0, in_doc, i, i, PDFDoc::e_none);
45 new_doc.Save(output_file, SDFDoc::e_remove_unused, 0);
46 cout << "Done. Result saved in " << fname << endl;
47 }
48 }
49 catch(Common::Exception& e)
50 {
51 cout << e << endl;
52 ret = 1;
53 }
54 catch(...)
55 {
56 cout << "Unknown Exception" << endl;
57 ret = 1;
58 }
59
60 // Sample 2 - Merge several PDF documents into one
61 try
62 {
63 cout << "_______________________________________________" << endl;
64 cout << "Sample 2 - Merge several PDF documents into one..." << endl;
65 PDFDoc new_doc;
66 new_doc.InitSecurityHandler();
67
68 int page_num = 15;
69 for (int i=1; i<=page_num; ++i)
70 {
71 char fname[256];
72 sprintf(fname, "newsletter_split_page_%d.pdf", i);
73 string input_file(output_path + fname);
74 cout << "Opening " << fname << endl;
75 PDFDoc in_doc(input_file);
76 new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDoc::e_none);
77 }
78 new_doc.Save(output_path + "newsletter_merge_pages.pdf", SDFDoc::e_remove_unused, 0);
79 cout << "Done. Result saved in newsletter_merge_pages.pdf" << endl;
80 }
81 catch(Common::Exception& e)
82 {
83 cout << e << endl;
84 ret = 1;
85 }
86 catch(...)
87 {
88 cout << "Unknown Exception" << endl;
89 ret = 1;
90 }
91
92
93 // Sample 3 - Delete every second page
94 try
95 {
96 cout << "_______________________________________________" << endl;
97 cout << "Sample 3 - Delete every second page..." << endl;
98 cout << "Opening the input pdf..." << endl;
99 PDFDoc in_doc((input_path + "newsletter.pdf").c_str());
100 in_doc.InitSecurityHandler();
101
102 int page_num = in_doc.GetPageCount();
103 while (page_num>=1)
104 {
105 PageIterator itr = in_doc.GetPageIterator(page_num);
106 in_doc.PageRemove(itr);
107 page_num -= 2;
108 }
109
110 in_doc.Save((output_path + "newsletter_page_remove.pdf").c_str(), 0, 0);
111 cout << "Done. Result saved in newsletter_page_remove.pdf..." << endl;
112 }
113 catch(Common::Exception& e)
114 {
115 cout << e << endl;
116 ret = 1;
117 }
118 catch(...)
119 {
120 cout << "Unknown Exception" << endl;
121 ret = 1;
122 }
123
124 // Sample 4 - Inserts a page from one document at different
125 // locations within another document
126 try
127 {
128 cout << "_______________________________________________" << endl;
129 cout << "Sample 4 - Insert a page at different locations..." << endl;
130 cout << "Opening the input pdf..." << endl;
131
132 PDFDoc in1_doc((input_path + "newsletter.pdf").c_str());
133 in1_doc.InitSecurityHandler();
134
135 PDFDoc in2_doc((input_path + "fish.pdf").c_str());
136 in2_doc.InitSecurityHandler();
137
138 PageIterator src_page = in2_doc.GetPageIterator();
139 PageIterator dst_page = in1_doc.GetPageIterator();
140 int page_num = 1;
141 while (dst_page.HasNext()) {
142 if (page_num++ % 3 == 0) {
143 in1_doc.PageInsert(dst_page, src_page.Current());
144 }
145 dst_page.Next();
146 }
147 in1_doc.Save((output_path + "newsletter_page_insert.pdf").c_str(), 0 , NULL);
148 cout << "Done. Result saved in newsletter_page_insert.pdf..." << endl;
149 }
150 catch(Common::Exception& e)
151 {
152 cout << e << endl;
153 ret = 1;
154 }
155 catch(...)
156 {
157 cout << "Unknown Exception" << endl;
158 ret = 1;
159 }
160
161 // Sample 5 - Replicate pages within a single document
162 try
163 {
164 cout << "_______________________________________________" << endl;
165 cout << "Sample 5 - Replicate pages within a single document..." << endl;
166 cout << "Opening the input pdf..." << endl;
167 PDFDoc doc((input_path + "newsletter.pdf").c_str());
168 doc.InitSecurityHandler();
169
170 // Replicate the cover page three times (copy page #1 and place it before the
171 // seventh page in the document page sequence)
172 Page cover = doc.GetPage(1);
173 PageIterator p7 = doc.GetPageIterator(7);
174 doc.PageInsert(p7, cover);
175 doc.PageInsert(p7, cover);
176 doc.PageInsert(p7, cover);
177
178 // Replicate the cover page two more times by placing it before and after
179 // existing pages.
180 doc.PagePushFront(cover);
181 doc.PagePushBack(cover);
182
183 doc.Save((output_path + "newsletter_page_clone.pdf").c_str(), 0, NULL);
184 cout << "Done. Result saved in newsletter_page_clone.pdf..." << endl;
185 }
186 catch(Common::Exception& e)
187 {
188 cout << e << endl;
189 ret = 1;
190 }
191 catch(...)
192 {
193 cout << "Unknown Exception" << endl;
194 ret = 1;
195 }
196
197 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
198 // in order to preserve shared resources between pages (e.g. images, fonts,
199 // colorspaces, etc.)
200 try
201 {
202 cout << "_______________________________________________" << endl;
203 cout << "Sample 6 - Preserving shared resources using ImportPages..." << endl;
204 cout << "Opening the input pdf..." << endl;
205 PDFDoc in_doc((input_path + "newsletter.pdf").c_str());
206 in_doc.InitSecurityHandler();
207
208 PDFDoc new_doc;
209
210 vector<Page> copy_pages;
211 for (PageIterator itr=in_doc.GetPageIterator(); itr.HasNext(); itr.Next())
212 {
213 copy_pages.push_back(itr.Current());
214 }
215
216 vector<Page> imported_pages = new_doc.ImportPages(copy_pages);
217 vector<Page>::iterator i;
218 for (i=imported_pages.begin(); i!=imported_pages.end(); ++i)
219 {
220 new_doc.PagePushFront(*i); // Order pages in reverse order.
221 // Use PagePushBack() if you would like to preserve the same order.
222 }
223
224 new_doc.Save((output_path + "newsletter_import_pages.pdf").c_str(), 0, NULL);
225 cout << "Done. Result saved in newsletter_import_pages.pdf..." << endl << endl
226 << "Note that the output file size is less than half the size" << endl
227 << "of the file produced using individual page copy operations" << endl
228 << "between two documents" << endl;
229 }
230 catch(Common::Exception& e)
231 {
232 cout << e << endl;
233 ret = 1;
234 }
235 catch(...)
236 {
237 cout << "Unknown Exception" << endl;
238 ret = 1;
239 }
240
241 PDFNet::Terminate();
242 return ret;
243}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "strconv"
10 . "pdftron"
11)
12
13import "pdftron/Samples/LicenseKey/GO"
14
15func main(){
16 PDFNetInitialize(PDFTronLicense.Key)
17
18 // Relative path to the folder containing the test files.
19 inputPath := "../../TestFiles/"
20 outputPath := "../../TestFiles/Output/"
21
22 // Sample 1 - Split a PDF document into multiple pages
23 fmt.Println("_______________________________________________")
24 fmt.Println("Sample 1 - Split a PDF document into multiple pages...")
25 fmt.Println("Opening the input pdf...")
26 inDoc := NewPDFDoc(inputPath + "newsletter.pdf")
27 inDoc.InitSecurityHandler()
28
29 pageNum := inDoc.GetPageCount()
30 for i := 1; i <= pageNum; i++ {
31 iStr := strconv.Itoa(i)
32 newDoc := NewPDFDoc()
33 newDoc.InsertPages(0, inDoc, i, i, PDFDocE_none)
34 newDoc.Save(outputPath + "newsletter_split_page_" + iStr + ".pdf", uint(SDFDocE_remove_unused))
35 fmt.Println("Done. Result saved in newsletter_split_page_" + iStr + ".pdf")
36 newDoc.Close()
37 }
38 // Close the open document to free up document memory sooner than waiting for the
39 // garbage collector
40 inDoc.Close()
41
42 // Sample 2 - Merge several PDF documents into one
43 fmt.Println("_______________________________________________")
44 fmt.Println("Sample 2 - Merge several PDF documents into one...")
45 newDoc := NewPDFDoc()
46 newDoc.InitSecurityHandler()
47
48 pageNum = 15
49
50 for i := 1; i <= pageNum; i ++ {
51 fmt.Println("Opening newsletter_split_page_" + strconv.Itoa(i) + ".pdf")
52 inDoc = NewPDFDoc(outputPath + "newsletter_split_page_" + strconv.Itoa(i) + ".pdf")
53 newDoc.InsertPages(i, inDoc, 1, inDoc.GetPageCount(), PDFDocE_none)
54 inDoc.Close()
55 }
56 newDoc.Save(outputPath + "newsletter_merge_pages.pdf", uint(SDFDocE_remove_unused))
57 fmt.Println("Done. Result saved in newsletter_merge_pages.pdf");
58
59 // Close the open document to free up document memory sooner than waiting for the
60 // garbage collector
61 inDoc.Close()
62
63 // Sample 3 - Delete every second page
64 fmt.Println("_______________________________________________")
65 fmt.Println("Sample 3 - Delete every second page...")
66 fmt.Println("Opening the input pdf...")
67 inDoc = NewPDFDoc(inputPath + "newsletter.pdf")
68 inDoc.InitSecurityHandler();
69 pageNum = inDoc.GetPageCount()
70
71 for pageNum >= 1 {
72 itr := inDoc.GetPageIterator(uint(pageNum))
73 inDoc.PageRemove(itr)
74 pageNum = pageNum - 2
75 }
76
77 inDoc.Save(outputPath + "newsletter_page_remove.pdf", uint(0))
78 fmt.Println("Done. Result saved in newsletter_page_remove.pdf...")
79
80 // Close the open document to free up document memory sooner than waiting for the
81 // garbage collector
82 inDoc.Close()
83
84 // Sample 4 - Inserts a page from one document at different
85 // locations within another document
86 fmt.Println("_______________________________________________")
87 fmt.Println("Sample 4 - Insert a page at different locations...")
88 fmt.Println("Opening the input pdf...")
89
90 in1Doc := NewPDFDoc(inputPath + "newsletter.pdf")
91 in1Doc.InitSecurityHandler()
92 in2Doc := NewPDFDoc(inputPath + "fish.pdf")
93 in2Doc.InitSecurityHandler()
94
95 srcPage := in2Doc.GetPageIterator()
96 dstPage := in1Doc.GetPageIterator()
97 pageNum = 1
98 for dstPage.HasNext(){
99 if (pageNum % 3 == 0){
100 in1Doc.PageInsert(dstPage, srcPage.Current())
101 }
102 pageNum = pageNum + 1
103 dstPage.Next()
104 }
105 in1Doc.Save(outputPath + "newsletter_page_insert.pdf", uint(0))
106 fmt.Println("Done. Result saved in newsletter_page_insert.pdf...")
107
108 // Close the open document to free up document memory sooner than waiting for the
109 // garbage collector
110 in1Doc.Close()
111 in2Doc.Close()
112
113 // Sample 5 - Replicate pages within a single document
114 fmt.Println("_______________________________________________")
115 fmt.Println("Sample 5 - Replicate pages within a single document...")
116 fmt.Println("Opening the input pdf...")
117
118 doc := NewPDFDoc(inputPath + "newsletter.pdf")
119 doc.InitSecurityHandler()
120
121 // Replicate the cover page three times (copy page //1 and place it before the
122 // seventh page in the document page sequence)
123 cover := doc.GetPage(1)
124 p7 := doc.GetPageIterator(uint(7))
125 doc.PageInsert(p7, cover)
126 doc.PageInsert(p7, cover)
127 doc.PageInsert(p7, cover)
128
129 // Replicate the cover page two more times by placing it before and after
130 // existing pages.
131 doc.PagePushFront(cover);
132 doc.PagePushBack(cover)
133
134 doc.Save(outputPath + "newsletter_page_clone.pdf", uint(0))
135 fmt.Println("Done. Result saved in newsletter_page_clone.pdf...")
136 doc.Close()
137
138 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
139 // in order to preserve shared resources between pages (e.g. images, fonts,
140 // colorspaces, etc.)
141 fmt.Println("_______________________________________________")
142 fmt.Println("Sample 6 - Preserving shared resources using ImportPages...")
143 fmt.Println("Opening the input pdf...")
144 inDoc = NewPDFDoc(inputPath + "newsletter.pdf")
145 inDoc.InitSecurityHandler()
146 newDoc = NewPDFDoc()
147 copyPages := NewVectorPage()
148 itr := inDoc.GetPageIterator()
149 for itr.HasNext(){
150 copyPages.Add(itr.Current())
151 itr.Next()
152 }
153
154 importedPages := newDoc.ImportPages(copyPages)
155 for i := 0; i < int(importedPages.Size()); i ++{
156 newDoc.PagePushFront(importedPages.Get(i))
157 }
158 newDoc.Save(outputPath + "newsletter_import_pages.pdf", uint(0))
159
160 // Close the open document to free up document memory sooner than waiting for the
161 // garbage collector
162 inDoc.Close()
163 newDoc.Close()
164
165 PDFNetTerminate()
166 fmt.Println("Done. Result saved in newsletter_import_pages.pdf...\n")
167 fmt.Println("Note that the output file size is less than half the size")
168 fmt.Println("of the file produced using individual page copy operations")
169 fmt.Println("between two documents")
170}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.*;
8import com.pdftron.common.PDFNetException;
9
10public class PDFPageTest {
11
12 public static void main(String[] args) {
13 PDFNet.initialize(PDFTronLicense.Key());
14
15 // Relative path to the folder containing test files.
16 String input_path = "../../TestFiles/";
17 String output_path = "../../TestFiles/Output/";
18
19 // Sample 1 - Split a PDF document into multiple pages
20 System.out.println("_______________________________________________");
21 System.out.println("Sample 1 - Split a PDF document into multiple pages...");
22 System.out.println("Opening the input pdf...");
23 try (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) {
24 in_doc.initSecurityHandler();
25
26 int page_num = in_doc.getPageCount();
27 for (int i = 1; i <= page_num; ++i) {
28 try (PDFDoc new_doc = new PDFDoc()) {
29 new_doc.insertPages(0, in_doc, i, i, PDFDoc.InsertBookmarkMode.NONE, null);
30 String fname = "newsletter_split_page_" + i + ".pdf";
31 new_doc.save(output_path + fname, SDFDoc.SaveMode.REMOVE_UNUSED, null);
32 // output PDF new_doc
33 System.out.println("Done. Result saved in newsletter_split_page_" + i + ".pdf");
34 }
35 }
36 } catch (Exception e2) {
37 System.out.println(e2);
38 }
39
40 // Sample 2 - Merge several PDF documents into one
41 System.out.println("_______________________________________________");
42 System.out.println("Sample 2 - Merge several PDF documents into one...");
43 try (PDFDoc new_doc = new PDFDoc()) {
44 new_doc.initSecurityHandler();
45
46 int page_num = 15;
47 for (int i = 1; i <= page_num; ++i) {
48 System.out.println("Opening newsletter_split_page_" + i + ".pdf");
49 String fname = "newsletter_split_page_" + i + ".pdf";
50 try (PDFDoc in_doc = new PDFDoc(output_path + fname)) {
51 new_doc.insertPages(i, in_doc, 1, in_doc.getPageCount(), PDFDoc.InsertBookmarkMode.NONE, null);
52 }
53 }
54 new_doc.save(output_path + "newsletter_merge_pages.pdf", SDFDoc.SaveMode.REMOVE_UNUSED, null);
55 System.out.println("Done. Result saved in newsletter_merge_pages.pdf");
56 } catch (Exception e2) {
57 System.out.println(e2);
58 }
59
60 // Sample 3 - Delete every second page
61 System.out.println("_______________________________________________");
62 System.out.println("Sample 3 - Delete every second page...");
63 System.out.println("Opening the input pdf...");
64 try (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf")) {
65 in_doc.initSecurityHandler();
66
67 int page_num = in_doc.getPageCount();
68 while (page_num >= 1) {
69 PageIterator itr = in_doc.getPageIterator(page_num);
70 in_doc.pageRemove(itr);
71 page_num -= 2;
72 }
73
74 in_doc.save(output_path + "newsletter_page_remove.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
75 System.out.println("Done. Result saved in newsletter_page_remove.pdf...");
76
77 } catch (Exception e2) {
78 System.out.println(e2);
79 }
80
81 // Sample 4 - Inserts a page from one document at different
82 // locations within another document
83 System.out.println("_______________________________________________");
84 System.out.println("Sample 4 - Insert a page at different locations...");
85 System.out.println("Opening the input pdf...");
86 try (PDFDoc in1_doc = new PDFDoc((input_path + "newsletter.pdf"))) {
87 in1_doc.initSecurityHandler();
88 try (PDFDoc in2_doc = new PDFDoc((input_path + "fish.pdf"))) {
89 in2_doc.initSecurityHandler();
90
91 PageIterator src_page_itr = in2_doc.getPageIterator();
92 Page src_page = src_page_itr.next();
93 PageIterator dst_page_itr = in1_doc.getPageIterator();
94 int page_num = 1;
95 while (dst_page_itr.hasNext()) {
96 if (page_num++ % 3 == 0) {
97 in1_doc.pageInsert(dst_page_itr, src_page);
98 }
99 dst_page_itr.next();
100 }
101
102 in1_doc.save(output_path + "newsletter_page_insert.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
103 // output PDF in1_doc
104 System.out.println("Done. Result saved in newsletter_page_insert.pdf...");
105 }
106 } catch (Exception e) {
107 System.out.println(e);
108 }
109
110 // Sample 5 - Replicate pages within a single document
111 System.out.println("_______________________________________________");
112 System.out.println("Sample 5 - Replicate pages within a single document...");
113 System.out.println("Opening the input pdf...");
114 try (PDFDoc doc = new PDFDoc((input_path + "newsletter.pdf"))) {
115 doc.initSecurityHandler();
116
117 // Replicate the cover page three times (copy page #1 and place it before the
118 // seventh page in the document page sequence)
119 Page cover = doc.getPage(1);
120 PageIterator p7 = doc.getPageIterator(7);
121 doc.pageInsert(p7, cover);
122 doc.pageInsert(p7, cover);
123 doc.pageInsert(p7, cover);
124
125 // Replicate the cover page two more times by placing it before and after
126 // existing pages.
127 doc.pagePushFront(cover);
128 doc.pagePushBack(cover);
129
130 doc.save(output_path + "newsletter_page_clone.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
131 System.out.println("Done. Result saved in newsletter_page_clone.pdf...");
132
133 } catch (Exception e2) {
134 System.out.println(e2);
135 }
136
137 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
138 // in order to preserve shared resources between pages (e.g. images, fonts,
139 // colorspaces, etc.)
140 System.out.println("_______________________________________________");
141 System.out.println("Sample 6 - Preserving shared resources using ImportPages...");
142 System.out.println("Opening the input pdf...");
143 try (PDFDoc in_doc = new PDFDoc((input_path + "newsletter.pdf"))) {
144 in_doc.initSecurityHandler();
145
146 try (PDFDoc new_doc = new PDFDoc()) {
147
148 Page[] copy_pages = new Page[in_doc.getPageCount()];
149 int j = 0;
150 for (PageIterator itr = in_doc.getPageIterator(); itr.hasNext(); j++) {
151 copy_pages[j] = itr.next();
152 }
153
154 Page[] imported_pages = new_doc.importPages(copy_pages);
155 for (int i = 0; i < imported_pages.length; ++i) {
156 new_doc.pagePushFront(imported_pages[i]); // Order pages in reverse order.
157 // Use pushBackPage() if you would like to preserve the same order.
158 }
159
160 new_doc.save(output_path + "newsletter_import_pages.pdf", SDFDoc.SaveMode.NO_FLAGS, null);
161
162 System.out.println("Done. Result saved in newsletter_import_pages.pdf...");
163 System.out.println();
164 System.out.println("Note that the output file size is less than half the size");
165 System.out.println("of the file produced using individual page copy operations");
166 System.out.println("between two documents");
167 }
168 } catch (Exception e1) {
169 System.out.println(e1);
170 }
171
172 PDFNet.terminate();
173 }
174}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6const { PDFNet } = require('@pdftron/pdfnet-node');
7const PDFTronLicense = require('../LicenseKey/LicenseKey');
8
9((exports) => {
10
11 exports.runPDFPageTest = () => {
12
13 const main = async () => {
14 // Relative path to the folder containing test files.
15 const inputPath = '../TestFiles/';
16 const outputPath = inputPath + 'Output/';
17
18 // Sample 1 - Split a PDF document into multiple pages
19 try {
20 console.log('_______________________________________________');
21 console.log('Sample 1 - Split a PDF document into multiple pages...');
22 console.log('Opening the input pdf...');
23 const inDoc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
24 inDoc.initSecurityHandler();
25
26 const pageCount = await inDoc.getPageCount();
27 for (let i = 1; i <= pageCount; ++i) {
28 const newDoc = await PDFNet.PDFDoc.create();
29 const filename = 'newsletter_split_page_' + i + '.pdf';
30 newDoc.insertPages(0, inDoc, i, i, PDFNet.PDFDoc.InsertFlag.e_none);
31 await newDoc.save(outputPath + filename, PDFNet.SDFDoc.SaveOptions.e_remove_unused);
32 console.log('Done. Result saved in ' + filename);
33 }
34 } catch (err) {
35 // console.log(err);
36 console.log(err.stack);
37 }
38
39 // Sample 2 - Merge several PDF documents into one
40 try {
41 console.log('_______________________________________________');
42 console.log('Sample 2 - Merge several PDF documents into one...');
43
44 // start stack-based deallocation with startDeallocateStack. Later on when endDeallocateStack is called,
45 // all objects in memory that were initialized since the most recent startDeallocateStack call will be
46 // cleaned up. Doing this makes sure that memory growth does not get too high.
47 await PDFNet.startDeallocateStack();
48 const newDoc = await PDFNet.PDFDoc.create();
49 newDoc.initSecurityHandler();
50
51 const pageNum = 15
52 for (let i = 1; i <= pageNum; ++i) {
53 const fname = 'newsletter_split_page_' + i + '.pdf';
54 console.log('Opening ' + fname);
55 const currDoc = await PDFNet.PDFDoc.createFromFilePath(outputPath + fname);
56 const currDocPageCount = await currDoc.getPageCount();
57 newDoc.insertPages(i, currDoc, 1, currDocPageCount, PDFNet.PDFDoc.InsertFlag.e_none);
58 }
59 await newDoc.save(outputPath + 'newsletter_merge_pages.pdf', PDFNet.SDFDoc.SaveOptions.e_remove_unused);
60 console.log('Done. Result saved in newsletter_merge_pages.pdf');
61 await PDFNet.endDeallocateStack();
62 } catch (err) {
63 // console.log(err);
64 console.log(err.stack);
65 ret = 1;
66 }
67
68 // Sample 3 - Delete every second page
69 try {
70 console.log('_______________________________________________');
71 console.log('Sample 3 - Delete every second page...');
72 console.log('Opening the input pdf...');
73 await PDFNet.startDeallocateStack();
74 const inDoc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
75 inDoc.initSecurityHandler();
76
77 let pageNum = await inDoc.getPageCount();
78
79 while (pageNum >= 1) {
80 const itr = await inDoc.getPageIterator(pageNum);
81 inDoc.pageRemove(itr);
82 pageNum -= 2;
83 }
84
85 await inDoc.save(outputPath + 'newsletter_page_remove.pdf', 0);
86 console.log('Done. Result saved in newsletter_page_remove.pdf...');
87 await PDFNet.endDeallocateStack();
88 } catch (err) {
89 console.log(err);
90 ret = 1;
91 }
92
93 // Sample 4 - Inserts a page from one document at different
94 // locations within another document
95 try {
96 console.log('_______________________________________________');
97 console.log('Sample 4 - Insert a page at different locations...');
98 console.log('Opening the input pdf...');
99 await PDFNet.startDeallocateStack();
100 const in1Doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
101 in1Doc.initSecurityHandler();
102
103 const in2Doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'fish.pdf');
104 in2Doc.initSecurityHandler();
105
106 const srcPage = await in2Doc.getPageIterator();
107 const dstPage = await in1Doc.getPageIterator();
108 let pageNum = 1;
109 while (await dstPage.hasNext()) {
110 if (pageNum++ % 3 === 0) {
111 in1Doc.pageInsert(dstPage, await srcPage.current());
112 }
113 dstPage.next();
114 }
115
116 await in1Doc.save(outputPath + 'newsletter_page_insert.pdf', 0);
117 console.log('Done. Result saved in newsletter_page_insert.pdf...');
118 await PDFNet.endDeallocateStack();
119 } catch (err) {
120 console.log(err.stack);
121 ret = 1;
122 }
123
124 // Sample 5 - Replicate pages within a single document
125 try {
126 console.log('_______________________________________________');
127 console.log('Sample 5 - Replicate pages within a single document...');
128 console.log('Opening the input pdf...');
129 await PDFNet.startDeallocateStack();
130 const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
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 const cover = await doc.getPage(1);
136 const p7 = await doc.getPageIterator(7);
137 doc.pageInsert(p7, cover);
138 doc.pageInsert(p7, cover);
139 doc.pageInsert(p7, cover);
140 // replicate cover page two more times by placing it before and after existing pages
141 doc.pagePushFront(cover);
142 doc.pagePushBack(cover);
143
144 await doc.save(outputPath + 'newsletter_page_clone.pdf', 0);
145 console.log('Done. Result saved in newsletter_page_clone.pdf...');
146 await PDFNet.endDeallocateStack();
147 } catch (err) {
148 console.log(err.stack);
149 ret = 1;
150 }
151
152 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
153 // in order to preserve shared resources between pages (e.g. images, fonts,
154 // colorspaces, etc.)
155 try {
156 console.log('_______________________________________________');
157 console.log('Sample 6 - Preserving shared resources using ImportPages...');
158 console.log('Opening the input pdf...');
159 const in_doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf');
160 in_doc.initSecurityHandler();
161
162 const new_doc = await PDFNet.PDFDoc.create();
163
164 const copy_pages = [];
165 for (const itr = await in_doc.getPageIterator(); await itr.hasNext(); await itr.next()) {
166 copy_pages.push(await itr.current());
167 }
168
169 const imported_pages = await new_doc.importPages(copy_pages);
170 for (var i = 0; i < imported_pages.length; ++i) {
171 new_doc.pagePushFront(imported_pages[i]); // Order pages in reverse order.
172 // Use PagePushBack() if you would like to preserve the same order.
173 }
174
175 await new_doc.save(outputPath + 'newsletter_import_pages.pdf', 0);
176 console.log('Done. Result saved in newsletter_import_pages.pdf...\n');
177 console.log('Note that the output file size is less than half the size\n'
178 + 'of the file produced using individual page copy operations\n'
179 + 'between two documents');
180 } catch (err) {
181 console.log(err.stack);
182 }
183 };
184
185 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
186 };
187 exports.runPDFPageTest();
188})(exports);
189// eslint-disable-next-line spaced-comment
190//# sourceURL=PDFPageTest.js
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14 PDFNet::Initialize($LicenseKey);
15 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
16
17 // Sample 1 - Split a PDF document into multiple pages
18
19 echo nl2br("_______________________________________________\n");
20 echo nl2br("Sample 1 - Split a PDF document into multiple pages...\n");
21 echo nl2br("Opening the input pdf...\n");
22 $in_doc = new PDFDoc($input_path."newsletter.pdf");
23 $in_doc->InitSecurityHandler();
24
25 $page_num = $in_doc->GetPageCount();
26 for ($i = 1; $i <= $page_num; ++$i)
27 {
28 $new_doc = new PDFDoc();
29 $new_doc->InsertPages(0, $in_doc, $i, $i, PDFDoc::e_none);
30 $new_doc->Save($output_path."newsletter_split_page_".$i.".pdf", SDFDoc::e_remove_unused);
31 echo nl2br("Done. Result saved in newsletter_split_page_".$i.".pdf\n");
32 $new_doc->Close();
33 }
34 $in_doc->Close();
35
36 // Sample 2 - Merge several PDF documents into one
37
38 echo nl2br("_______________________________________________\n");
39 echo nl2br("Sample 2 - Merge several PDF documents into one...\n");
40 $new_doc = new PDFDoc();
41 $new_doc->InitSecurityHandler();
42
43 $page_num = 15;
44 for ($i = 1; $i <= $page_num; ++$i)
45 {
46 echo nl2br("Opening newsletter_split_page_".$i.".pdf\n");
47 $in_doc = new PDFDoc($output_path."newsletter_split_page_".$i.".pdf");
48 $new_doc->InsertPages($i, $in_doc, 1, $in_doc->GetPageCount(), PDFDoc::e_none);
49 $in_doc->Close();
50 }
51 $new_doc->Save($output_path."newsletter_merge_pages.pdf", SDFDoc::e_remove_unused);
52 echo nl2br("Done. Result saved in newsletter_merge_pages.pdf\n");
53 $in_doc->Close();
54
55 // Sample 3 - Delete every second page
56
57 echo nl2br("_______________________________________________\n");
58 echo nl2br("Sample 3 - Delete every second page...\n");
59 echo nl2br("Opening the input pdf...\n");
60 $in_doc = new PDFDoc($input_path."newsletter.pdf");
61 $in_doc->InitSecurityHandler();
62
63 $page_num = $in_doc->GetPageCount();
64 while ($page_num>=1)
65 {
66 $itr = $in_doc->GetPageIterator($page_num);
67 $in_doc->PageRemove($itr);
68 $page_num -= 2;
69 }
70
71 $in_doc->Save($output_path."newsletter_page_remove.pdf", 0);
72 echo nl2br("Done. Result saved in newsletter_page_remove.pdf...\n");
73
74 //Close the open document to free up document memory sooner than waiting for the
75 //garbage collector
76 $in_doc->Close();
77
78 // Sample 4 - Inserts a page from one document at different
79 // locations within another document
80
81 echo nl2br("_______________________________________________\n");
82 echo nl2br("Sample 4 - Insert a page at different locations...\n");
83 echo nl2br("Opening the input pdf...\n");
84
85 $in1_doc = new PDFDoc($input_path."newsletter.pdf");
86 $in1_doc->InitSecurityHandler();
87
88 $in2_doc = new PDFDoc($input_path."fish.pdf");
89 $in2_doc->InitSecurityHandler();
90
91 $src_page = $in2_doc->GetPageIterator();
92 $dst_page = $in1_doc->GetPageIterator();
93 $page_num = 1;
94 while ($dst_page->HasNext()) {
95 if ($page_num++ % 3 == 0) {
96 $in1_doc->PageInsert($dst_page, $src_page->Current());
97 }
98 $dst_page->Next();
99 }
100
101 $in1_doc->Save($output_path."newsletter_page_insert.pdf", 0);
102 echo nl2br("Done. Result saved in newsletter_page_insert.pdf...\n");
103
104 //Close the open document to free up document memory sooner than waiting for the
105 //garbage collector
106 $in1_doc->Close();
107 $in2_doc->Close();
108
109 // Sample 5 - Replicate pages within a single document
110
111 echo nl2br("_______________________________________________\n");
112 echo nl2br("Sample 5 - Replicate pages within a single document...\n");
113 echo nl2br("Opening the input pdf...\n");
114 $doc = new PDFDoc($input_path."newsletter.pdf");
115 $doc->InitSecurityHandler();
116
117 // Replicate the cover page three times (copy page #1 and place it before the
118 // seventh page in the document page sequence)
119 $cover = $doc->GetPage(1);
120 $p7 = $doc->GetPageIterator(7);
121 $doc->PageInsert($p7, $cover);
122 $doc->PageInsert($p7, $cover);
123 $doc->PageInsert($p7, $cover);
124
125 // Replicate the cover page two more times by placing it before and after
126 // existing pages.
127 $doc->PagePushFront($cover);
128 $doc->PagePushBack($cover);
129
130 $doc->Save($output_path."newsletter_page_clone.pdf", 0);
131 echo nl2br("Done. Result saved in newsletter_page_clone.pdf...\n");
132 $doc->Close();
133
134 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
135 // in order to preserve shared resources between pages (e.g. images, fonts,
136 // colorspaces, etc.)
137
138 echo nl2br("_______________________________________________\n");
139 echo nl2br("Sample 6 - Preserving shared resources using ImportPages...\n");
140 echo nl2br("Opening the input pdf...\n");
141 $in_doc = new PDFDoc($input_path."newsletter.pdf");
142 $in_doc->InitSecurityHandler();
143
144 $new_doc = new PDFDoc();
145
146 $copy_pages = new VectorPage();
147 for ($itr=$in_doc->GetPageIterator(); $itr->HasNext(); $itr->Next())
148 {
149 $copy_pages->push($itr->Current());
150 }
151
152 $imported_pages = $new_doc->ImportPages($copy_pages);
153 for ($i=0; $i<$imported_pages->size(); ++$i)
154 {
155 $new_doc->PagePushFront($imported_pages->get($i)); // Order pages in reverse order.
156 // Use PagePushBack() if you would like to preserve the same order.
157 }
158
159 $new_doc->Save($output_path."newsletter_import_pages.pdf", 0);
160 PDFNet::Terminate();
161 echo nl2br("Done. Result saved in newsletter_import_pages.pdf...\n\n"
162 ."Note that the output file size is less than half the size\n"
163 ."of the file produced using individual page copy operations\n"
164 ."between two documents\n");
165?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14def main():
15 PDFNet.Initialize(LicenseKey)
16
17 # Relative path to the folder containing the test files.
18 input_path = "../../TestFiles/"
19 output_path = "../../TestFiles/Output/"
20
21 # Sample 1 - Split a PDF document into multiple pages
22 print("_______________________________________________")
23 print("Sample 1 - Split a PDF document into multiple pages...")
24 print("Opening the input pdf...")
25 in_doc=PDFDoc(input_path + "newsletter.pdf")
26 in_doc.InitSecurityHandler()
27
28 page_num = in_doc.GetPageCount()
29 i = 1
30 while i<=page_num:
31 new_doc=PDFDoc()
32 new_doc.InsertPages(0, in_doc, i, i, PDFDoc.e_none)
33 new_doc.Save(output_path + "newsletter_split_page_" + str(i) + ".pdf", SDFDoc.e_remove_unused)
34 print("Done. Result saved in newsletter_split_page_" + str(i) + ".pdf")
35 new_doc.Close()
36 i = i + 1
37
38 # Close the open document to free up document memory sooner than waiting for the
39 # garbage collector
40 in_doc.Close()
41
42 # Sample 2 - Merge several PDF documents into one
43 print("_______________________________________________")
44 print("Sample 2 - Merge several PDF documents into one...")
45 new_doc=PDFDoc()
46 new_doc.InitSecurityHandler()
47
48 page_num = 15
49 i = 1
50 while i <= page_num:
51 print("Opening newsletter_split_page_" + str(i) + ".pdf")
52 in_doc=PDFDoc(output_path + "newsletter_split_page_" + str(i) + ".pdf")
53 new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount(), PDFDoc.e_none)
54 in_doc.Close()
55 i = i + 1
56
57 new_doc.Save(output_path + "newsletter_merge_pages.pdf", SDFDoc.e_remove_unused)
58 print("Done. Result saved in newsletter_merge_pages.pdf");
59
60 # Close the open document to free up document memory sooner than waiting for the
61 # garbage collector
62 in_doc.Close()
63
64 # Sample 3 - Delete every second page
65 print("_______________________________________________")
66 print("Sample 3 - Delete every second page...")
67 print("Opening the input pdf...")
68 in_doc = PDFDoc(input_path + "newsletter.pdf")
69 in_doc.InitSecurityHandler();
70 page_num = in_doc.GetPageCount()
71
72 while page_num >= 1:
73 itr = in_doc.GetPageIterator(page_num)
74 in_doc.PageRemove(itr)
75 page_num = page_num - 2
76
77 in_doc.Save(output_path + "newsletter_page_remove.pdf", 0)
78 print("Done. Result saved in newsletter_page_remove.pdf...")
79
80 # Close the open document to free up document memory sooner than waiting for the
81 # garbage collector
82 in_doc.Close()
83
84 # Sample 4 - Inserts a page from one document at different
85 # locations within another document
86 print("_______________________________________________")
87 print("Sample 4 - Insert a page at different locations...")
88 print("Opening the input pdf...")
89
90 in1_doc = PDFDoc(input_path + "newsletter.pdf")
91 in1_doc.InitSecurityHandler()
92 in2_doc = PDFDoc(input_path + "fish.pdf")
93 in2_doc.InitSecurityHandler()
94
95 src_page = in2_doc.GetPageIterator()
96 dst_page = in1_doc.GetPageIterator()
97 page_num = 1
98 while dst_page.HasNext():
99 if page_num % 3 == 0:
100 in1_doc.PageInsert(dst_page, src_page.Current())
101 page_num = page_num + 1
102 dst_page.Next()
103 in1_doc.Save(output_path + "newsletter_page_insert.pdf", 0)
104 print("Done. Result saved in newsletter_page_insert.pdf...")
105
106 # Close the open document to free up document memory sooner than waiting for the
107 # garbage collector
108 in1_doc.Close()
109 in2_doc.Close()
110
111 # Sample 5 - Replicate pages within a single document
112 print("_______________________________________________")
113 print("Sample 5 - Replicate pages within a single document...")
114 print("Opening the input pdf...")
115
116 doc = PDFDoc(input_path + "newsletter.pdf")
117 doc.InitSecurityHandler()
118
119 # Replicate the cover page three times (copy page #1 and place it before the
120 # seventh page in the document page sequence)
121 cover = doc.GetPage(1)
122 p7 = doc.GetPageIterator(7)
123 doc.PageInsert(p7, cover)
124 doc.PageInsert(p7, cover)
125 doc.PageInsert(p7, cover)
126
127 # Replicate the cover page two more times by placing it before and after
128 # existing pages.
129 doc.PagePushFront(cover);
130 doc.PagePushBack(cover)
131
132 doc.Save(output_path + "newsletter_page_clone.pdf", 0)
133 print("Done. Result saved in newsletter_page_clone.pdf...")
134 doc.Close()
135
136 # Sample 6 - Use ImportPages() in order to copy multiple pages at once
137 # in order to preserve shared resources between pages (e.g. images, fonts,
138 # colorspaces, etc.)
139 print("_______________________________________________")
140 print("Sample 6 - Preserving shared resources using ImportPages...")
141 print("Opening the input pdf...")
142 in_doc = PDFDoc(input_path + "newsletter.pdf")
143 in_doc.InitSecurityHandler()
144 new_doc = PDFDoc()
145 copy_pages = VectorPage()
146 itr = in_doc.GetPageIterator()
147 while itr.HasNext():
148 copy_pages.push_back(itr.Current())
149 itr.Next()
150
151 imported_pages = new_doc.ImportPages(copy_pages)
152 i = iter(imported_pages)
153 for x in i:
154 new_doc.PagePushFront(x) # Order pages in reverse order.
155 # Use PagePushBack() if you would like to preserve the same order.
156
157 new_doc.Save(output_path + "newsletter_import_pages.pdf", 0)
158
159 # Close the open document to free up document memory sooner than waiting for the
160 # garbage collector
161 in_doc.Close()
162 new_doc.Close()
163
164 PDFNet.Terminate()
165
166 print("Done. Result saved in newsletter_import_pages.pdf...\n")
167 print("Note that the output file size is less than half the size")
168 print("of the file produced using individual page copy operations")
169 print("between two documents")
170
171if __name__ == '__main__':
172 main()
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12 PDFNet.Initialize(PDFTronLicense.Key)
13
14 # Relative path to the folder containing the test files.
15 input_path = "../../TestFiles/"
16 output_path = "../../TestFiles/Output/"
17
18 # Sample 1 - Split a PDF document into multiple pages
19
20 puts "_______________________________________________"
21 puts "Sample 1 - Split a PDF document into multiple pages..."
22 puts "Opening the input pdf..."
23 in_doc = PDFDoc.new(input_path + "newsletter.pdf")
24 in_doc.InitSecurityHandler
25
26 page_num = in_doc.GetPageCount
27 i = 1
28 while i <= page_num do
29 new_doc = PDFDoc.new
30 new_doc.InsertPages(0, in_doc, i, i, PDFDoc::E_none)
31 new_doc.Save(output_path + "newsletter_split_page_" + i.to_s + ".pdf", SDFDoc::E_remove_unused)
32 puts "Done. Result saved in newsletter_split_page_" + i.to_s + ".pdf"
33 new_doc.Close
34 i = i + 1
35 end
36
37 # Close the open document to free up document memory sooner than waiting for the
38 # garbage collector
39 in_doc.Close
40
41 # Sample 2 - Merge several PDF documents into one
42
43 puts "_______________________________________________"
44 puts "Sample 2 - Merge several PDF documents into one..."
45 new_doc = PDFDoc.new
46 new_doc.InitSecurityHandler
47
48 page_num = 15
49 i = 1
50 while i <= page_num do
51 puts "Opening newsletter_split_page_" + i.to_s + ".pdf"
52 in_doc = PDFDoc.new(output_path + "newsletter_split_page_" + i.to_s + ".pdf")
53 new_doc.InsertPages(i, in_doc, 1, in_doc.GetPageCount, PDFDoc::E_none)
54 in_doc.Close
55 i = i + 1
56 end
57
58 new_doc.Save(output_path + "newsletter_merge_pages.pdf", SDFDoc::E_remove_unused)
59 puts "Done. Result saved in newsletter_merge_pages.pdf"
60
61 # Close the open document to free up document memory sooner than waiting for the
62 # garbage collector
63 in_doc.Close
64
65 # Sample 3 - Delete every second page
66
67 puts "_______________________________________________"
68 puts "Sample 3 - Delete every second page..."
69 puts "Opening the input pdf..."
70 in_doc = PDFDoc.new(input_path + "newsletter.pdf")
71 in_doc.InitSecurityHandler
72 page_num = in_doc.GetPageCount
73
74 while page_num >= 1 do
75 itr = in_doc.GetPageIterator(page_num)
76 in_doc.PageRemove(itr)
77 page_num = page_num - 2
78 end
79
80 in_doc.Save(output_path + "newsletter_page_remove.pdf", 0)
81 puts "Done. Result saved in newsletter_page_remove.pdf..."
82
83 # Close the open document to free up document memory sooner than waiting for the
84 # garbage collector
85 in_doc.Close
86
87 # Sample 4 - Inserts a page from one document at different
88 # locations within another document
89 puts "_______________________________________________"
90 puts "Sample 4 - Insert a page at different locations..."
91 puts "Opening the input pdf..."
92
93 in1_doc = PDFDoc.new(input_path + "newsletter.pdf")
94 in1_doc.InitSecurityHandler
95 in2_doc = PDFDoc.new(input_path + "fish.pdf")
96 in2_doc.InitSecurityHandler
97
98 src_page = in2_doc.GetPageIterator
99 dst_page = in1_doc.GetPageIterator
100 page_num = 1
101 while dst_page.HasNext
102 if page_num % 3 == 0
103 in1_doc.PageInsert(dst_page, src_page.Current)
104 end
105 page_num = page_num + 1
106 dst_page.Next
107 end
108 in1_doc.Save(output_path + "newsletter_page_insert.pdf", 0)
109 puts "Done. Result saved in newsletter_page_insert.pdf..."
110
111 # Close the open document to free up document memory sooner than waiting for the
112 # garbage collector
113 in1_doc.Close
114 in2_doc.Close
115
116 # Sample 5 - Replicate pages within a single document
117 puts "_______________________________________________"
118 puts "Sample 5 - Replicate pages within a single document..."
119 puts "Opening the input pdf..."
120
121 doc = PDFDoc.new(input_path + "newsletter.pdf")
122 doc.InitSecurityHandler
123
124 # Replicate the cover page three times (copy page #1 and place it before the
125 # seventh page in the document page sequence)
126 cover = doc.GetPage(1)
127 p7 = doc.GetPageIterator(7)
128 doc.PageInsert(p7, cover)
129 doc.PageInsert(p7, cover)
130 doc.PageInsert(p7, cover)
131
132 # Replicate the cover page two more times by placing it before and after
133 # existing pages.
134 doc.PagePushFront(cover);
135 doc.PagePushBack(cover)
136
137 doc.Save(output_path + "newsletter_page_clone.pdf", 0)
138 puts "Done. Result saved in newsletter_page_clone.pdf..."
139 doc.Close
140
141 # Sample 6 - Use ImportPages in order to copy multiple pages at once
142 # in order to preserve shared resources between pages (e.g. images, fonts,
143 # colorspaces, etc.)
144 puts "_______________________________________________"
145 puts "Sample 6 - Preserving shared resources using ImportPages..."
146 puts "Opening the input pdf..."
147 in_doc = PDFDoc.new(input_path + "newsletter.pdf")
148 in_doc.InitSecurityHandler
149 new_doc = PDFDoc.new
150 copy_pages = VectorPage.new
151 itr = in_doc.GetPageIterator
152 while itr.HasNext do
153 copy_pages << itr.Current
154 itr.Next
155 end
156
157 imported_pages = new_doc.ImportPages(copy_pages)
158 for x in imported_pages
159 new_doc.PagePushFront(x) # Order pages in reverse order.
160 # Use PagePushBack if you would like to preserve the same order.
161 end
162
163 new_doc.Save(output_path + "newsletter_import_pages.pdf", 0)
164
165 # Close the open document to free up document memory sooner than waiting for the
166 # garbage collector
167 in_doc.Close
168 new_doc.Close
169 PDFNet.Terminate
170 puts "Done. Result saved in newsletter_import_pages.pdf..."
171 puts "\nNote that the output file size is less than half the size"
172 puts "of the file produced using individual page copy operations"
173 puts "between two documents"
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6Imports System.Collections
7
8Imports pdftron
9Imports pdftron.Common
10Imports pdftron.Filters
11Imports pdftron.SDF
12Imports pdftron.PDF
13
14
15Module PDFPageTestVB
16 Dim pdfNetLoader As PDFNetLoader
17 Sub New()
18 pdfNetLoader = pdftron.PDFNetLoader.Instance()
19 End Sub
20
21 Sub Main()
22
23 PDFNet.Initialize(PDFTronLicense.Key)
24
25 ' Relative path to the folder containing test files.
26 Dim input_path As String = "../../../../TestFiles/"
27 Dim output_path As String = "../../../../TestFiles/Output/"
28
29 Try
30 '---------------------------------------------------------------------------------
31 ' Sample 1 - Split a PDF document into multiple pages
32
33 Console.WriteLine("_______________________________________________")
34 Console.WriteLine("Sample 1 - Split a PDF document into multiple pages...")
35 Console.WriteLine("Opening the input pdf...")
36
37 Using in_doc As PDFDoc = New PDFDoc(input_path + "newsletter.pdf")
38 in_doc.InitSecurityHandler()
39
40 Dim page_num As Integer = in_doc.GetPageCount()
41 Dim i As Integer
42
43 For i = 1 To page_num
44 Dim new_doc As PDFDoc = New PDFDoc()
45 new_doc.InsertPages(0, in_doc, i, i, PDFDoc.InsertFlag.e_none)
46 new_doc.Save(output_path + "newsletter_split_page_" + CStr(i) + ".pdf", SDFDoc.SaveOptions.e_remove_unused)
47 Console.WriteLine("Done. Result saved in newsletter_split_page_" + CStr(i) + ".pdf")
48 new_doc.Close()
49 Next i
50 End Using
51
52 '---------------------------------------------------------------------------------
53 ' Sample 2 - Merge several PDF documents into one
54
55 Console.WriteLine("_______________________________________________")
56 Console.WriteLine("Sample 2 - Merge several PDF documents into one...")
57
58 Using new1_doc As PDFDoc = New PDFDoc()
59 new1_doc.InitSecurityHandler()
60
61 Dim page_num1 As Integer = 15
62 Dim i1 As Integer
63
64 For i1 = 1 To page_num1
65 Console.WriteLine("Opening newsletter_split_page_" + CStr(i1) + ".pdf")
66 Dim in0_doc As PDFDoc = New PDFDoc(output_path + "newsletter_split_page_" + CStr(i1) + ".pdf")
67 new1_doc.InsertPages(i1, in0_doc, 1, in0_doc.GetPageCount(), PDFDoc.InsertFlag.e_none)
68 in0_doc.Close()
69 Next i1
70
71 new1_doc.Save(output_path + "newsletter_merge_pages.pdf", SDFDoc.SaveOptions.e_remove_unused)
72 Console.WriteLine("Done. Result saved in newsletter_merge_pages.pdf")
73 End Using
74
75 '---------------------------------------------------------------------------------
76 ' Sample 3 - Delete every second page
77
78 Console.WriteLine("_______________________________________________")
79 Console.WriteLine("Sample 3 - Delete every second page...")
80 Console.WriteLine("Opening the input pdf...")
81
82 Using in1_doc As PDFDoc = New PDFDoc(input_path + "newsletter.pdf")
83 in1_doc.InitSecurityHandler()
84
85 Dim page_num2 As Integer = in1_doc.GetPageCount()
86 Dim itr As PageIterator
87
88 While page_num2 >= 1
89 itr = in1_doc.GetPageIterator(page_num2)
90 in1_doc.PageRemove(itr)
91 page_num2 -= 2
92 End While
93
94 in1_doc.Save(output_path + "newsletter_page_remove.pdf", 0)
95 Console.WriteLine("Done. Result saved in newsletter_page_remove.pdf...")
96 End Using
97
98 '---------------------------------------------------------------------------------
99 ' Sample 4 - Inserts a page from one document at different
100 ' locations within another document
101 Console.WriteLine("_______________________________________________")
102 Console.WriteLine("Sample 4 - Insert a page at different locations...")
103 Console.WriteLine("Opening the input pdf...")
104
105 Using in2_doc As PDFDoc = New PDFDoc(input_path + "newsletter.pdf")
106 in2_doc.InitSecurityHandler()
107
108 Using in3_doc As PDFDoc = New PDFDoc(input_path + "fish.pdf")
109 in3_doc.InitSecurityHandler()
110
111 Dim src_page4 As Page = in3_doc.GetPage(1) ' Get the first page
112 Dim dst_page4 As PageIterator = in2_doc.GetPageIterator(1)
113 Dim page_num4 As Integer = 1
114 While dst_page4.HasNext()
115 If page_num4 Mod 3 = 0 Then
116 in2_doc.PageInsert(dst_page4, src_page4)
117 End If
118 page_num4 = page_num4 + 1
119 dst_page4.Next()
120 End While
121
122 in2_doc.Save(output_path + "newsletter_page_insert.pdf", 0)
123 Console.WriteLine("Done. Result saved in newsletter_page_insert.pdf...")
124 End Using
125 End Using
126
127 '---------------------------------------------------------------------------------
128 ' Sample 5 - Replicate pages within a single document
129
130 Console.WriteLine("_______________________________________________")
131 Console.WriteLine("Sample 5 - Replicate pages within a single document...")
132 Console.WriteLine("Opening the input pdf...")
133 Using doc5 As PDFDoc = New PDFDoc(input_path + "newsletter.pdf")
134 doc5.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 Dim cover As Page = doc5.GetPage(1)
139 Dim p7 as PageIterator = doc5.GetPageIterator(7)
140 doc5.PageInsert(p7, cover)
141 doc5.PageInsert(p7, cover)
142 doc5.PageInsert(p7, cover)
143
144 ' Replicate the cover page two more times by placing it before and after
145 ' existing pages.
146 doc5.PagePushFront(cover)
147 doc5.PagePushBack(cover)
148
149 doc5.Save(output_path + "newsletter_page_clone.pdf", 0)
150 Console.WriteLine("Done. Result saved in newsletter_page_clone.pdf...")
151 End Using
152
153 '---------------------------------------------------------------------------------
154 ' Sample 6 - Use ImportPages() in order to copy multiple pages at once
155 ' in order to preserve shared resources between pages (e.g. images, fonts,
156 ' colorspaces, etc.)
157 Console.WriteLine("_______________________________________________")
158 Console.WriteLine("Sample 6 - Preserving shared resources using ImportPages...")
159 Console.WriteLine("Opening the input pdf...")
160 Using in_doc6 As PDFDoc = New PDFDoc(input_path + "newsletter.pdf")
161 in_doc6.InitSecurityHandler()
162
163 Using new_doc6 As PDFDoc = New PDFDoc
164 Dim copy_pages As ArrayList = New ArrayList
165 Dim itr6 As PageIterator = in_doc6.GetPageIterator()
166 While itr6.HasNext()
167 copy_pages.Add(itr6.Current())
168 itr6.Next()
169 End While
170
171 Dim imported_pages As ArrayList = new_doc6.ImportPages(copy_pages)
172 Dim i6 As Integer
173 For i6 = 0 To imported_pages.Count - 1
174 new_doc6.PagePushFront(imported_pages(i6)) ' Order pages in reverse order.
175 ' Use PagePushBack() if you would like to preserve the same order.
176 Next i6
177
178 new_doc6.Save(output_path + "newsletter_import_pages.pdf", 0)
179 Console.WriteLine("Done. Result saved in newsletter_import_pages.pdf...")
180 Console.WriteLine()
181 Console.WriteLine("Note that the output file size is less than half the size")
182 Console.WriteLine("of the file produced using individual page copy operations")
183 Console.WriteLine("between two documents")
184 End Using
185 End Using
186
187 Catch ex As PDFNetException
188 Console.WriteLine(ex.Message)
189 Catch ex As Exception
190 MsgBox(ex.Message)
191 End Try
192 PDFNet.Terminate()
193 End Sub
194End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales