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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "os"
10 . "pdftron"
11)
12
13import "pdftron/Samples/LicenseKey/GO"
14
15//-----------------------------------------------------------------------------------
16// The sample illustrates how multiple pages can be combined/imposed
17// using PDFNet. Page imposition can be used to arrange/order pages
18// prior to printing or to assemble a 'master' page from several 'source'
19// pages. Using PDFNet API it is possible to write applications that can
20// re-order the pages such that they will display in the correct order
21// when the hard copy pages are compiled and folded correctly.
22//-----------------------------------------------------------------------------------
23
24func main(){
25 PDFNetInitialize(PDFTronLicense.Key)
26
27 //var resource_path = ""
28 //if len(os.Args) > 3{
29 // resource_path = os.Args[3]
30 //}else{
31 // resource_path = "../../../resources"
32 //}
33
34 // Relative path to the folder containing the test files.
35 inputPath := "../../TestFiles/newsletter.pdf"
36 outputPath := "../../TestFiles/Output/newsletter_booklet.pdf"
37
38 fmt.Println("-------------------------------------------------")
39 fmt.Println("Opening the input pdf...")
40
41 var filein = ""
42 if len(os.Args) > 1{
43 filein = os.Args[1]
44 }else{
45 filein = inputPath
46 }
47 var fileout = ""
48 if len(os.Args) > 2{
49 fileout = os.Args[2]
50 }else{
51 fileout = outputPath
52 }
53
54 inDoc := NewPDFDoc(filein)
55 inDoc.InitSecurityHandler()
56
57 // Create a list of pages to import from one PDF document to another
58 importPages := NewVectorPage()
59 itr := inDoc.GetPageIterator()
60 for itr.HasNext(){
61 importPages.Add(itr.Current())
62 itr.Next()
63 }
64
65 newDoc := NewPDFDoc()
66 importedPages := newDoc.ImportPages(importPages)
67
68 // Paper dimension for A3 format in points. Because one inch has
69 // 72 points, 11.69 inch 72 = 841.69 points
70 mediaDox := NewRect(0.0, 0.0, 1190.88, 841.69)
71 midPoint := mediaDox.Width()/2
72
73 builder := NewElementBuilder()
74 writer := NewElementWriter()
75
76 i := 0
77 for i < int(importedPages.Size()){
78 // Create a blank new A3 page and place on it two pages from the input document.
79 newPage := newDoc.PageCreate(mediaDox)
80 writer.Begin(newPage)
81
82 // Place the first page
83 srcPage := importedPages.Get(i)
84
85 element := builder.CreateForm(importedPages.Get(i))
86 sc_x := midPoint / srcPage.GetPageWidth()
87 sc_y := mediaDox.Height() / srcPage.GetPageHeight()
88 var scale = 0.0
89 if sc_x < sc_y { // min(sc_x, sc_y)
90 scale = sc_x
91 }else{
92 scale = sc_y
93 }
94 element.GetGState().SetTransform(scale, 0.0, 0.0, scale, 0.0, 0.0)
95 writer.WritePlacedElement(element)
96
97 // Place the second page
98 i = i + 1
99 if i < int(importedPages.Size()){
100 srcPage = importedPages.Get(i)
101 element = builder.CreateForm(srcPage)
102 sc_x = midPoint / srcPage.GetPageWidth()
103 sc_y = mediaDox.Height() / srcPage.GetPageHeight()
104 if sc_x < sc_y { // min(sc_x, sc_y)
105 scale = sc_x
106 }else{
107 scale = sc_y
108 }
109 element.GetGState().SetTransform(scale, 0.0, 0.0, scale, midPoint, 0.0)
110 writer.WritePlacedElement(element)
111 }
112 writer.End()
113 newDoc.PagePushBack(newPage)
114 i = i + 1
115 }
116 newDoc.Save(fileout, uint(SDFDocE_linearized))
117 PDFNetTerminate()
118 fmt.Println("Done. Result saved in newsletter_booklet.pdf...")
119}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/ElementBuilder.h>
9#include <PDF/ElementWriter.h>
10#include <PDF/ElementReader.h>
11#include <iostream>
12#include "../../LicenseKey/CPP/LicenseKey.h"
13
14using namespace std;
15using namespace pdftron;
16using namespace Common;
17using namespace SDF;
18using namespace PDF;
19
20//-----------------------------------------------------------------------------------
21// The sample illustrates how multiple pages can be combined/imposed
22// using PDFNet. Page imposition can be used to arrange/order pages
23// prior to printing or to assemble a 'master' page from several 'source'
24// pages. Using PDFNet API it is possible to write applications that can
25// re-order the pages such that they will display in the correct order
26// when the hard copy pages are compiled and folded correctly.
27//-----------------------------------------------------------------------------------
28
29int main(int argc, char *argv[])
30{
31 int ret = 0;
32 PDFNet::Initialize(LicenseKey);
33
34 const char* resource_path = argc>3 ? argv[3] : "../../../resources";
35
36 // Relative path to the folder containing test files.
37 string input_path = "../../TestFiles/newsletter.pdf";
38 string output_path = "../../TestFiles/Output/newsletter_booklet.pdf";
39
40 try
41 {
42 cout << "-------------------------------------------------" << endl;
43 cout << "Opening the input pdf..." << endl;
44
45 const char* filein = argc>1 ? argv[1] : input_path.c_str();
46 const char* fileout = argc>2 ? argv[2] : output_path.c_str();
47
48 PDFDoc in_doc(filein);
49 in_doc.InitSecurityHandler();
50
51 // Create a list of pages to import from one PDF document to another.
52 vector<Page> import_pages;
53 for (PageIterator itr=in_doc.GetPageIterator(); itr.HasNext(); itr.Next())
54 import_pages.push_back(itr.Current());
55
56 PDFDoc new_doc;
57 vector<Page> imported_pages = new_doc.ImportPages(import_pages);
58
59 // Paper dimension for A3 format in points. Because one inch has
60 // 72 points, 11.69 inch 72 = 841.69 points
61 Rect media_box(0, 0, 1190.88, 841.69);
62 double mid_point = media_box.Width()/2;
63
64 ElementBuilder builder;
65 ElementWriter writer;
66
67 for (size_t i=0; i<imported_pages.size(); ++i)
68 {
69 // Create a blank new A3 page and place on it two pages from the input document.
70 Page new_page = new_doc.PageCreate(media_box);
71 writer.Begin(new_page);
72
73 // Place the first page
74 Page src_page = imported_pages[i];
75 Element element = builder.CreateForm(src_page);
76
77 double sc_x = mid_point / src_page.GetPageWidth();
78 double sc_y = media_box.Height() / src_page.GetPageHeight();
79 double scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
80 element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0);
81 writer.WritePlacedElement(element);
82
83 // Place the second page
84 ++i;
85 if (i<imported_pages.size()) {
86 src_page = imported_pages[i];
87 element = builder.CreateForm(src_page);
88 sc_x = mid_point / src_page.GetPageWidth();
89 sc_y = media_box.Height() / src_page.GetPageHeight();
90 scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
91 element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0);
92 writer.WritePlacedElement(element);
93 }
94
95 writer.End();
96 new_doc.PagePushBack(new_page);
97 }
98
99 new_doc.Save(fileout, SDFDoc::e_linearized, 0);
100 cout << "Done. Result saved in newsletter_booklet.pdf..." << endl;
101 }
102 catch(Common::Exception& e)
103 {
104 cout << e << endl;
105 ret = 1;
106 }
107 catch(...)
108 {
109 cout << "Unknown Exception" << endl;
110 ret = 1;
111 }
112
113 PDFNet::Terminate();
114 return ret;
115}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.SDFDoc;
8
9//-----------------------------------------------------------------------------------
10// The sample illustrates how multiple pages can be combined/imposed
11// using PDFNet. Page imposition can be used to arrange/order pages
12// prior to printing or to assemble a 'master' page from several 'source'
13// pages. Using PDFNet API it is possible to write applications that can
14// re-order the pages such that they will display in the correct order
15// when the hard copy pages are compiled and folded correctly.
16//-----------------------------------------------------------------------------------
17public class ImpositionTest {
18
19 public static void main(String[] args) {
20 PDFNet.initialize(PDFTronLicense.Key());
21
22 // Relative path to the folder containing test files.
23 String input_path = "../../TestFiles/";
24 String output_path = "../../TestFiles/Output/";
25
26 String filein = input_path + "newsletter.pdf";
27 String fileout = output_path + "newsletter_booklet.pdf";
28
29 System.out.println("-------------------------------------------------");
30 System.out.println("Opening the input pdf...");
31 try (PDFDoc in_doc = new PDFDoc(filein)) {
32 in_doc.initSecurityHandler();
33
34 // Create a list of pages to import from one PDF document to another.
35 Page[] copy_pages = new Page[in_doc.getPageCount()];
36 int j = 0;
37 for (PageIterator itr = in_doc.getPageIterator(); itr.hasNext(); j++) {
38 copy_pages[j] = itr.next();
39 }
40
41 try (PDFDoc new_doc = new PDFDoc()) {
42 Page[] imported_pages = new_doc.importPages(copy_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 Rect media_box = new Rect(0, 0, 1190.88, 841.69);
47 double mid_point = media_box.getWidth() / 2;
48
49 ElementBuilder builder = new ElementBuilder();
50 ElementWriter writer = new ElementWriter();
51
52 for (int i = 0; i < imported_pages.length; ++i) {
53 // Create a blank new A3 page and place on it two pages from the input document.
54 Page new_page = new_doc.pageCreate(media_box);
55 writer.begin(new_page);
56
57 // Place the first page
58 Page src_page = imported_pages[i];
59 Element element = builder.createForm(src_page);
60
61 double sc_x = mid_point / src_page.getPageWidth();
62 double sc_y = media_box.getHeight() / src_page.getPageHeight();
63 double scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
64 element.getGState().setTransform(scale, 0, 0, scale, 0, 0);
65 writer.writePlacedElement(element);
66
67 // Place the second page
68 ++i;
69 if (i < imported_pages.length) {
70 src_page = imported_pages[i];
71 element = builder.createForm(src_page);
72 sc_x = mid_point / src_page.getPageWidth();
73 sc_y = media_box.getHeight() / src_page.getPageHeight();
74 scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
75 element.getGState().setTransform(scale, 0, 0, scale, mid_point, 0);
76 writer.writePlacedElement(element);
77 }
78
79 writer.end();
80 new_doc.pagePushBack(new_page);
81 }
82
83 new_doc.save(fileout, SDFDoc.SaveMode.LINEARIZED, null);
84 System.out.println("Done. Result saved in newsletter_booklet.pdf...");
85 }
86 } catch (Exception e) {
87 e.printStackTrace();
88 }
89
90 PDFNet.terminate();
91 }
92}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6//-----------------------------------------------------------------------------------
7// The sample illustrates how multiple pages can be combined/imposed
8// using PDFNet. Page imposition can be used to arrange/order pages
9// prior to printing or to assemble a 'master' page from several 'source'
10// pages. Using PDFNet API it is possible to write applications that can
11// re-order the pages such that they will display in the correct order
12// when the hard copy pages are compiled and folded correctly.
13//-----------------------------------------------------------------------------------
14
15const { PDFNet } = require('@pdftron/pdfnet-node');
16const PDFTronLicense = require('../LicenseKey/LicenseKey');
17
18((exports) => {
19 'use strict';
20
21 exports.runImpositionTest = () => {
22 const main = async () => {
23 try {
24 console.log('-------------------------------------------------');
25 console.log('Opening the input pdf...');
26 const in_doc = await PDFNet.PDFDoc.createFromFilePath('../TestFiles/newsletter.pdf');
27 in_doc.initSecurityHandler();
28
29 // Create a list of pages to import from one PDF document to another.
30 const import_pages = [];
31 for (let itr = await in_doc.getPageIterator(); await itr.hasNext(); await itr.next()) {
32 import_pages.push(await itr.current());
33 }
34
35 const new_doc = await PDFNet.PDFDoc.create();
36 const imported_pages = await new_doc.importPages(import_pages);
37
38 // Paper dimension for A3 format in points. Because one inch has
39 // 72 points, 11.69 inch 72 = 841.69 points
40 const media_box = await PDFNet.Rect.init(0, 0, 1190.88, 841.69);
41 const mid_point = await media_box.width() / 2;
42 const builder = await PDFNet.ElementBuilder.create();
43 const writer = await PDFNet.ElementWriter.create();
44 for (let i = 0; i < imported_pages.length; ++i) {
45 // Create a blank new A3 page and place on it two pages from the input document.
46 const new_page = await new_doc.pageCreate(media_box);
47 writer.beginOnPage(new_page);
48 // Place the first page
49 let src_page = imported_pages[i];
50 var element = await builder.createFormFromPage(src_page);
51
52 let sc_x = mid_point / await src_page.getPageWidth();
53 let sc_y = await media_box.height() / await src_page.getPageHeight();
54 let scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
55 await element.getGState().then(gstate => gstate.setTransform(scale, 0, 0, scale, 0, 0));
56 writer.writePlacedElement(element);
57
58 // Place the second page
59 ++i;
60 if (i < imported_pages.length) {
61 src_page = imported_pages[i];
62 element = await builder.createFormFromPage(src_page);
63 sc_x = mid_point / await src_page.getPageWidth();
64 sc_y = await media_box.height() / await src_page.getPageHeight();
65 scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
66 await element.getGState().then(gstate => gstate.setTransform(scale, 0, 0, scale, mid_point, 0));
67 writer.writePlacedElement(element);
68 }
69
70 await writer.end();
71 new_doc.pagePushBack(new_page);
72 }
73 await new_doc.save('../TestFiles/Output/newsletter_booklet.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
74 console.log('Done. Result saved in newsletter_booklet.pdf...');
75
76 } catch (err) {
77 console.log(err);
78 }
79 }
80 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
81 console.log('Error: ' + JSON.stringify(error));
82 }).then(function(){ return PDFNet.shutdown(); });
83 };
84 exports.runImpositionTest();
85})(exports);
86// eslint-disable-next-line spaced-comment
87//# sourceURL=ImpositionTest.js
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/newsletter_booklet.pdf";
13
14//-----------------------------------------------------------------------------------
15// The sample illustrates how multiple pages can be combined/imposed
16// using PDFNet. Page imposition can be used to arrange/order pages
17// prior to printing or to assemble a 'master' page from several 'source'
18// pages. Using PDFNet API it is possible to write applications that can
19// re-order the pages such that they will display in the correct order
20// when the hard copy pages are compiled and folded correctly.
21//-----------------------------------------------------------------------------------
22
23 PDFNet::Initialize($LicenseKey);
24 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.
25
26 echo nl2br("-------------------------------------------------\n");
27 echo nl2br("Opening the input pdf...\n");
28
29 $in_doc = new PDFDoc($input_path."newsletter.pdf");
30 $in_doc->InitSecurityHandler();
31
32 // Create a list of pages to import from one PDF document to another.
33 $import_pages = new VectorPage();
34 for ($itr=$in_doc->GetPageIterator(); $itr->HasNext(); $itr->Next())
35 $import_pages->push($itr->Current());
36
37 $new_doc = new PDFDoc();
38 $imported_pages = $new_doc->ImportPages($import_pages);
39
40 // Paper dimension for A3 format in points. Because one inch has
41 // 72 points, 11.69 inch 72 = 841.69 points
42 $media_box = new Rect(0.0, 0.0, 1190.88, 841.69);
43 $mid_point = $media_box->Width()/2;
44
45 $builder = new ElementBuilder();
46 $writer = new ElementWriter();
47
48 for ($i=0; $i<$imported_pages->size(); ++$i)
49 {
50 // Create a blank new A3 page and place on it two pages from the input document.
51 $new_page = $new_doc->PageCreate($media_box);
52 $writer->Begin($new_page);
53
54 // Place the first page
55 $src_page = $imported_pages->get($i);
56 $element = $builder->CreateForm($src_page);
57
58 $sc_x = $mid_point / $src_page->GetPageWidth();
59 $sc_y = $media_box->Height() / $src_page->GetPageHeight();
60 $scale = $sc_x < $sc_y ? $sc_x : $sc_y; // min(sc_x, sc_y)
61 $element->GetGState()->SetTransform($scale, 0.0, 0.0, $scale, 0.0, 0.0);
62 $writer->WritePlacedElement($element);
63
64 // Place the second page
65 ++$i;
66 if ($i<$imported_pages->size()) {
67 $src_page = $imported_pages->get($i);
68 $element = $builder->CreateForm($src_page);
69 $sc_x = $mid_point / $src_page->GetPageWidth();
70 $sc_y = $media_box->Height() / $src_page->GetPageHeight();
71 $scale = $sc_x < $sc_y ? $sc_x : $sc_y; // min(sc_x, sc_y)
72 $element->GetGState()->SetTransform($scale, 0.0, 0.0, $scale, $mid_point, 0.0);
73 $writer->WritePlacedElement($element);
74 }
75
76 $writer->End();
77 $new_doc->PagePushBack($new_page);
78 }
79
80 $new_doc->Save($output_path, SDFDoc::e_linearized);
81 PDFNet::Terminate();
82 echo nl2br("Done. Result saved in newsletter_booklet.pdf...");
83?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14#-----------------------------------------------------------------------------------
15# The sample illustrates how multiple pages can be combined/imposed
16# using PDFNet. Page imposition can be used to arrange/order pages
17# prior to printing or to assemble a 'master' page from several 'source'
18# pages. Using PDFNet API it is possible to write applications that can
19# re-order the pages such that they will display in the correct order
20# when the hard copy pages are compiled and folded correctly.
21#-----------------------------------------------------------------------------------
22
23def main(args):
24 PDFNet.Initialize(LicenseKey)
25
26 resource_path = args[3] if len(args) > 3 else "../../../resources"
27
28 # Relative path to the folder containing the test files.
29 input_path = "../../TestFiles/newsletter.pdf"
30 output_path = "../../TestFiles/Output/newsletter_booklet.pdf"
31
32 print("-------------------------------------------------")
33 print("Opening the input pdf...")
34
35 filein = args[1] if len(args)>1 else input_path
36 fileout = args[2] if len(args)>2 else output_path
37
38 in_doc = PDFDoc(filein)
39 in_doc.InitSecurityHandler()
40
41 # Create a list of pages to import from one PDF document to another
42 import_pages = VectorPage()
43 itr = in_doc.GetPageIterator()
44 while itr.HasNext():
45 import_pages.append(itr.Current())
46 itr.Next()
47
48 new_doc = PDFDoc()
49 imported_pages = new_doc.ImportPages(import_pages)
50
51 # Paper dimension for A3 format in points. Because one inch has
52 # 72 points, 11.69 inch 72 = 841.69 points
53 media_box = Rect(0, 0, 1190.88, 841.69)
54 mid_point = media_box.Width()/2
55
56 builder = ElementBuilder()
57 writer = ElementWriter()
58
59 i = 0
60 while i < len(imported_pages):
61 # Create a blank new A3 page and place on it two pages from the input document.
62 new_page = new_doc.PageCreate(media_box)
63 writer.Begin(new_page)
64
65 # Place the first page
66 src_page = imported_pages[i]
67
68 element = builder.CreateForm(imported_pages[i])
69 sc_x = mid_point / src_page.GetPageWidth()
70 sc_y = media_box.Height() / src_page.GetPageHeight()
71 scale = sc_x if sc_x < sc_y else sc_y # min(sc_x, sc_y)
72 element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0)
73 writer.WritePlacedElement(element)
74
75 # Place the second page
76 i = i + 1
77 if i < len(imported_pages):
78 src_page = imported_pages[i]
79 element = builder.CreateForm(src_page)
80 sc_x = mid_point / src_page.GetPageWidth()
81 sc_y = media_box.Height() / src_page.GetPageHeight()
82 scale = sc_x if sc_x < sc_y else sc_y # min(sc_x, sc_y)
83 element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0)
84 writer.WritePlacedElement(element)
85
86 writer.End()
87 new_doc.PagePushBack(new_page)
88 i = i + 1
89
90 new_doc.Save(fileout, SDFDoc.e_linearized)
91 PDFNet.Terminate()
92 print("Done. Result saved in newsletter_booklet.pdf...")
93
94if __name__ == '__main__':
95 args = []
96 main(args)
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..."
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6Imports System.Collections
7
8Imports pdftron
9Imports pdftron.Common
10Imports pdftron.Filters
11Imports pdftron.SDF
12Imports pdftron.PDF
13
14'-----------------------------------------------------------------------------------
15' The sample illustrates how multiple pages can be combined/imposed
16' using PDFNet. Page imposition can be used to arrange/order pages
17' prior to printing or to assemble a 'master' page from several 'source'
18' pages. Using PDFNet API it is possible to write applications that can
19' re-order the pages such that they will display in the correct order
20' when the hard copy pages are compiled and folded correctly.
21'-----------------------------------------------------------------------------------
22
23Module ImpositionTestVB
24 Dim pdfNetLoader As PDFNetLoader
25 Sub New()
26 pdfNetLoader = pdftron.PDFNetLoader.Instance()
27 End Sub
28
29 Sub Main()
30
31 PDFNet.Initialize(PDFTronLicense.Key)
32
33 ' Relative path to the folder containing test files.
34 Dim input_path As String = "../../../../TestFiles/"
35 Dim output_path As String = "../../../../TestFiles/Output/"
36
37 Try
38 Console.WriteLine("-------------------------------------------------")
39 Console.WriteLine("Opening the input pdf...")
40 Using in_doc As PDFDoc = New PDFDoc(input_path + "newsletter.pdf")
41 in_doc.InitSecurityHandler()
42
43 ' Create a list of pages to import from one PDF document to another.
44 Dim import_list As ArrayList = New ArrayList
45 Dim itr As PageIterator = in_doc.GetPageIterator()
46 While itr.HasNext()
47 import_list.Add(itr.Current())
48 itr.Next()
49 End While
50
51 Using new_doc As PDFDoc = New PDFDoc ' Create a new document
52 Dim imported_pages As ArrayList = new_doc.ImportPages(import_list)
53
54 ' Paper dimension for A3 format in points. Because one inch has
55 ' 72 points, 11.69 inch 72 = 841.69 points
56 Dim media_box As Rect = New Rect(0, 0, 1190.88, 841.69)
57 Dim mid_point As Double = media_box.Width() / 2
58
59 Using builder As ElementBuilder = New ElementBuilder
60 Using writer As ElementWriter = New ElementWriter
61
62 Dim i As Integer = 0
63 While i < imported_pages.Count
64 ' Create a blank new A3 page and place on it two pages from the input document.
65 Dim new_page As Page = new_doc.PageCreate(media_box)
66 writer.Begin(new_page)
67
68 ' Place the first page
69 Dim src_page As Page = imported_pages(i)
70 Dim element As Element = builder.CreateForm(src_page)
71
72 Dim sc_x As Double = mid_point / src_page.GetPageWidth()
73 Dim sc_y As Double = media_box.Height() / src_page.GetPageHeight()
74 Dim scale As Double = Math.Min(sc_x, sc_y)
75 element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0)
76 writer.WritePlacedElement(element)
77
78 ' Place the second page
79 i = i + 1
80 If i < imported_pages.Count Then
81 src_page = imported_pages(i)
82 element = builder.CreateForm(src_page)
83
84 sc_x = mid_point / src_page.GetPageWidth()
85 sc_y = media_box.Height() / src_page.GetPageHeight()
86 scale = Math.Min(sc_x, sc_y)
87 element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0)
88 writer.WritePlacedElement(element)
89 i = i + 1
90 End If
91
92 writer.End()
93 new_doc.PagePushBack(new_page)
94 End While
95
96 End Using
97 End Using
98
99 new_doc.Save(output_path + "newsletter_booklet.pdf", SDFDoc.SaveOptions.e_linearized)
100
101 End Using
102 End Using
103 Console.WriteLine("Done. Result saved in newsletter_booklet.pdf...")
104
105 Catch e As Exception
106 Console.WriteLine("Exception caught:\n{0}", e)
107 End Try
108 PDFNet.Terminate()
109 End Sub
110
111End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales