1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems 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
22using NUnit.Framework;
23
24namespace MiscellaneousSamples
25{
26 [TestFixture]
27 public class ImpositionTest
28 {
29
30 [Test]
31 public static void Sample()
32 {
33
34 // Relative path to the folder containing test files.
35 const string input_path = "TestFiles/";
36
37 try
38 {
39 Console.WriteLine("-------------------------------------------------");
40 Console.WriteLine("Opening the input pdf...");
41 using (PDFDoc in_doc = new PDFDoc(Utils.GetAssetTempFile(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(Utils.CreateExternalFile("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 Assert.True(false);
102 }
103 }
104 }
105}