Combine PDFs with Page Imposition - Ruby Sample Code

Sample code for using Apryse SDK to combine (impose) multiple PDF pages. Page imposition can be used to arrange/order pages prior to printing or for document assembly (assemble a 'master' page from several 'source' pages). It is also possible to write applications that can re-order the pages such that they will display in the correct order when the hard copy pages are compiled and folded correctly. 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# Relative path to the folder containing the test files.
13input_path = "../../TestFiles/newsletter.pdf"
14output_path = "../../TestFiles/Output/newsletter_booklet.pdf"
15
16#-----------------------------------------------------------------------------------
17# The sample illustrates how multiple pages can be combined/imposed
18# using PDFNet. Page imposition can be used to arrange/order pages
19# prior to printing or to assemble a 'master' page from several 'source'
20# pages. Using PDFNet API it is possible to write applications that can
21# re-order the pages such that they will display in the correct order
22# when the hard copy pages are compiled and folded correctly.
23#-----------------------------------------------------------------------------------
24
25 PDFNet.Initialize(PDFTronLicense.Key)
26
27 puts "-------------------------------------------------"
28 puts "Opening the input pdf..."
29
30 in_doc = PDFDoc.new(input_path)
31 in_doc.InitSecurityHandler
32
33 # Create a list of pages to import from one PDF document to another
34 import_pages = VectorPage.new
35 itr = in_doc.GetPageIterator
36 while itr.HasNext do
37 import_pages << (itr.Current)
38 itr.Next
39 end
40
41 new_doc = PDFDoc.new
42 imported_pages = new_doc.ImportPages(import_pages)
43
44 # Paper dimension for A3 format in points. Because one inch has
45 # 72 points, 11.69 inch 72 = 841.69 points
46 media_box = Rect.new(0, 0, 1190.88, 841.69)
47 mid_point = media_box.Width/2
48
49 builder = ElementBuilder.new
50 writer = ElementWriter.new
51
52 i = 0
53 while i < imported_pages.size do
54 # Create a blank new A3 page and place on it two pages from the input document.
55 new_page = new_doc.PageCreate(media_box)
56 writer.Begin(new_page)
57
58 # Place the first page
59 src_page = imported_pages[i]
60
61 element = builder.CreateForm(imported_pages[i])
62 sc_x = mid_point / src_page.GetPageWidth
63 sc_y = media_box.Height / src_page.GetPageHeight
64 scale = sc_x < sc_y ? sc_x : sc_y # min(sc_x, sc_y)
65 element.GetGState.SetTransform(scale, 0, 0, scale, 0, 0)
66 writer.WritePlacedElement(element)
67
68 # Place the second page
69 i = i + 1
70 if i < imported_pages.size
71 src_page = imported_pages[i]
72 element = builder.CreateForm(src_page)
73 sc_x = mid_point / src_page.GetPageWidth
74 sc_y = media_box.Height / src_page.GetPageHeight
75 scale = sc_x < sc_y ? sc_x : sc_y # min(sc_x, sc_y)
76 element.GetGState.SetTransform(scale, 0, 0, scale, mid_point, 0)
77 writer.WritePlacedElement(element)
78 end
79
80 writer.End
81 new_doc.PagePushBack(new_page)
82 i = i + 1
83 end
84
85 new_doc.Save(output_path, SDFDoc::E_linearized)
86 PDFNet.Terminate
87 puts "Done. Result saved in newsletter_booklet.pdf..."

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales