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

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.Collections.Generic;
7using System.IO;
8using System.Threading.Tasks;
9using Windows.Foundation;
10
11using pdftron.PDF;
12using pdftron.SDF;
13
14using PDFNetUniversalSamples.ViewModels;
15
16namespace PDFNetSamples
17{
18 public sealed class ImpositionTest : Sample
19 {
20 public ImpositionTest() :
21 base("Imposition", "The sample illustrates how multiple pages can be combined/imposed using PDFNet. Page imposition can be used to arrange/order pages prior to printing or to assemble a 'master' page from several 'source' pages. Using PDFNet API it is 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.")
22 {
23 }
24
25 public override IAsyncAction RunAsync()
26 {
27 return Task.Run(new System.Action(async () => {
28 WriteLine("--------------------------------");
29 WriteLine("Starting Imposition Test...");
30 WriteLine("--------------------------------\n");
31 try
32 {
33 string input_file_path = Path.Combine(InputPath, "newsletter.pdf");
34 WriteLine("Opening input file " + input_file_path);
35 PDFDoc in_doc = new PDFDoc(input_file_path);
36 in_doc.InitSecurityHandler();
37
38 // Create a list of pages to import from one PDF document to another.
39 IList<pdftron.PDF.Page> import_list = new List<pdftron.PDF.Page>();
40 for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next())
41 import_list.Add(itr.Current());
42
43 PDFDoc new_doc = new PDFDoc(); // Create a new document
44 IList<pdftron.PDF.Page> imported_pages = new_doc.ImportPages(import_list);
45
46 // Paper dimension for A3 format in points. Because one inch has
47 // 72 points, 11.69 inch 72 = 841.69 points
48 pdftron.PDF.Rect media_box = new pdftron.PDF.Rect(0, 0, 1190.88, 841.69);
49 double mid_point = media_box.Width()/2;
50
51 ElementBuilder builder = new ElementBuilder();
52 ElementWriter writer = new ElementWriter();
53
54 for (int i=0; i<imported_pages.Count; ++i)
55 {
56 // Create a blank new A3 page and place on it two pages from the input document.
57 pdftron.PDF.Page new_page = new_doc.PageCreate(media_box);
58 writer.Begin(new_page);
59
60 // Place the first page
61 pdftron.PDF.Page src_page = (pdftron.PDF.Page)imported_pages[i];
62 Element element = builder.CreateForm(src_page);
63
64 double sc_x = mid_point / src_page.GetPageWidth();
65 double sc_y = media_box.Height() / src_page.GetPageHeight();
66 double scale = Math.Min(sc_x, sc_y);
67 element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0);
68 writer.WritePlacedElement(element);
69
70 // Place the second page
71 ++i;
72 if (i<imported_pages.Count)
73 {
74 src_page = (pdftron.PDF.Page)imported_pages[i];
75 element = builder.CreateForm(src_page);
76 sc_x = mid_point / src_page.GetPageWidth();
77 sc_y = media_box.Height() / src_page.GetPageHeight();
78 scale = Math.Min(sc_x, sc_y);
79 element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0);
80 writer.WritePlacedElement(element);
81 }
82
83 writer.End();
84 new_doc.PagePushBack(new_page);
85 }
86
87 string output_file_path = Path.Combine(OutputPath, "newsletter_booklet.pdf");
88 await new_doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
89 new_doc.Destroy();
90 in_doc.Destroy();
91 WriteLine("Done. Results saved in " + output_file_path);
92 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
93 }
94 catch (Exception e)
95 {
96 WriteLine(GetExceptionMessage(e));
97 }
98
99 WriteLine("\n--------------------------------");
100 WriteLine("Done Annotation Test.");
101 WriteLine("--------------------------------\n");
102 })).AsAsyncAction();
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