Imposition

Sample C# code for using Apryse SDK to impose (combine) 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. 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// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5using System;
6using System.IO;
7using System.Collections;
8using pdftron;
9using pdftron.Common;
10using pdftron.SDF;
11using pdftron.PDF;
12
13//-----------------------------------------------------------------------------------
14// The sample illustrates how multiple pages can be combined/imposed
15// using PDFNet. Page imposition can be used to arrange/order pages
16// prior to printing or to assemble a 'master' page from several 'source'
17// pages. Using PDFNet API it is possible to write applications that can
18// re-order the pages such that they will display in the correct order
19// when the hard copy pages are compiled and folded correctly.
20//-----------------------------------------------------------------------------------
21
22namespace ImpositionTestCS
23{
24 class Class1
25 {
26 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
27 static Class1() {}
28
29 static void Main(string[] args)
30 {
31 PDFNet.Initialize(PDFTronLicense.Key);
32
33 // Relative path to the folder containing test files.
34 string input_path = "../../../../TestFiles/";
35 string output_path = "../../../../TestFiles/Output/";
36
37 try
38 {
39 Console.WriteLine("-------------------------------------------------");
40 Console.WriteLine("Opening the input pdf...");
41 using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf"))
42 {
43 in_doc.InitSecurityHandler();
44
45 // Create a list of pages to import from one PDF document to another.
46 ArrayList import_list = new ArrayList();
47 for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next())
48 import_list.Add(itr.Current());
49
50 using (PDFDoc new_doc = new PDFDoc()) // Create a new document
51 using (ElementBuilder builder = new ElementBuilder())
52 using (ElementWriter writer = new ElementWriter())
53 {
54 ArrayList imported_pages = new_doc.ImportPages(import_list);
55
56 // Paper dimension for A3 format in points. Because one inch has
57 // 72 points, 11.69 inch 72 = 841.69 points
58 Rect media_box= new Rect(0, 0, 1190.88, 841.69);
59 double mid_point = media_box.Width()/2;
60
61 for (int i=0; i<imported_pages.Count; ++i)
62 {
63 // Create a blank new A3 page and place on it two pages from the input document.
64 Page new_page = new_doc.PageCreate(media_box);
65 writer.Begin(new_page);
66
67 // Place the first page
68 Page src_page = (Page)imported_pages[i];
69 Element element = builder.CreateForm(src_page);
70
71 double sc_x = mid_point / src_page.GetPageWidth();
72 double sc_y = media_box.Height() / src_page.GetPageHeight();
73 double scale = Math.Min(sc_x, sc_y);
74 element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0);
75 writer.WritePlacedElement(element);
76
77 // Place the second page
78 ++i;
79 if (i<imported_pages.Count)
80 {
81 src_page = (Page)imported_pages[i];
82 element = builder.CreateForm(src_page);
83 sc_x = mid_point / src_page.GetPageWidth();
84 sc_y = media_box.Height() / src_page.GetPageHeight();
85 scale = Math.Min(sc_x, sc_y);
86 element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0);
87 writer.WritePlacedElement(element);
88 }
89
90 writer.End();
91 new_doc.PagePushBack(new_page);
92 }
93 new_doc.Save(output_path + "newsletter_booklet.pdf", SDFDoc.SaveOptions.e_linearized);
94 Console.WriteLine("Done. Result saved in newsletter_booklet.pdf...");
95 }
96 }
97 }
98 catch (Exception e)
99 {
100 Console.WriteLine("Exception caught:\n{0}", e);
101 }
102 PDFNet.Terminate();
103 }
104 }
105}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales