Powershell scripting Assistance "Searchable PDF Code Fragment"

Experts,

I am looking for a "free" way to take a PDF document save the contents of the PDF to a variable in a "Searchable" format..


So essentially I want to do the following :


get-webrequest -uri "Path to PDF"

Store this information into a variable in a searchable format and then run a query based on "Text" or a "String" withen that PDF file..


What is the easiest way of doing this .. Please provide a code snippet for it.
LVL 17
ITguy565Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
This uses iTextSharp.dll (licensed under AGPL) from https://github.com/itext/itextsharp
Latest binary release is here: https://github.com/itext/itextsharp/releases/tag/5.5.11
No need to install anything; download itextsharp-all-5.5.11.zip, extract the "itextsharp-dll-core.zip", extract "itextsharp.dll", and drop it in a folder, and change line 9 accordingly.
Like Get-Content, it'll return an array of strings.
You can assign or filter the output as usual:
$found = ConvertFrom-Pdf -Path "Path to PDF file" |  Where-Object {$_ -like '*Find me*'}

Open in new window

Function ConvertFrom-Pdf {
[CmdletBinding()]
Param(
	[Parameter(ValueFromPipeline=$true)]
	[Alias('FullName')]
	[string]$Path,
	[switch]$Raw
)
	Begin {
		Add-Type -Path 'C:\Temp\itextsharp.dll'
	}
	Process {
		Try {
			If (-not [IO.Path]::IsPathRooted($_)) {
				$Path = (Resolve-Path -Path $Path -ErrorAction Stop).Path
			}
			$pdfReader = New-Object -TypeName 'iTextSharp.text.pdf.pdfreader' -ArgumentList $Path -ErrorAction Stop
			For ($i = 1; $i -le $pdfReader.NumberOfPages; $i++) {
				Write-Verbose "Converting page $($i)"
				[iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($pdfReader, $i) -split "`r?`n"
			}
		} Catch {
			$PSCmdlet.WriteError($_)
		} Finally {
			If ($pdfReader) {
				Try {$pdfReader.Close()} Catch {}
				Remove-Variable -Name pdfReader -ErrorAction SilentlyContinue
			}
		}
	}
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITguy565Author Commented:
Thanks oDbA!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.