-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathGet-PdfText.ps1
37 lines (30 loc) · 1.01 KB
/
Get-PdfText.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# http://powershell.org/wp/forums/topic/convertfrom-pdf-powershell-cmdlet/
# https://social.technet.microsoft.com/Forums/scriptcenter/en-US/1268809d-5dc6-4cd2-a97f-a26bc3ae3a8b/using-powershell-to-parse-a-pdf-file?forum=ITCG
# https://www.reddit.com/r/PowerShell/comments/4ad9gp/crappy_powershell_script_to_scrape_the_cafeteria/
function Get-PdfText
{
[CmdletBinding()]
[OutputType([string])]
param (
[Parameter(Mandatory = $true)]
[string]
$Path
)
$Path = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Path)
try
{
$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $Path
}
catch
{
throw
}
$stringBuilder = New-Object System.Text.StringBuilder
for ($page = 1; $page -le $reader.NumberOfPages; $page++)
{
$text = [iTextSharp.text.pdf.parser.PdfTextExtractor]::GetTextFromPage($reader, $page)
$null = $stringBuilder.AppendLine($text)
}
$reader.Close()
return $stringBuilder.ToString()
}