-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpdfEncrypt_v1.0.ps1
More file actions
102 lines (78 loc) · 3.39 KB
/
Copy pathpdfEncrypt_v1.0.ps1
File metadata and controls
102 lines (78 loc) · 3.39 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Loading the Library
Add-Type -Path "$PSScriptRoot\dll_library\BouncyCastle.Crypto.dll"
[System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\dll_library\itextsharp.dll")
function PSUsing {
param (
[System.IDisposable] $excelSheetNumbernputObject = $(throw "The parameter -inputObject is required."),
[ScriptBlock] $scriptBlock = $(throw "The parameter -scriptBlock is required.")
)
Try {
&$scriptBlock
}
Finally {
if ($excelSheetNumbernputObject.psbase -eq $null) {
$excelSheetNumbernputObject.Dispose()
}
else {
$excelSheetNumbernputObject.psbase.Dispose()
}
}
}
$sourcePath = "$PSScriptRoot\OriginalPDF\"
$destinationPath = "$PSScriptRoot\EncryptedPDF\"
$excelReferenceSheet = "$PSScriptRoot\PasswdList\PDF_Password_List.xlsx"
$startRow=2
$fileNameCol=1
$PasswordCol=2
$excelSheetNumber=1
# Open the excel file having fileName (COLUMN A) & Password (COLUMN B)
$excel=new-object -com excel.application
$excel.DisplayAlerts = $false
$excel.Visible = $false
$workbook=$excel.workbooks.open($excelReferenceSheet)
$worksheet=$workbook.Sheets.Item($excelSheetNumber)
$rowIncrement=0;
# Find the Number of Rows & Columns
$WorksheetRange = $workSheet.UsedRange
$RowCount = $WorksheetRange.Rows.Count
$ColumnCount = $WorksheetRange.Columns.Count
Write-Host "RowCount:" $RowCount
Write-Host "ColumnCount" $ColumnCount
# Start loop to iterate on all the records of file name
for($i=$startRow; $i -le $RowCount; $i++) {
$filename = $worksheet.Cells.Item($i,$fileNameCol).value2
$SourceFilePathAndFileName= $sourcePath + $filename
#Write-Host "File Name: " $filepath
# Check if the given file exists
if (Test-Path -Path $SourceFilePathAndFileName -PathType Leaf){
Write-Host "Row: " $i " | Input File Found: " $SourceFilePathAndFileName
# Fetch the password
$password= $worksheet.Cells.Item($i,$PasswordCol).value2
# Write-Host "Encryption Password: " $password
# Destination Path and File Name
$destinationPathAndFileName = $destinationPath + $filename
Write-Host "Destination Path: " $destinationPathAndFileName
# Password Protect the Source Input File
New-Object PSObject -Property @{Source=$SourceFilePathAndFileName;Destination=$destinationPathAndFileName;Password=$password}
$file = New-Object System.IO.FileInfo $SourceFilePathAndFileName
$fileWithPassword = New-Object System.IO.FileInfo $destinationPathAndFileName
PSUsing ( $fileStreamIn = $file.OpenRead() ) {
PSUsing ( $fileStreamOut = New-Object System.IO.FileStream($fileWithPassword.FullName,[System.IO.FileMode]::Create,[System.IO.FileAccess]::Write,[System.IO.FileShare]::None) ) {
PSUsing ( $reader = New-Object iTextSharp.text.pdf.PdfReader $fileStreamIn ) {
[iTextSharp.text.pdf.PdfEncryptor]::Encrypt($reader, $fileStreamOut, $true, $password, $password, [iTextSharp.text.pdf.PdfWriter]::ALLOW_PRINTING)
}
}
}
}
else{
Write-Host "[!] Error Occured, File not Found: " $filePath
}
}
$excel.Close
$excel.Quit()
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet)
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Remove-Variable -Name excel