1

I have several Jasper reports with deprecated properties (pdfFontName, pdfEncoding and isPdfEmbedded) which cause the warning "PDF Font is deprecated and replaced by the Font Extension".

As there are many files to be corrected (maybe more than a thousand of them), I'm trying to come up with a script that automatically removes those properties and their values from the source code of the files (replaces the string in question with the empty string).

For example:

The following line of code of one of a file's source code:

<font fontName="Courier New" size="10" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="cour.ttf"/>

Would become:

<font fontName="Courier New" size="10" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" />

I already tried to ran this script on Windows Powershell:

Get-ChildItem "pathToFile" -Recurse | ForEach-Object {
(Get-Content $_.FullName).Replace('pdfFontName="\w+"','') | Set-Content $_.FullName
}

The property wasn't removed with this so I tried to replace the regular expression with the actual property value like this:

Get-ChildItem "pathToFile" -Recurse | ForEach-Object {
(Get-Content $_.FullName).Replace('pdfFontName="Helvetica"','') | Set-Content $_.FullName
}

It worked. For this reason, I think the problem has to do with the regular expression. I tried other regex but none of them worked. Can you help me correct this?

Alex K
  • 22,315
  • 19
  • 108
  • 236

1 Answers1

0

As Santiago notes, for regex-based replacements, use the -replace operator rather than the String type's .Replace() method (the latter only performs literal replacements):

(Get-Content $_.FullName) -replace 'pdfFontName="\w+"', ''

Note:

  • If the substitution operand is the empty string (''), it can be omitted.

  • -replace - unlike .Replace() - is case-insensitive by default; use the -creplace variant for case-sensitivity.

  • -replace builds on the (more powerful) [Regex]::Replace() .NET method; see this answer for how they differ.

For guidance on when to use -replace vs. String.Replace(), see the bottom section of this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775