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 replace text within a PDF file. The PDF file must exist within ProjectWise. This functionality was added with Version 1.12.9.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.14.1.0. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.
- Get-PWDocumentsBySearch
- Reset-PWPDFText
Create PDF File
I have created a very simple PDF file for demonstration purposes. The PDF file only contains 3 lines of text which will be replaced using the Reset-PWPDFText cmdlet.
We will be replacing each of the text strings containing the brackets ([]).
Get ProjectWise Document
I have imported my PDF document into ProjectWise.
We will use the Get-PWDocumentsBySearch cmdlet to select the document to be updated. The InputDocuments parameter for the Reset-PWPDFText cmdlet accepts an array of ProjectWise Document objects. So, you can update multiple PDF documents at one time.
# Get the ProjectWise document object to update. # Using a splat for readability. $Splat_GetPDFDoc = @{ FolderPath = 'Projects\PDFs' JustThisFolder = $true DocumentName = 'ResetPWPDFText.pdf' } $pdf = Get-PWDocumentsBySearch @Splat_GetPDFDoc -Verbose
Now that we have our PDF document to update, we can run the Reset-PWPDFText cmdlet.
First, let’s create a hashtable containing the text we want to replace with the corresponding new text.
# Hashtable containing the text to be replaced and the corresponding new text. $ReplacementText = @{ '[PATH]' = 'MyNewPath' '[TITLE]' = 'MyNewTitle' '[USERNAME]' = 'Brian.Flaherty' }
Finally, lets update the PDF file.
# Splat containing the parameter and value pairs for the cmdlet. # Using a splat for readability. $Splat_ResetPWPDFText = @{ InputDocuments = $pdf ReplacementText = $ReplacementText TextColor = 'Red' } Reset-PWPDFText @Splat_ResetPWPDFText -Verbose
The following shows the updated text within the PDF file.
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.