Combine PDFs with Page Imposition - Go Sample Code

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

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales