PDF Page Manipulation - Merge, Copy, Delete, Rearrange - Go Sample Code

Sample code for using Apryse SDK to copy pages from one document to another, delete and rearrange pages, and use ImportPages() method for very efficient copy and merge operations. 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 "strconv"
10 . "pdftron"
11)
12
13import "pdftron/Samples/LicenseKey/GO"
14
15func main(){
16 PDFNetInitialize(PDFTronLicense.Key)
17
18 // Relative path to the folder containing the test files.
19 inputPath := "../../TestFiles/"
20 outputPath := "../../TestFiles/Output/"
21
22 // Sample 1 - Split a PDF document into multiple pages
23 fmt.Println("_______________________________________________")
24 fmt.Println("Sample 1 - Split a PDF document into multiple pages...")
25 fmt.Println("Opening the input pdf...")
26 inDoc := NewPDFDoc(inputPath + "newsletter.pdf")
27 inDoc.InitSecurityHandler()
28
29 pageNum := inDoc.GetPageCount()
30 for i := 1; i <= pageNum; i++ {
31 iStr := strconv.Itoa(i)
32 newDoc := NewPDFDoc()
33 newDoc.InsertPages(0, inDoc, i, i, PDFDocE_none)
34 newDoc.Save(outputPath + "newsletter_split_page_" + iStr + ".pdf", uint(SDFDocE_remove_unused))
35 fmt.Println("Done. Result saved in newsletter_split_page_" + iStr + ".pdf")
36 newDoc.Close()
37 }
38 // Close the open document to free up document memory sooner than waiting for the
39 // garbage collector
40 inDoc.Close()
41
42 // Sample 2 - Merge several PDF documents into one
43 fmt.Println("_______________________________________________")
44 fmt.Println("Sample 2 - Merge several PDF documents into one...")
45 newDoc := NewPDFDoc()
46 newDoc.InitSecurityHandler()
47
48 pageNum = 15
49
50 for i := 1; i <= pageNum; i ++ {
51 fmt.Println("Opening newsletter_split_page_" + strconv.Itoa(i) + ".pdf")
52 inDoc = NewPDFDoc(outputPath + "newsletter_split_page_" + strconv.Itoa(i) + ".pdf")
53 newDoc.InsertPages(i, inDoc, 1, inDoc.GetPageCount(), PDFDocE_none)
54 inDoc.Close()
55 }
56 newDoc.Save(outputPath + "newsletter_merge_pages.pdf", uint(SDFDocE_remove_unused))
57 fmt.Println("Done. Result saved in newsletter_merge_pages.pdf");
58
59 // Close the open document to free up document memory sooner than waiting for the
60 // garbage collector
61 inDoc.Close()
62
63 // Sample 3 - Delete every second page
64 fmt.Println("_______________________________________________")
65 fmt.Println("Sample 3 - Delete every second page...")
66 fmt.Println("Opening the input pdf...")
67 inDoc = NewPDFDoc(inputPath + "newsletter.pdf")
68 inDoc.InitSecurityHandler();
69 pageNum = inDoc.GetPageCount()
70
71 for pageNum >= 1 {
72 itr := inDoc.GetPageIterator(uint(pageNum))
73 inDoc.PageRemove(itr)
74 pageNum = pageNum - 2
75 }
76
77 inDoc.Save(outputPath + "newsletter_page_remove.pdf", uint(0))
78 fmt.Println("Done. Result saved in newsletter_page_remove.pdf...")
79
80 // Close the open document to free up document memory sooner than waiting for the
81 // garbage collector
82 inDoc.Close()
83
84 // Sample 4 - Inserts a page from one document at different
85 // locations within another document
86 fmt.Println("_______________________________________________")
87 fmt.Println("Sample 4 - Insert a page at different locations...")
88 fmt.Println("Opening the input pdf...")
89
90 in1Doc := NewPDFDoc(inputPath + "newsletter.pdf")
91 in1Doc.InitSecurityHandler()
92 in2Doc := NewPDFDoc(inputPath + "fish.pdf")
93 in2Doc.InitSecurityHandler()
94
95 srcPage := in2Doc.GetPageIterator()
96 dstPage := in1Doc.GetPageIterator()
97 pageNum = 1
98 for dstPage.HasNext(){
99 if (pageNum % 3 == 0){
100 in1Doc.PageInsert(dstPage, srcPage.Current())
101 }
102 pageNum = pageNum + 1
103 dstPage.Next()
104 }
105 in1Doc.Save(outputPath + "newsletter_page_insert.pdf", uint(0))
106 fmt.Println("Done. Result saved in newsletter_page_insert.pdf...")
107
108 // Close the open document to free up document memory sooner than waiting for the
109 // garbage collector
110 in1Doc.Close()
111 in2Doc.Close()
112
113 // Sample 5 - Replicate pages within a single document
114 fmt.Println("_______________________________________________")
115 fmt.Println("Sample 5 - Replicate pages within a single document...")
116 fmt.Println("Opening the input pdf...")
117
118 doc := NewPDFDoc(inputPath + "newsletter.pdf")
119 doc.InitSecurityHandler()
120
121 // Replicate the cover page three times (copy page //1 and place it before the
122 // seventh page in the document page sequence)
123 cover := doc.GetPage(1)
124 p7 := doc.GetPageIterator(uint(7))
125 doc.PageInsert(p7, cover)
126 doc.PageInsert(p7, cover)
127 doc.PageInsert(p7, cover)
128
129 // Replicate the cover page two more times by placing it before and after
130 // existing pages.
131 doc.PagePushFront(cover);
132 doc.PagePushBack(cover)
133
134 doc.Save(outputPath + "newsletter_page_clone.pdf", uint(0))
135 fmt.Println("Done. Result saved in newsletter_page_clone.pdf...")
136 doc.Close()
137
138 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
139 // in order to preserve shared resources between pages (e.g. images, fonts,
140 // colorspaces, etc.)
141 fmt.Println("_______________________________________________")
142 fmt.Println("Sample 6 - Preserving shared resources using ImportPages...")
143 fmt.Println("Opening the input pdf...")
144 inDoc = NewPDFDoc(inputPath + "newsletter.pdf")
145 inDoc.InitSecurityHandler()
146 newDoc = NewPDFDoc()
147 copyPages := NewVectorPage()
148 itr := inDoc.GetPageIterator()
149 for itr.HasNext(){
150 copyPages.Add(itr.Current())
151 itr.Next()
152 }
153
154 importedPages := newDoc.ImportPages(copyPages)
155 for i := 0; i < int(importedPages.Size()); i ++{
156 newDoc.PagePushFront(importedPages.Get(i))
157 }
158 newDoc.Save(outputPath + "newsletter_import_pages.pdf", uint(0))
159
160 // Close the open document to free up document memory sooner than waiting for the
161 // garbage collector
162 inDoc.Close()
163 newDoc.Close()
164
165 PDFNetTerminate()
166 fmt.Println("Done. Result saved in newsletter_import_pages.pdf...\n")
167 fmt.Println("Note that the output file size is less than half the size")
168 fmt.Println("of the file produced using individual page copy operations")
169 fmt.Println("between two documents")
170}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales