> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/get-started/samples/impositiontest.md).

# Combine PDFs with Page Imposition - Imposition

Sample code for using Apryse SDK to combine multiple PDF pages using impose functionality. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

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](/core/get-started/frameworks/dotnet.md) and [PDF Editing & Manipulation Library](/core/page-manipulation/manipulation.md).

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------
using System;
using System.IO;
using System.Collections;
using pdftron;
using pdftron.Common;
using pdftron.SDF;
using pdftron.PDF;

//-----------------------------------------------------------------------------------
// 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. 
//-----------------------------------------------------------------------------------

namespace ImpositionTestCS
{
	class Class1
	{
		private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
		static Class1() {}
		
		static void Main(string[] args)
		{
			PDFNet.Initialize(PDFTronLicense.Key);

			// Relative path to the folder containing test files.
			string input_path =  "../../../../TestFiles/";
			string output_path = "../../../../TestFiles/Output/";

			try	
			{    
				Console.WriteLine("-------------------------------------------------");
				Console.WriteLine("Opening the input pdf...");
				using (PDFDoc in_doc = new PDFDoc(input_path + "newsletter.pdf"))
				{
					in_doc.InitSecurityHandler();

					// Create a list of pages to import from one PDF document to another.
					ArrayList import_list = new ArrayList(); 
					for (PageIterator itr = in_doc.GetPageIterator(); itr.HasNext(); itr.Next()) 
						import_list.Add(itr.Current());

					using (PDFDoc new_doc = new PDFDoc()) //  Create a new document
					using (ElementBuilder builder = new ElementBuilder())
					using (ElementWriter  writer  = new ElementWriter())
					{
						ArrayList imported_pages = new_doc.ImportPages(import_list);

						// Paper dimension for A3 format in points. Because one inch has 
						// 72 points, 11.69 inch 72 = 841.69 points
						Rect media_box= new Rect(0, 0, 1190.88, 841.69); 
						double mid_point = media_box.Width()/2;

						for (int i=0; i<imported_pages.Count; ++i)
						{
							// Create a blank new A3 page and place on it two pages from the input document.
							Page new_page = new_doc.PageCreate(media_box);
							writer.Begin(new_page);

							// Place the first page
							Page src_page = (Page)imported_pages[i];
							Element element = builder.CreateForm(src_page);

							double sc_x = mid_point / src_page.GetPageWidth();
							double sc_y = media_box.Height() / src_page.GetPageHeight();
							double scale = Math.Min(sc_x, sc_y);
							element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0);
							writer.WritePlacedElement(element);

							// Place the second page
							++i; 
							if (i<imported_pages.Count)	
							{
								src_page = (Page)imported_pages[i];
								element = builder.CreateForm(src_page);
								sc_x = mid_point / src_page.GetPageWidth();
								sc_y = media_box.Height() / src_page.GetPageHeight();
								scale = Math.Min(sc_x, sc_y);
								element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0);
								writer.WritePlacedElement(element);
							}

							writer.End();
							new_doc.PagePushBack(new_page); 
						}
						new_doc.Save(output_path + "newsletter_booklet.pdf", SDFDoc.SaveOptions.e_linearized);
						Console.WriteLine("Done. Result saved in newsletter_booklet.pdf...");
					}
				}
			}
			catch (Exception e)
			{
				Console.WriteLine("Exception caught:\n{0}", e);
			}
			PDFNet.Terminate();
		}
	}
}
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------

package main
import (
	"fmt"
    "os"
	. "pdftron"
)

import  "pdftron/Samples/LicenseKey/GO"

//-----------------------------------------------------------------------------------
// 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. 
//-----------------------------------------------------------------------------------

func main(){
    PDFNetInitialize(PDFTronLicense.Key)

    //var resource_path = ""
    //if len(os.Args) > 3{
    //    resource_path = os.Args[3]
    //}else{
    //    resource_path = "../../../resources"
    //}
    
    // Relative path to the folder containing the test files.
    inputPath := "../../TestFiles/newsletter.pdf"
    outputPath := "../../TestFiles/Output/newsletter_booklet.pdf"
    
    fmt.Println("-------------------------------------------------")
    fmt.Println("Opening the input pdf...")
    
    var filein = ""
    if len(os.Args) > 1{
        filein = os.Args[1]
    }else{
        filein = inputPath
    }
    var fileout = ""
    if len(os.Args) > 2{
        fileout = os.Args[2]
    }else{
        fileout = outputPath
    }
    
    inDoc := NewPDFDoc(filein)
    inDoc.InitSecurityHandler()
    
    // Create a list of pages to import from one PDF document to another
    importPages := NewVectorPage()
    itr := inDoc.GetPageIterator()
    for itr.HasNext(){
        importPages.Add(itr.Current())
        itr.Next()
    }

    newDoc := NewPDFDoc()
    importedPages := newDoc.ImportPages(importPages)

    // Paper dimension for A3 format in points. Because one inch has 
    // 72 points, 11.69 inch 72 = 841.69 points
    mediaDox := NewRect(0.0, 0.0, 1190.88, 841.69)
    midPoint := mediaDox.Width()/2

    builder := NewElementBuilder()
    writer := NewElementWriter()

    i := 0    
    for i < int(importedPages.Size()){
        // Create a blank new A3 page and place on it two pages from the input document.
        newPage := newDoc.PageCreate(mediaDox)
        writer.Begin(newPage)
        
        // Place the first page
        srcPage := importedPages.Get(i)
        
        element := builder.CreateForm(importedPages.Get(i))
        sc_x := midPoint / srcPage.GetPageWidth()
        sc_y := mediaDox.Height() / srcPage.GetPageHeight()
        var scale = 0.0
        if sc_x < sc_y { // min(sc_x, sc_y)
            scale = sc_x
        }else{
            scale = sc_y
        } 
        element.GetGState().SetTransform(scale, 0.0, 0.0, scale, 0.0, 0.0)
        writer.WritePlacedElement(element)
        
        // Place the second page
        i = i + 1
        if i < int(importedPages.Size()){
            srcPage = importedPages.Get(i)
            element = builder.CreateForm(srcPage)
            sc_x = midPoint / srcPage.GetPageWidth()
            sc_y = mediaDox.Height() / srcPage.GetPageHeight()
            if sc_x < sc_y { // min(sc_x, sc_y)
                scale = sc_x
            }else{
                scale = sc_y
            } 
            element.GetGState().SetTransform(scale, 0.0, 0.0, scale, midPoint, 0.0)
            writer.WritePlacedElement(element)
        }    
        writer.End()
        newDoc.PagePushBack(newPage)
        i = i + 1
    }    
    newDoc.Save(fileout, uint(SDFDocE_linearized))
    PDFNetTerminate()
    fmt.Println("Done. Result saved in newsletter_booklet.pdf...")
}
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/ElementBuilder.h>
#include <PDF/ElementWriter.h>
#include <PDF/ElementReader.h>
#include <iostream>
#include "../../LicenseKey/CPP/LicenseKey.h"

using namespace std;
using namespace pdftron;
using namespace Common;
using namespace SDF;
using namespace PDF;

//-----------------------------------------------------------------------------------
// 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. 
//-----------------------------------------------------------------------------------

int main(int argc, char *argv[])
{
	int ret = 0;
	PDFNet::Initialize(LicenseKey);

	const char* resource_path = argc>3 ? argv[3] : "../../../resources";

	// Relative path to the folder containing test files.
	string input_path =  "../../TestFiles/newsletter.pdf";
	string output_path = "../../TestFiles/Output/newsletter_booklet.pdf";

	try
	{	 
		cout << "-------------------------------------------------" << endl;
		cout << "Opening the input pdf..." << endl;

		const char* filein = argc>1 ? argv[1] : input_path.c_str();
		const char* fileout = argc>2 ? argv[2] : output_path.c_str();

		PDFDoc in_doc(filein);
		in_doc.InitSecurityHandler();

		// Create a list of pages to import from one PDF document to another.
		vector<Page> import_pages; 
		for (PageIterator itr=in_doc.GetPageIterator(); itr.HasNext(); itr.Next())
			import_pages.push_back(itr.Current());

		PDFDoc new_doc;
		vector<Page> imported_pages = new_doc.ImportPages(import_pages);

		// Paper dimension for A3 format in points. Because one inch has 
		// 72 points, 11.69 inch 72 = 841.69 points
		Rect media_box(0, 0, 1190.88, 841.69); 
		double mid_point = media_box.Width()/2;

		ElementBuilder builder;
		ElementWriter  writer;
	
		for (size_t i=0; i<imported_pages.size(); ++i)
		{
			// Create a blank new A3 page and place on it two pages from the input document.
			Page new_page = new_doc.PageCreate(media_box);
			writer.Begin(new_page);

			// Place the first page
			Page src_page = imported_pages[i];
			Element element = builder.CreateForm(src_page);

			double sc_x = mid_point / src_page.GetPageWidth();
			double sc_y = media_box.Height() / src_page.GetPageHeight();
			double scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
			element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0);
			writer.WritePlacedElement(element);
			
			// Place the second page
			++i; 
			if (i<imported_pages.size())	{
				src_page = imported_pages[i];
				element = builder.CreateForm(src_page);
				sc_x = mid_point / src_page.GetPageWidth();
				sc_y = media_box.Height() / src_page.GetPageHeight();
				scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
				element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0);
				writer.WritePlacedElement(element);
			}

			writer.End();
			new_doc.PagePushBack(new_page);
		}		

		new_doc.Save(fileout, SDFDoc::e_linearized, 0);
		cout << "Done. Result saved in newsletter_booklet.pdf..." << endl;
	}
	catch(Common::Exception& e)
	{
		cout << e << endl;
		ret = 1;
	}
	catch(...)
	{
		cout << "Unknown Exception" << endl;
		ret = 1;
	}

	PDFNet::Terminate();
	return ret;
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import com.pdftron.pdf.*;
import com.pdftron.sdf.SDFDoc;

//-----------------------------------------------------------------------------------
// 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. 
//-----------------------------------------------------------------------------------
public class ImpositionTest {

    public static void main(String[] args) {
        PDFNet.initialize(PDFTronLicense.Key());

        // Relative path to the folder containing test files.
        String input_path = "../../TestFiles/";
        String output_path = "../../TestFiles/Output/";

        String filein = input_path + "newsletter.pdf";
        String fileout = output_path + "newsletter_booklet.pdf";

        System.out.println("-------------------------------------------------");
        System.out.println("Opening the input pdf...");
        try (PDFDoc in_doc = new PDFDoc(filein)) {
            in_doc.initSecurityHandler();

            // Create a list of pages to import from one PDF document to another.
            Page[] copy_pages = new Page[in_doc.getPageCount()];
            int j = 0;
            for (PageIterator itr = in_doc.getPageIterator(); itr.hasNext(); j++) {
                copy_pages[j] = itr.next();
            }

            try (PDFDoc new_doc = new PDFDoc()) {
                Page[] imported_pages = new_doc.importPages(copy_pages);

                // Paper dimension for A3 format in points. Because one inch has
                // 72 points, 11.69 inch 72 = 841.69 points
                Rect media_box = new Rect(0, 0, 1190.88, 841.69);
                double mid_point = media_box.getWidth() / 2;

                ElementBuilder builder = new ElementBuilder();
                ElementWriter writer = new ElementWriter();

                for (int i = 0; i < imported_pages.length; ++i) {
                    // Create a blank new A3 page and place on it two pages from the input document.
                    Page new_page = new_doc.pageCreate(media_box);
                    writer.begin(new_page);

                    // Place the first page
                    Page src_page = imported_pages[i];
                    Element element = builder.createForm(src_page);

                    double sc_x = mid_point / src_page.getPageWidth();
                    double sc_y = media_box.getHeight() / src_page.getPageHeight();
                    double scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
                    element.getGState().setTransform(scale, 0, 0, scale, 0, 0);
                    writer.writePlacedElement(element);

                    // Place the second page
                    ++i;
                    if (i < imported_pages.length) {
                        src_page = imported_pages[i];
                        element = builder.createForm(src_page);
                        sc_x = mid_point / src_page.getPageWidth();
                        sc_y = media_box.getHeight() / src_page.getPageHeight();
                        scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
                        element.getGState().setTransform(scale, 0, 0, scale, mid_point, 0);
                        writer.writePlacedElement(element);
                    }

                    writer.end();
                    new_doc.pagePushBack(new_page);
                }

                new_doc.save(fileout, SDFDoc.SaveMode.LINEARIZED, null);
                System.out.println("Done. Result saved in newsletter_booklet.pdf...");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        PDFNet.terminate();
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------
// 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. 
//-----------------------------------------------------------------------------------

const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');

((exports) => {
  'use strict';

  exports.runImpositionTest = () => {
    const main = async () => {
      try {
        console.log('-------------------------------------------------');
        console.log('Opening the input pdf...');
        const in_doc = await PDFNet.PDFDoc.createFromFilePath('../TestFiles/newsletter.pdf');
        in_doc.initSecurityHandler();

        // Create a list of pages to import from one PDF document to another.
        const import_pages = [];
        for (let itr = await in_doc.getPageIterator(); await itr.hasNext(); await itr.next()) {
          import_pages.push(await itr.current());
        }

        const new_doc = await PDFNet.PDFDoc.create();
        const imported_pages = await new_doc.importPages(import_pages);

        // Paper dimension for A3 format in points. Because one inch has 
        // 72 points, 11.69 inch 72 = 841.69 points
        const media_box = await PDFNet.Rect.init(0, 0, 1190.88, 841.69);
        const mid_point = await media_box.width() / 2;
        const builder = await PDFNet.ElementBuilder.create();
        const writer = await PDFNet.ElementWriter.create();
        for (let i = 0; i < imported_pages.length; ++i) {
          // Create a blank new A3 page and place on it two pages from the input document.
          const new_page = await new_doc.pageCreate(media_box);
          writer.beginOnPage(new_page);
          // Place the first page
          let src_page = imported_pages[i];
          var element = await builder.createFormFromPage(src_page);

          let sc_x = mid_point / await src_page.getPageWidth();
          let sc_y = await media_box.height() / await src_page.getPageHeight();
          let scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
          await element.getGState().then(gstate => gstate.setTransform(scale, 0, 0, scale, 0, 0));
          writer.writePlacedElement(element);

          // Place the second page
          ++i;
          if (i < imported_pages.length) {
            src_page = imported_pages[i];
            element = await builder.createFormFromPage(src_page);
            sc_x = mid_point / await src_page.getPageWidth();
            sc_y = await media_box.height() / await src_page.getPageHeight();
            scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
            await element.getGState().then(gstate => gstate.setTransform(scale, 0, 0, scale, mid_point, 0));
            writer.writePlacedElement(element);
          }

          await writer.end();
          new_doc.pagePushBack(new_page);
        }
        await new_doc.save('../TestFiles/Output/newsletter_booklet.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
        console.log('Done. Result saved in newsletter_booklet.pdf...');

      } catch (err) {
        console.log(err);
      }
    }
    PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
      console.log('Error: ' + JSON.stringify(error));
    }).then(function(){ return PDFNet.shutdown(); });
  };
  exports.runImpositionTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=ImpositionTest.js
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
<?php
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------
if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
include("../../../PDFNetC/Lib/PDFNetPHP.php");
include("../../LicenseKey/PHP/LicenseKey.php");

// Relative path to the folder containing the test files.
$input_path = getcwd()."/../../TestFiles/";
$output_path = $input_path."Output/newsletter_booklet.pdf";

//-----------------------------------------------------------------------------------
// 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. 
//-----------------------------------------------------------------------------------

	PDFNet::Initialize($LicenseKey);
	PDFNet::GetSystemFontList();    // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
	
	echo nl2br("-------------------------------------------------\n");
	echo nl2br("Opening the input pdf...\n");

	$in_doc = new PDFDoc($input_path."newsletter.pdf");
	$in_doc->InitSecurityHandler();

	// Create a list of pages to import from one PDF document to another.
	$import_pages = new VectorPage(); 
	for ($itr=$in_doc->GetPageIterator(); $itr->HasNext(); $itr->Next())
		$import_pages->push($itr->Current());

	$new_doc = new PDFDoc();
	$imported_pages = $new_doc->ImportPages($import_pages);

	// Paper dimension for A3 format in points. Because one inch has 
	// 72 points, 11.69 inch 72 = 841.69 points
	$media_box = new Rect(0.0, 0.0, 1190.88, 841.69); 
	$mid_point = $media_box->Width()/2;

	$builder = new ElementBuilder();
	$writer = new ElementWriter();

	for ($i=0; $i<$imported_pages->size(); ++$i)
	{
		// Create a blank new A3 page and place on it two pages from the input document.
		$new_page = $new_doc->PageCreate($media_box);
		$writer->Begin($new_page);

		// Place the first page
		$src_page = $imported_pages->get($i);
		$element = $builder->CreateForm($src_page);

		$sc_x = $mid_point / $src_page->GetPageWidth();
		$sc_y = $media_box->Height() / $src_page->GetPageHeight();
		$scale = $sc_x < $sc_y ? $sc_x : $sc_y; // min(sc_x, sc_y)
		$element->GetGState()->SetTransform($scale, 0.0, 0.0, $scale, 0.0, 0.0);
		$writer->WritePlacedElement($element);
			
		// Place the second page
		++$i; 
		if ($i<$imported_pages->size())	{
			$src_page = $imported_pages->get($i);
			$element = $builder->CreateForm($src_page);
			$sc_x = $mid_point / $src_page->GetPageWidth();
			$sc_y = $media_box->Height() / $src_page->GetPageHeight();
			$scale = $sc_x < $sc_y ? $sc_x : $sc_y; // min(sc_x, sc_y)
			$element->GetGState()->SetTransform($scale, 0.0, 0.0, $scale, $mid_point, 0.0);
			$writer->WritePlacedElement($element);
		}

		$writer->End();
		$new_doc->PagePushBack($new_page);
	}		

	$new_doc->Save($output_path, SDFDoc::e_linearized);
	PDFNet::Terminate();
	echo nl2br("Done. Result saved in newsletter_booklet.pdf...");	
?>
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

import site
site.addsitedir("../../../PDFNetC/Lib")
import sys
from PDFNetPython import *

sys.path.append("../../LicenseKey/PYTHON")
from LicenseKey import *

#-----------------------------------------------------------------------------------
# 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. 
#-----------------------------------------------------------------------------------

def main(args):
    PDFNet.Initialize(LicenseKey)
    
    resource_path = args[3] if len(args) > 3 else "../../../resources"
    
    # Relative path to the folder containing the test files.
    input_path = "../../TestFiles/newsletter.pdf"
    output_path = "../../TestFiles/Output/newsletter_booklet.pdf"
    
    print("-------------------------------------------------")
    print("Opening the input pdf...")
    
    filein = args[1] if len(args)>1 else input_path
    fileout = args[2] if len(args)>2 else output_path
    
    in_doc = PDFDoc(filein)
    in_doc.InitSecurityHandler()
    
    # Create a list of pages to import from one PDF document to another
    import_pages = VectorPage()
    itr = in_doc.GetPageIterator()
    while itr.HasNext():
        import_pages.append(itr.Current())
        itr.Next()

    new_doc = PDFDoc()
    imported_pages = new_doc.ImportPages(import_pages)

    # Paper dimension for A3 format in points. Because one inch has 
    # 72 points, 11.69 inch 72 = 841.69 points
    media_box = Rect(0, 0, 1190.88, 841.69)
    mid_point = media_box.Width()/2

    builder = ElementBuilder()
    writer = ElementWriter()

    i = 0    
    while i < len(imported_pages):
        # Create a blank new A3 page and place on it two pages from the input document.
        new_page = new_doc.PageCreate(media_box)
        writer.Begin(new_page)
        
        # Place the first page
        src_page = imported_pages[i]
        
        element = builder.CreateForm(imported_pages[i])
        sc_x = mid_point / src_page.GetPageWidth()
        sc_y = media_box.Height() / src_page.GetPageHeight()
        scale = sc_x if sc_x < sc_y else sc_y # min(sc_x, sc_y)
        element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0)
        writer.WritePlacedElement(element)
        
        # Place the second page
        i = i + 1
        if i < len(imported_pages):
            src_page = imported_pages[i]
            element = builder.CreateForm(src_page)
            sc_x = mid_point / src_page.GetPageWidth()
            sc_y = media_box.Height() / src_page.GetPageHeight()
            scale = sc_x if sc_x < sc_y else sc_y # min(sc_x, sc_y)
            element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0)
            writer.WritePlacedElement(element)
            
        writer.End()
        new_doc.PagePushBack(new_page)
        i = i + 1
        
    new_doc.Save(fileout, SDFDoc.e_linearized)
    PDFNet.Terminate()
    print("Done. Result saved in newsletter_booklet.pdf...")
    
if __name__ == '__main__':
    args = []
    main(args)
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
#---------------------------------------------------------------------------------------
# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
# Consult LICENSE.txt regarding license information.
#---------------------------------------------------------------------------------------

require '../../../PDFNetC/Lib/PDFNetRuby'
include PDFNetRuby
require '../../LicenseKey/RUBY/LicenseKey'

$stdout.sync = true

# Relative path to the folder containing the test files.
input_path = "../../TestFiles/newsletter.pdf"
output_path = "../../TestFiles/Output/newsletter_booklet.pdf"

#-----------------------------------------------------------------------------------
# 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. 
#-----------------------------------------------------------------------------------

	PDFNet.Initialize(PDFTronLicense.Key)
	
	puts "-------------------------------------------------"
	puts "Opening the input pdf..."
	
	in_doc = PDFDoc.new(input_path)
	in_doc.InitSecurityHandler
	
	# Create a list of pages to import from one PDF document to another
	import_pages = VectorPage.new
	itr = in_doc.GetPageIterator
	while itr.HasNext do
		import_pages << (itr.Current)
		itr.Next
	end

	new_doc = PDFDoc.new
	imported_pages = new_doc.ImportPages(import_pages)

	# Paper dimension for A3 format in points. Because one inch has 
	# 72 points, 11.69 inch 72 = 841.69 points
	media_box = Rect.new(0, 0, 1190.88, 841.69)
	mid_point = media_box.Width/2

	builder = ElementBuilder.new
	writer = ElementWriter.new

	i = 0	
	while i < imported_pages.size do
		# Create a blank new A3 page and place on it two pages from the input document.
		new_page = new_doc.PageCreate(media_box)
		writer.Begin(new_page)
		
		# Place the first page
		src_page = imported_pages[i]
		
		element = builder.CreateForm(imported_pages[i])
		sc_x = mid_point / src_page.GetPageWidth
		sc_y = media_box.Height / src_page.GetPageHeight
		scale = sc_x < sc_y ? sc_x : sc_y	# min(sc_x, sc_y)
		element.GetGState.SetTransform(scale, 0, 0, scale, 0, 0)
		writer.WritePlacedElement(element)
		
		# Place the second page
		i = i + 1
		if i < imported_pages.size
			src_page = imported_pages[i]
			element = builder.CreateForm(src_page)
			sc_x = mid_point / src_page.GetPageWidth
			sc_y = media_box.Height / src_page.GetPageHeight
			scale = sc_x < sc_y ? sc_x : sc_y	# min(sc_x, sc_y)
			element.GetGState.SetTransform(scale, 0, 0, scale, mid_point, 0)
			writer.WritePlacedElement(element)
		end
			
		writer.End
		new_doc.PagePushBack(new_page)
		i = i + 1
	end
		
	new_doc.Save(output_path, SDFDoc::E_linearized)
	PDFNet.Terminate
	puts "Done. Result saved in newsletter_booklet.pdf..."
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
'
' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
'

Imports System
Imports System.Collections

Imports pdftron
Imports pdftron.Common
Imports pdftron.Filters
Imports pdftron.SDF
Imports pdftron.PDF

'-----------------------------------------------------------------------------------
' 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. 
'-----------------------------------------------------------------------------------

Module ImpositionTestVB
	Dim pdfNetLoader As PDFNetLoader
	Sub New()
		pdfNetLoader = pdftron.PDFNetLoader.Instance()
	End Sub

	Sub Main()

		PDFNet.Initialize(PDFTronLicense.Key)

		' Relative path to the folder containing test files.
		Dim input_path As String = "../../../../TestFiles/"
		Dim output_path As String = "../../../../TestFiles/Output/"

		Try
			Console.WriteLine("-------------------------------------------------")
			Console.WriteLine("Opening the input pdf...")
			Using in_doc As PDFDoc = New PDFDoc(input_path + "newsletter.pdf")
				in_doc.InitSecurityHandler()

				' Create a list of pages to import from one PDF document to another.
				Dim import_list As ArrayList = New ArrayList
				Dim itr As PageIterator = in_doc.GetPageIterator()
				While itr.HasNext()
					import_list.Add(itr.Current())
					itr.Next()
				End While

				Using new_doc As PDFDoc = New PDFDoc		  ' Create a new document
					Dim imported_pages As ArrayList = new_doc.ImportPages(import_list)

					' Paper dimension for A3 format in points. Because one inch has 
					' 72 points, 11.69 inch 72 = 841.69 points
					Dim media_box As Rect = New Rect(0, 0, 1190.88, 841.69)
					Dim mid_point As Double = media_box.Width() / 2

					Using builder As ElementBuilder = New ElementBuilder
						Using writer As ElementWriter = New ElementWriter

							Dim i As Integer = 0
							While i < imported_pages.Count
								' Create a blank new A3 page and place on it two pages from the input document.
								Dim new_page As Page = new_doc.PageCreate(media_box)
								writer.Begin(new_page)

								' Place the first page
								Dim src_page As Page = imported_pages(i)
								Dim element As Element = builder.CreateForm(src_page)

								Dim sc_x As Double = mid_point / src_page.GetPageWidth()
								Dim sc_y As Double = media_box.Height() / src_page.GetPageHeight()
								Dim scale As Double = Math.Min(sc_x, sc_y)
								element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0)
								writer.WritePlacedElement(element)

								' Place the second page
								i = i + 1
								If i < imported_pages.Count Then
									src_page = imported_pages(i)
									element = builder.CreateForm(src_page)

									sc_x = mid_point / src_page.GetPageWidth()
									sc_y = media_box.Height() / src_page.GetPageHeight()
									scale = Math.Min(sc_x, sc_y)
									element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0)
									writer.WritePlacedElement(element)
									i = i + 1
								End If

								writer.End()
								new_doc.PagePushBack(new_page)
							End While

						End Using
					End Using

					new_doc.Save(output_path + "newsletter_booklet.pdf", SDFDoc.SaveOptions.e_linearized)

				End Using
			End Using
			Console.WriteLine("Done. Result saved in newsletter_booklet.pdf...")

		Catch e As Exception
			Console.WriteLine("Exception caught:\n{0}", e)
		End Try
		PDFNet.Terminate()
	End Sub

End Module
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/get-started/samples/impositiontest.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
