Merge & Split PDF Files With Power Automate Desktop
When I’ve want merge or split a PDF file I admit I’ve often relied upon an free online services which are less than trustworthy or paid hundreds of dollars for a full Adobe Acrobat license. Now that I have Power Automate Desktop installed on my PC I no longer need to do this. Now I am able to write a few simple desktop flows that can merge or split PDF files for free. In this article I will show you how to merge and split PDF files using Power Automate Desktop.
Note: if you would like to follow along with this tutorial you can find the sample files I used here. |
Table Of Contents:
Merge PDF Files With Power Automate Desktop
• Organize PDF Files To Merge In A Folder
• Merge All PDF Files In A Folder Using A Desktop Flow
• Run Desktop Flow To Merge PDF Files In A Folder
• Alternate Method: Merge Selected PDF Files With Power Automate Desktop
• Run The Desktop Flow To Merge Only Selected PDF Files
• Split A PDF File After Every Page Using A Desktop Flow
Split A PDF File With Power Automate Desktop
• Repeat The Split PDF Actions For All Pages In The Input PDF File
• Add Error Handling To Detect The Final Split PDF Page
• Run The Desktop Flow To Split A PDF
• Alternate Method: Split A PDF Every "N" Pages With A Desktop Flow
• Modify The Desktop Flow To Split A PDF At Every N Pages
• Run The Desktop Flow To Split A PDF At Every N Pages
Merge PDF Files With Power Automate Desktop
This section will show two methods to merge a PDF file: merging all of the PDF files in a folder and how to merge only the selected files in a folder.
Organize PDF Files To Merge In A Folder
Begin by placing all of the PDF files we want to merge into the same folder. Then create a subfolder called Output. The output folder is where the merged PDF will be stored.
In this example, there are 5 PDF files each having 3 pages of text.
Merge All PDF Files In A Folder Using A Desktop Flow
Open Power Automate Desktop and create a new flow called Merge PDF Document.
We want the user to manually select the folder where there are PDFs waiting to be merged. Add a Display select folder dialog action with the title “Select Folder With PDFs To Merge.”
Next, add a Get files in folder action and point it at the %SelectedFolder% from the previous action. Sort the files by Full name to ensure the PDFs are merged in the same order they appear in the folder.
Then add a Merge PDF files action to join the %Files% found in the folder.
The completed desktop flow to merge PDF files should look like this.
Run Desktop Flow To Merge PDF Files In A Folder
Press play to test the desktop flow. When the dialog box appears select the folder where the PDFs to merge are stored. Then click OK.
Once the desktop flow is finished the merged PDF file will be found in the Output folder.
Open the Merged.pdf file. The merged file has 15 pages. This makes sense because there were originally 5 PDFs each having 3 pages.
Alternate Method: Merge Selected PDF Files With Power Automate Desktop
We can also build the desktop flow to merge only the selected PDF files by a user instead of an entire folder. Start a new desktop flow.
Add a Display select file dialog with the title “Select PDFs to Merge” and enable Allow multiple selection.
A strange quirk of the select files dialog is that it will output the list of files we selected in the opposite order that we chose them. To correct this behaviour we add a Reverse list action to transform the %SelectedFiles% list.
Then merge the PDF files in the same manner as the previous method.
The desktop flow to merge only selected PDF files in Power Automate Desktop looks like this.
Run The Desktop Flow To Merge Only Selected PDF Files
Give the flow we built a try by pressing the play button. When we do this a select files dialog appears. Choose all of the PDFs to be merged and click Open.
The resulting Merged.pdf file will be found in the Output folder.
Split A PDF File With Power Automate Desktop
This section will show two methods to split a PDF file: how to split a PDF after every page and how to split a PDF after every “n” pages.
Split A PDF File After Every Page Using A Desktop Flow
Now that we’ve successfully merged a PDF, let’s learn to do the opposite. We will now take a single PDF file and split it into multiple PDFs – one for each page. Create a new flow in Power Automate Desktop and call it Split PDF Document.
We want the user to select a PDF file to be split. Add a Display select file dialog action with the title “Select PDF To Split.”
Then set another variable called CurrentPage. This variable will be used inside of a loop to track which PDF page is currently being split.
Repeat The Split PDF Actions For All Pages In The Input PDF File
We want to several new PDF files each with a single page split from the original PDF. To do this we will need to use a loop. Add a Loop condition action to the desktop flow.
Set both the First operand and the Second operand to %True%. The loop will continue infinitely until we add a break loop condition later on. Don’t worry about this for now.
Then insert an Extract PDF file page to new PDF file action. Use the %CurrentPage% as the page selection and set the Extracted PDF file path to target the Output folder.
After the PDF file page is extracted increase the %CurrentPage% variable by 1 to go to the next page.
The Power Automate Desktop flow should look like this so far.
Add Error Handling To Detect The Final Split PDF Page
If we go ahead and run the flow it will give an error on the last page. There is no desktop flow action to determine the number of pages in PDF so we will use error handling to break the loop when the %CurrentPage% is out of bounds.
Add a Label action to the bottom of the flow called FinishedPdfSplit.
Then go back to the Extract PDF file pages action and click OnError.
In the Advanced section, choose the Page out of bounds error type and add a new rule. Continue the flow run when an error occurs. Handle the error be going to the label FinishedPdfSplit.
The finished Power Automate Desktop flow to split a PDF should look like this.
Run The Desktop Flow To Split A PDF
Press play to give the flow a test. When the select a file dialog PDF appears select a PDF with multiple pages then click Open.
Once the desktop flow is finished running there will page one PDF for each page of the input PDF found in the Output folder.
Alternate Method: Split A PDF Every “N” Pages With A Desktop Flow
What if we wanted to split the PDF every 2 pages, every 3 pages or some other number of pages? The image below shows a modified version of the flow we just built that can split a PDF at any number of pages by updating the EveryNPages variable. Flow actions in green are new actions and flow actions in red are updates of existing actions.
Modify The Desktop Flow To Split A PDF At Every N Pages
Let’s build the desktop flow to split a PDF at every “n” pages together. Beginning at line 4, set a new variable called EveryNPages to a value of 3.
Then add a Create new list action as the first action inside of the loop and name the list %PageRange%. We will use PageRange to determine which PDF pages to split during the current flow loop.
Insert an Add item to list step and add the current page number to the list.
Then place another Add item to list step below it and insert %CurrentPage + EveryNPages – 1%.
After adding both items to the list we can now build the page range. Power Automate Desktop does not have any ability to join numbers and text in a single string by using variable manipulation and the % notation. That is why we are building a list of numbers and using a Join text action to make some text showing the page range (examples: 1-3, 4-6, 7-9).
Setup the Join text action as shown below.
Modify the Extract PDF file pages to new PDF action to use a Page selection of %JoinedText%.
Then change the Increase variable step to Increase by %EveryNPages%.
Finally, at the end of the loop add a Clear list action to reset %PageRange% for the next loop run.
Run The Desktop Flow To Split A PDF At Every N Pages
We are done building the desktop flow to split a PDF at every “n” pages. To test the flow press the play button. A select file dialog will appear. Choose the PDF we want to split.
After the flow is finished running we can see there were 5 files saved to the Output folder.
Each file has exactly 3 pages. This makes sense because we set the variable %EveryNPages% to a value of 3.
Did You Enjoy This Article? 😺
Subscribe to get new Power Automate For Desktop articles sent to your inbox each week for FREE
Questions?
If you have any questions about Merge & Split PDF Files With Power Automate For Desktop please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.
Great article and thanks for sharing!
I have a scenario like this, the files usually has 50+ pages, each page is an invoice. and each invoice should be split into 1 single PDF.
However, sometimes, an invoice runs to 2nd or 3rd page. So I cannot use a fixed number to do the split. In case like this, on the 2nd and 3rd page, there’s a keyword of “Page of”.
How could I configure so the bot knows where to do the split correctly?
John,
I’m interested in figuring this puzzle out but I don’t have any good samples to test on. I need a set of invoices with a variable number of pages and something I can openly publish on my website. Do you know where I could find a sample for playing around with in my spare time?
Hi Matthew,
Thanks! for sharing this great article.
I am having the same scenario as John has mentioned above.
Is it possible with power automate?, I am still struggling if invoice runs to 2nd or 3rd page.
I have an example of this task. I added invoice number extraction from pdf into the the flow and save files using invoice numbers as file names. Attaching sample on 9 pages with 6 invoices, inv.#1802 is on 2 pages, inv.#1905 is on 3 pages. I my case invoice pages are sequential (which i hope should help).
Vlad,
I have removed the link to your file. Sorry, but I do not allow people to post a file link on my comments section since it can be potentially harmful (a virus or malware).
HI Matt,
Did you get a chance to figure out Johns scenario?
Alwin,
Sorry, I have not looked into this any further.
ok
Hi Matthew,
Any update on a solution for invoices that has different numbers of pages as John mentioned above? Thanks!
Dominique,
Sorry, I have not found any solution.
HI John, Did you figure this out?I am having a same scenario and unable to figure it out!
You could use this template & set up a Microsoft Azure Function to split the PDFs by text
https://community.powerplatform.com/galleries/gallery-posts/?postid=4bbb1ba8-1158-ef11-a317-0022482822d9
Thanks Matthew, extremely helpful!
Do you know if it’s possible to use “Extract PDF file pages to new PDF file” using the “Allow multiple selection” in the “Display select file dialog”?
As I understand, the “Extract PDF file pages to new PDF file” requests a single file and thus does not allow the input of multiple files. At leas thats the error I’m running into. My script works fine if I only select 1 file at a time, but if I check the “Allow multiple selection” the “Extract PDF action won’t work because it wants a single pdf, not multiple. screen shots below.
Thanks again for a great tutorial!
Jeff,
The general strategy here should be:
1. File Dialog with allow multiple files enabled
2. Create a for each loop to loop over those files
3. Extract pages from each file on each loop iteration
4. Merge the pages on each loop iteration (skip the first iteration)
First of all Thanks for your greate tutorial
But, same issue as Jeff has for multiple files selection to split into single page
Can you share the screen shot if possible?
Hongchul,
Can you please tell me more about your scenario? I want to understand what you are trying to do. Tell me what your inputs are and what your end goal is for them.
Hi Matthew,
Thanks for your questions
What I’d like to do is to split multiple PDF files to single PDF page each of them from the folder
Error in here
Hope you got my point that A PDF is ok with your training, but I need to split lots of PDF files into single file
I am a complete novice, and want to thank you for sharing your insight. I was able to create what I needed easily!!
Is it possible to automate unique file names in the output folder?
Stephen,
The simplest way to create a unique file name is to append the current date & time down to milleseconds at the end of the filename.
Thank you!
Matthew thank you so much for providing us such a great tutorial when it comes to splitting files. I was wondering, there is a way to create another routine if I have a folder with several pdf files that I need to split? Something to refer the folder al let the AI do the splitting to one page of all pdf files?
Thank you in advance if you any idea to address this puzzle.
Luis
Luis,
Sorry, I haven’t tackled this question yet. But I’d like to figure it out in the future.
Try this:
(293) How to Split a PDF into Multiple Files With Power Automate Desktop – YouTube
Hi Matthew,
Useful article,
Here is my scenario,i have 2folders contains pdf’s in different locations.
1.compare each file name
2.if yes need to merge these 2files with same name and store in another folder
3.this process should be iterated
I done the first step,I’m facing the challenge with merging.
How can I configure this ?
Thanks in advance
Great article and thanks for sharing. I have an 800+ pages PDF file. An error is thrown each time randomly. Something with “object reference”. Any idea? I followed the same steps.
Kishan,
Sorry, but I don’t recognize that error message.
Hi Matthew
Thanks for the great article. I am wondering if you can help with this scenario as im struggling to figure it out.
When a file is created in a SharePoint Online document library. Send the file to Power Automate Desktop and split the PDF, for each split page, save the file back to SharePoint online in a folder that should be created for the original file name.
e.g. If i have a pdf called myFile.pdf with 50 pages, i would expect after the flow has ran that i will now have a folder called myFile and inside of that folder would be 50 pdfs.
Thanks in advance.
Hello Matt, thank you for those amazing tutorials!
Is there a way to tell the Merge-PDF flow to keep the name of one of the used PDFs as the “save as” name?
The situation at work here is that we are merging files that go to individuals with an always updated boilerplate. It would be great to be able to merge that boilerplate into the individual PDFs and keep the individual PDF name.
Hello, this blog is very helpful, but I am having a different usecase, I want to split the pdf based on the userid. In my pdf there were several userid at different positions example,
User ID
…….
…..
COntent of Person1
…
….
userId
…
……
content of Person2
I am not sure how to do that, Can you help me out??
If each User Id is on a different page, then this Azure Function template may help you split by the User Id text
https://community.powerplatform.com/galleries/gallery-posts/?postid=4bbb1ba8-1158-ef11-a317-0022482822d9
Tyler,
Nice to see you here Mr. Kolota. I am a fan of your work.
Matthew,
Thank you for sharing your excellent idea of how to split PDF pages to a page per file. I have been searching for the solution for a long time and am glad that I finally found yours thru Bing AI. It works perfect and is exactly what I am looking for.
Once again, thank you for your work. You made a community strong.
Paul,
You’re welcome 🙂
Not sure if somebody else has asked this however, I used mail merge to create my original PDF. So each page is the same as it’s based on the template. The steps in your article works perfectly to split the pages however, I want to be able to name the split Pdfs based on the unique name in the template. I’ve tried Extract Text from PDF and OCR but i’m messing up somewhere.