PDF Page Manipulation - Merge, Copy, Delete, Rearrange - Ruby Sample Code

Sample 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. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

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"

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales