AVAILABLE NOW: Spring 2025 Release

Split, Merge, and Insert Pages in a PDF Using Go

By Garry Klooesterman | 2025 May 01

Sanity Image
Read time

4 min

Summary: PDFs have become one of the most popular formats for sharing information quickly and easily. However, manipulating PDFs isn't always as easy and can be challenging when completing more complex tasks like merging PDFs or inserting pages. In this blog, we’ll look at using a PDF SDK such as the Apryse SDK to automate these and other PDF manipulation tasks.

Introduction

Copied to clipboard

PDFs are one of the most popular formats used to share information quickly and easily. One feature that makes them so popular is that they can be viewed on practically any device and will display the same regardless of the hardware or software used. A challenge with PDFs, however, is manipulating them for more complex tasks such as merging PDFs or inserting pages.

In these cases, you could use PDF editors such as xodo.com or Xodo PDF Studio or app based editors like Apryse WebViewer, but there would still be some manual element to the process.

To automate these processes and more, you can use a PDF Manipulation library like the one included with the Apryse SDK. In this blog, we’ll use Go for the examples, but if you prefer another language or framework, the SDK is available for others including C#, Java, Python and Node.js.

How to Get Started

Copied to clipboard

Follow these instructions to get started.

  1. Download the Apryse Server SDK. In this case, choose the Go PDF library.
  2. Follow these steps to extract the folder from the .zip file and set up the environment.
  3. Get a free trial key, if you don’t already have one.
  4. See our documentation for the full code you’ll need to support the following example tasks. I’ve included a sample of the specific code for each task based on our demo.

Now let’s look at some of the ways we can manipulate PDFs using the Apryse SDK. In this blog, we’ll show you how to:

  • Split a PDF into multiple pages
  • Merge several PDFs into one
  • Insert a page from one PDF to another PDF

Split a PDF into Multiple Pages

Copied to clipboard

Splitting a PDF into separate pages is easy with a PDF SDK. Here’s an example of when you might want to do this. You are preparing a report for a meeting later today and you realize that the source file has a summary page after each section that you don’t need to include. You can split the PDF report into separate pages using the following code.

	// Sample 1 - Split a PDF document into multiple pages    
    fmt.Println("_______________________________________________")
    fmt.Println("Sample 1 - Split a PDF document into multiple pages...")
    fmt.Println("Opening the input pdf...")
    inDoc := NewPDFDoc(inputPath + "newsletter.pdf")
    inDoc.InitSecurityHandler()
	
    pageNum := inDoc.GetPageCount()
	for i := 1; i <= pageNum; i++ {
		iStr := strconv.Itoa(i)
        newDoc := NewPDFDoc()
        newDoc.InsertPages(0, inDoc, i, i, PDFDocE_none)
        newDoc.Save(outputPath + "newsletter_split_page_" + iStr + ".pdf", uint(SDFDocE_remove_unused))
        fmt.Println("Done. Result saved in newsletter_split_page_" + iStr + ".pdf")
        newDoc.Close()
    }
    // Close the open document to free up document memory sooner than waiting for the
    // garbage collector    
    inDoc.Close()

Merge Several PDFs into One

Copied to clipboard

With the report split into separate pages, you can discard the summaries, leaving just what you need. Now you’re ready to create the new report and you can use the following code to merge the pages into one report.

    // Sample 2 - Merge several PDF documents into one    
    fmt.Println("_______________________________________________")
    fmt.Println("Sample 2 - Merge several PDF documents into one...")
    newDoc := NewPDFDoc()
    newDoc.InitSecurityHandler()
    
    pageNum = 15
    
	for i := 1; i <= pageNum; i ++ {
        fmt.Println("Opening newsletter_split_page_" + strconv.Itoa(i) + ".pdf")
        inDoc = NewPDFDoc(outputPath + "newsletter_split_page_" + strconv.Itoa(i) + ".pdf")
        newDoc.InsertPages(i, inDoc, 1, inDoc.GetPageCount(), PDFDocE_none)
        inDoc.Close()
    }  
    newDoc.Save(outputPath + "newsletter_merge_pages.pdf", uint(SDFDocE_remove_unused))
    fmt.Println("Done. Result saved in newsletter_merge_pages.pdf");
	
    // Close the open document to free up document memory sooner than waiting for the
    // garbage collector   
    inDoc.Close()

Insert a Page from One PDF to Another PDF

Copied to clipboard

Almost done. The report is ready and you’re doing a final check when you remember seeing an email recently about reports needing an intro page added for each department. You already have the intro pages in another PDF, so it’s just a matter of running the following code to insert those pages throughout the new report.

    // Sample 4 - Inserts a page from one document at different 
    // locations within another document
    fmt.Println("_______________________________________________")
    fmt.Println("Sample 4 - Insert a page at different locations...")
    fmt.Println("Opening the input pdf...")
    
    in1Doc := NewPDFDoc(inputPath +  "newsletter.pdf")
    in1Doc.InitSecurityHandler()
    in2Doc := NewPDFDoc(inputPath +  "fish.pdf")
    in2Doc.InitSecurityHandler()
    
    srcPage := in2Doc.GetPageIterator()
    dstPage := in1Doc.GetPageIterator()
    pageNum = 1
    for dstPage.HasNext(){
        if (pageNum % 3 == 0){
            in1Doc.PageInsert(dstPage, srcPage.Current())
		}
        pageNum = pageNum + 1
        dstPage.Next()
	}
    in1Doc.Save(outputPath +  "newsletter_page_insert.pdf", uint(0))
    fmt.Println("Done. Result saved in newsletter_page_insert.pdf...")
    
    // Close the open document to free up document memory sooner than waiting for the
    // garbage collector
    in1Doc.Close()
    in2Doc.Close()

Other Examples

Copied to clipboard

Looking for more examples of what you can do with this SDK? Check out the documentation where you’ll find sample code for removing a page, cropping a page, and more.

Conclusion

Copied to clipboard

Manipulating PDFs using a PDF Manipulation Library like the Apryse PDF SDK is easy and convenient. We’ve looked at a few of the basic tasks in this blog but you can use the code samples as a starting point to handle other complex PDF manipulation tasks.

Try it out for yourself with our free trial.

Get started now or contact our sales team for any questions. You can also check out our Discord community for support and discussions.

If you’d prefer a non-coding option, consider using a PDF editor such as xodo.com or Xodo PDF Studio.

Sanity Image

Garry Klooesterman

Senior Technical Content Creator

Share this post

email
linkedIn
twitter