AVAILABLE NOW: Spring 2025 Release

How to Programmatically Merge PDFs Without Third-Party Apps

By Isaac Maw | 2025 Apr 30

Sanity Image
Read time

3 min

PDF files offer numerous advantages for documents. They’re universally compatible, maintain their fixed layout and formatting, and essentially act as digital versions of paper documents. However, it can be challenging to find ways to manipulate PDF documents, such as merging multiple files, removing certain pages, or even editing or annotating content.

For example, merging individual reports into a monthly report, or merging multiple records into one case file are use cases which require PDF merging and manipulation.

There are numerous free online tools to convert and manipulate PDFs, but business regulations, security compliance requirements or personal documents may mean these online services are not an option, as they expose documents to unknown security and privacy risks. It comes back to the old adage: If you want something done right, you’ve got to do it yourself.

Fortunately, developers can easily add PDF manipulation functionality to software applications using the Apryse Server SDK and its PDF manipulation features, including merging PDFs. The below instructions illustrate how to get this done.

How to Get Started with Apryse Server SDK

Copied to clipboard
  1. Getting started is fairly easy.
  2. Download the Apryse Server SDK. In this case, we’ll want to choose the JavaScript PDF library.
  3. Extract the folder from the .zip file.
  4. Get a free trial key.
  5. See our documentation for the full code you’ll need to handle the following example task.

Now that we’ve got everything set up, we can look at some examples of what we can do. In this blog, we’ll look at how to merge a PDF document into multiple pages.

Merge Several PDFs into One

Copied to clipboard

If you have multiple PDF documents that need to be merged, try this sample code:

// Sample 2 - Merge several PDF documents into one 
try { console.log('_______________________________________________'); console.log('Sample 2 - Merge several PDF documents into one...'); 
   // start stack-based deallocation with startDeallocateStack. Later on when endDeallocateStack is called, 
    // all objects in memory that were initialized since the most recent startDeallocateStack call will be 
    // cleaned up. Doing this makes sure that memory growth does not get too high. 
    await PDFNet.startDeallocateStack(); 
    const newDoc = await PDFNet.PDFDoc.create(); 
    newDoc.initSecurityHandler(); 
 
    const pageNum = 15 
    for (let i = 1; i <= pageNum; ++i) { 
      const fname = 'newsletter_split_page_' + i + '.pdf'; 
      console.log('Opening ' + fname); 
      const currDoc = await PDFNet.PDFDoc.createFromFilePath(outputPath + fname); 
      const currDocPageCount = await currDoc.getPageCount(); 
      newDoc.insertPages(i, currDoc, 1, currDocPageCount, PDFNet.PDFDoc.InsertFlag.e_none); 
    } 
    await newDoc.save(outputPath + 'newsletter_merge_pages.pdf', PDFNet.SDFDoc.SaveOptions.e_remove_unused); 
    console.log('Done. Result saved in newsletter_merge_pages.pdf'); 
    await PDFNet.endDeallocateStack(); 
  } catch (err) { 
    // console.log(err); 
    console.log(err.stack); 
    ret = 1; 
  } 
 

Other PDF Manipulation Examples

See our documentation for more examples of what you can do such as removing a PDF page and cropping a page.

Conclusion

Copied to clipboard

We’ve just covered merging PDFs using the Apryse PDF SDK. Check out the documentation to find solutions to help you to manipulate PDFs, whether that is to split, add, remove, crop or reorder pages, or to rotate single or multiple pages in your PDF.

If you’re curious to see it in action, why not try it out for yourself? 

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

Sanity Image

Isaac Maw

Technical Content Creator

Share this post

email
linkedIn
twitter