Have you ever struggled to merge and split PDF files? In this article we will show you how to easily do so using Microsoft Power Automate for Desktop!
Why might you need to merge or split PDF files?
Maybe you have separate documents from different co-workers that need to be consolidated into one document for reporting or filing purposes. Or maybe you have scanned a big bunch of documents together, but they need to be separate documents.
There are many more reasons you may need to merge/split PDF files, and Microsoft Power Automate for Desktop can help you automate the process.
For those of you who prefer video instructions, you can have a look at the video below. If you prefer to read, just scroll down further for the article.
Skip ahead to the instructions you need:
- How to merge PDF files in Microsoft Power Automate for Desktop
- How to split PDF files in Microsoft Power Automate for Desktop
- How to split PDF files in Microsoft Power Automate for Desktop (unknown number of pages)
How to merge PDF files in Microsoft Power Automate for Desktop
For the merging files portion of the article, we will be using a folder which contains five individual PDF files as an example.
Firstly, we need to use the action “Get files in folders”. Under the “Folder” parameter, choose the right folder from the dropdown menu – in our case, it is the “merge PDF” folder. This action will retrieve all the files in this folder and store it inside a variable called Files which is essentially a list of file objects.
Next, we will drag and drop the “Merge PDF Files” action, which can be found under PDF actions, into the main workspace. The input parameter in this case would be the “Files” variable
For the “Merged PDF path”, navigate to your desired folder – in our case here, it is the “Merge PDF” folder, and rename the file as “Merged PDF.pdf”. Remember to specify the .pdf extension.
Next, just to be safe, select the “Overwrite” option for “If file exists”.
Run the flow. Navigating back to your folder, will show that a merged file containing all five individual PDFs has been successfully created!
How to split PDF files in Microsoft Power Automate for Desktop
To explain splitting PDF files, we will be using a single PDF titled “Sample PDF File”, containing five individual pages.
Step 1: Extract PDF file page
Firstly, we will be using the “Extract PDF files pages to new PDF file” action. Under the “PDF file” parameter, specify the PDF file you wish to extract the pages from, and set the “Page selection” as the page you want to extract. For extracted PDF file path, extract the page into a separate PDF file and in this case we have renamed it as “Page 1.pdf”. Remember to always add the file extension .pdf.
Lastly, select “Overwrite” from the “If file exists” dropdown menu.
After running the flow and navigating to our folder, we can see that we have successfully extracted the first page into a separate pdf file called page1.pdf
Step 2: Implement a loop to repeat for other pages
Firstly, drag the “Loop” action into the workspace. Since we know there are 5 individual pages, we will set the “Start from” as 1, the “End to” as 5, and the “Increment by” as 1.
Then, drag your PDF action into the body of your loop. It should look like this:
Remove the “Page selection” value which was hardcoded earlier, click on the {x} and choose “LoopIndex” from the dropdown menu.
Likewise, do the same for the extracted pdf file path instead of hard coding the value to be “Page 1”, we shall use the dynamic value “LoopIndex” instead.
What this does is loop through the pdf file five times. The starting loop index is 1 and for the first loop we will extract the first page of the pdf file into a new pdf file called “Page 1.pdf” (i.e. Page %LoopIndex%.pdf, where LoopIndex is 1). For the second iteration, the loop index becomes the value of 2 and for this iteration will not extract the first page but will extract the second page and save the second page into a new file called “Page 2.pdf”. This continues so on and so forth until five iterations of the loop have been completed.
Running this flow, will cause the LoopIndex to increment from 1 to 5, and each page has been successfully extracted into five individual PDF files!
However, it is important to note that we coded the loop to increase from 1 to 5 because we knew beforehand that the total pdf files file contains 5 pages. There would be a few change and additional steps to the flow in the scenario where we aren’t aware of how many pages the PDF file contains.
How to split PDF files when number of pages is unknown
We would need to change the “End to” in the Loop dialogue box to an arbitrary large number. In this case, we have set it as 100.
We will need to create a label, using the “label” action under “Flow control”. Drag and drop the label to the end of the loop on your workspace.
Name the label as “OutofBoundsError”.
Click on your “Extract PDF file pages into new PDF file” action, and the “On error” option.
Under “On error”, choose the option “Continue flow run”, and “Go to label”. Select label as the “OutOfBoundsError”. Click save.
What this does is: Initially we have increased the end loop index to 100, although our PDF file contains only 5 pages. Hence when the loop index reaches the value of 6, which is when it tries to extract page 6 of the PDF file, it will encounter an error as page 6 does not exist. The exception handling has been configured in such a way that when an error is occurred, the flow will jump to the “OutOfBoundsError” label, hence exiting the loop.
Now you have successfully managed to split a PDF into individual PDF pages, without hardcoding the number of pages in the PDF!
Automate splitting and merging PDF files in Microsoft Power Automate for Desktop
Now that you’ve learned how to use Microsoft Power Automate for Desktop to automatically split or merge PDF files, it’s time you integrate it into your daily work. We hope you’ve found our tips useful in helping you become more productive.