AVAILABLE NOW: Spring 2025 Release

How to Programmatically Merge, Split, and Reorder PDFs Using JavaScript

By Isaac Maw | 2025 Apr 09

Sanity Image
Read time

5 min

Summary: Offering essential PDF Manipulation tools within your application helps keep users within your ecosystem. Here’s a quick overview of adding Apryse PDF Manipulation tools via SDK to your application using JavaScript.

Without an effective solution in place for manipulating the pages of PDFs, your users many need to go elsewhere to find ways to split, reorder, or merge pages of a PDF. Not only does this cause issues for your application in terms of the lack of needed functionality, but when users find external PDF tools, it can lead to security and privacy compliance issues.

To provide an automated way to programmatically manipulate PDFs, let’s take a look at how to use the Apryse SDK to manipulate PDFs in a few ways such as splitting a PDF into multiple pages and merging multiple PDFs into one. We’ll use JavaScript for these examples, but the SDK is also available for other languages and frameworks including C++, .NET, Python and Node.js.

How to Get Started

Copied to clipboard

Getting started is easy:

  1. Download the Apryse Server SDK. In this case, we’ll want to choose the JavaScript PDF library.
  2. Extract the folder from the .zip file.
  3. Get a free trial key.
  4. See our documentation for the full code you’ll need to handle the following example tasks. I’ve included a sample of the specific code for each 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:

  • Split a PDF document into multiple pages
  • Merge several PDF documents into one
  • Delete every second page

Split a PDF into Multiple Pages

Copied to clipboard

There may be times that you need to split a PDF into multiple pages. Maybe you need to split a PDF into separate pages. For example, you’re splitting out a report of your employees to create a separate file for each employee.

We’ll use the following code:

async function main() {
  const doc = await PDFNet.PDFDoc.createFromURL(filename);
  const page_num = await doc.getPageCount();
  for (let i=1; i<=page_num; ++i)
  {
    const newDoc = await PDFNet.PDFDoc.create();
    newDoc.insertPages(0, doc, i, i, PDFNet.PDFDoc.InsertFlag.e_none);
    const buf = await newDoc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
    //optionally save the blob to a file or upload to a server
    const blob = new Blob([buf], { type: 'application/pdf' });
  }
}
PDFNet.runWithCleanup(main);

Merge Several PDFs into One

Copied to clipboard

Now let’s look at the previous example in reverse. You have a separate PDF for each of your employees and need to merge them into one report for your manager.

Let’s use the following code to merge those PDFs.

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; 
  }

Delete Specific Pages

Copied to clipboard

If you need to delete specific pages, you can try this sample code. For example, if a long report has title pages or blank pages that you’d like to remove, this function can help. In this sample code, you'll find how to delete every second page, leaving just the pages you need:

try { 
    console.log('_______________________________________________'); 
    console.log('Sample 3 - Delete every second page...'); 
    console.log('Opening the input pdf...'); 
    await PDFNet.startDeallocateStack(); 
    const inDoc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'newsletter.pdf'); 
    inDoc.initSecurityHandler(); 
 
    let pageNum = await inDoc.getPageCount(); 
 
    while (pageNum >= 1) { 
      const itr = await inDoc.getPageIterator(pageNum); 
      inDoc.pageRemove(itr); 
      pageNum -= 2; 
    } 
 
    await inDoc.save(outputPath + 'newsletter_page_remove.pdf', 0); 
    console.log('Done. Result saved in newsletter_page_remove.pdf...'); 
    await PDFNet.endDeallocateStack(); 
  } catch (err) { 
    console.log(err); 
    ret = 1; 
  }

Other Examples

Copied to clipboard

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

Conclusion

We’ve just covered a few of the many ways to manipulate PDFs using the Apryse PDF SDK. The code in these examples can be expanded to create more elaborate 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