Be sure to check out my Scripting4Crypto initiative. It’s a fun way to get into using cryptocurrencies all while getting your PowerShell needs met.
In this post, I will show you how to merge multiple PDF files into a new Merged PDF file. The PDF files to be merged must exist within ProjectWise. Although, this functionality has been available for a while, we have recently added the ability to replace the physical file of a Merged PDF document or create a new version of the Merged PDF document when the Merged PDF document exists. This functionality was added with Version 1.15.0.0 of the PWPS_DAB module.
We will be using the following cmdlets to accomplish this task. All of the ProjectWise related cmdlets are available using the PWPS_DAB module. At the time of this post, I am using version 1.15.0.0. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.
- Get-PWDocumentsBySearch
- Merge-PWPDFs
Scenario One
In the first scenario, we will merge two PDF files which exist in the same ProjectWise folder into a Merged PDF file in a specified separate folder.
The following shows the two source PDF files contained in the PDF Files folder.
Next we will get the two ProjectWise Document objects in preparation to merge them.
<# Scenario One - PDF documents to be merged exist in the same folder. Select PDF documents to be merged. Use the -Slow switch parameter to populate the FullPath property. #> $Splat_GetPDFDocs = @{ FolderPath = 'PowerShell\PDF Files' FileName = '%.pdf' } $pwDocuments = Get-PWDocumentsBySearch @Splat_GetPDFDocs -Slow # Displays the fullpath of each document. $pwDocuments.FullPath
Now that we have our PDF documents to merge, we can run the Merge-PWPDFs cmdlet.
# Create new Merged PDF file. $Splat_Merge = @{ InputDocuments = $pwDocuments OutputFolder = 'PowerShell\Merged' OutputName = 'Merged_PDF' } $pwDocumentMerged = Merge-PWPDFs @Splat_Merge
The following shows the generated PDF file in the Merged folder.
Scenario Two
In the second scenario, we will merge two PDF files which exist in separate ProjectWise folders within the same ProjectWise datasource into a Merged PDF file in a specified separate folder.
The following shows the two source PDF files returned using a Document Search. Notice these two files are located in two separate folders.
Next we will get the two ProjectWise Document objects in preparation to merge them.
<# Scenario Two - PDF documents to be merged exist in different folders. $Splat_GetDocs = @{ FolderPath = 'PowerShell\PDFs' FileName = '%.pdf' } $pwDocuments = Get-PWDocumentsBySearch @Splat_GetDocs -Slow # Displays the fullpath of each document. $pwDocuments.FullPath
Now that we have our PDF documents to merge, we can run the Merge-PWPDFs cmdlet.
# Create new Merged PDF file. $Splat_Merge2 = @{ InputDocuments = $pwDocuments OutputFolder = 'PowerShell\PDFs\Merged' OutputName = 'Merged_PDF2' } $pwDocumentMerged2 = Merge-PWPDFs @Splat_Merge2
The following shows the merged PDF file was not created in the specified Merged folder ‘‘PowerShell\PDFs\Merged‘‘, but rather in the folder containing the first PDF file to be merged.
This occurs by default when the OutputFolder is not included, or the specified folder does not exist. This is the reason for going through this exercise.
Scenario Three
In this scenario we will attempt to create a new ‘Merged_PDF’ document in the target folder ‘PowerShell\Merged’. Because the target file already exists, you will receive a WARNING message.
<# Scenario Three - Attempt to merge PDF documents when the target PDF document already exists. $Splat_GetPDFDocs = @{ FolderPath = 'PowerShell\PDF Files' FileName = '%.pdf' } $pwDocuments = Get-PWDocumentsBySearch @Splat_GetPDFDocs -Slow # Create new Merged PDF file. $Splat_Merge = @{ InputDocuments = $pwDocuments OutputFolder = 'PowerShell\Merged' OutputName = 'Merged_PDF' } $pwDocumentMerged = Merge-PWPDFs @Splat_Merge
The following shows the WARNING message received.
VersionExisting
Include the -VersionExisting switch parameter with the Merge-PWPDFs cmdlet, to create a new version of the target document, .
# Include -VersionExisting switch parameter to generate new document version. $pwDocumentMerged = Merge-PWPDFs @Splat_Merge -VersionExisting
ReplaceExisting
Include the -ReplaceExisting switch parameter with the Merge-PWPDFs cmdlet, to replace the physical file associated with the existing merged pdf file.
The following shows the document property, ‘FIle Updated On’, value has been changed indicating the physical file had been updated.
Experiment with it and have fun.
Hopefully, you find this useful. Please let me know if you have any questions or comments. If you like this post, please click the Like button at the bottom of the page.
Hi, i have followed exactly the steps you have given and i get a combined PDF however when i try to open the file i get this error ‘There was an error opening this document. The file cannot be opened because it has no pages’
LikeLike
This issue had to do with the fact that the working directory was not created. Once created, things worked as expected.
LikeLike