【发布时间】:2021-02-11 20:44:21
【问题描述】:
我正在尝试创建一个简单的表单来执行以下操作
- 浏览已创建的 pdf
- 有一个文本框,可以接受用于创建密码的文本/数字/特殊字符
- 提示保存受密码保护的 pdf 文件的位置,并在末尾添加 _protected 或其他内容,以便我可以将其保存在同一个位置而不会覆盖。 我确实创建了一部分代码,但我遇到了诸如 - 如何创建一个按钮来帮助我找到目标位置? 我似乎无法使文本框充当密码输入,而且似乎当我单击浏览按钮时它没有正确加载文件? 仅使用带有参数的 set-pdf 密码运行代码本身效果很好(不是我的代码),但我似乎在为此创建表单时遇到问题。 谁能提供一些关于热门问题的提示?
#############################################Functions##################################################################
function Set-PdfPassword {
<#
.SYNOPSIS
Protects the PDF with a password
.PARAMETER SourceFile
Path to the SourceFile
.PARAMETER DestinationFile
Password for the file
.PARAMETER Password
Full path of the password protected PDF
.PARAMETER PdfSharpPath
Full path to the required PdfSharp library
.PARAMETER Force
Tries to force destination file and directory creation and deletion of source files, even when they are read-only
.PARAMETER
RemoveSourceFiles
Deletes the source files after PDF is merged
.EXAMPLE
Set-PdfPassword -SourceFile "C:\TEMP\test.pdf" -DestinationFile "C:\TEMP\protected_test.pdf" -Password "SECRET" -PdfSharpPath 'C:\ProgramData\coolOrange\powerJobs\Modules\PdfSharp-gdi.dll' -Force -RemoveSourceFiles
#>
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]$SourceFile=
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]$DestinationFile,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$Password=
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$PdfSharpPath,
[switch]$Force,
[switch]$RemoveSourceFiles
)
Write-Host ">> $($MyInvocation.MyCommand.Name) >>"
if((Test-Path $PdfSharpPath) -eq $false) {
throw "Could not find pdfsharp assembly at $($PdfSharpPath)"
}
Add-Type -LiteralPath $PdfSharpPath
if((Test-Path $DestinationFile.FullName) -and $DestinationFile.IsReadOnly -and -not $Force) {
throw "Destination file '$($DestinationFile.FullName)' is read only"
}
$document = [PdfSharp.Pdf.IO.PdfReader]::Open($SourceFile.FullName)
$securitySettings = $document.SecuritySettings;
# Set Password
$securitySettings.UserPassword = $Password
# Restrict some permissions
$securitySettings.PermitAccessibilityExtractContent = $false;
$securitySettings.PermitAnnotations = $false;
$securitySettings.PermitAssembleDocument = $false;
$securitySettings.PermitExtractContent = $false;
$securitySettings.PermitFormsFill = $true;
$securitySettings.PermitFullQualityPrint = $false;
$securitySettings.PermitModifyDocument = $true;
$securitySettings.PermitPrint = $false;
Write-Host "Saving PDF"
if((Test-Path $DestinationFile.FullName) -and $Force) {
Remove-Item $DestinationFile.FullName -Force
}
$document.Save($DestinationFile.FullName)
}
function Select-FolderDialog {
param([String]$Description="Select Folder",
[String]$RootFolder="Desktop")
$objForm = New-Object System.Windows.Forms.FolderBrowserDialog
$objForm.Rootfolder = $RootFolder
$objForm.Description = $Description
$Show = $objForm.ShowDialog()
if ($Show -eq "OK")
{
return $objForm.SelectedPath
}
}
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "PDF Files (*.pdf)| *.pdf|All files (*.*)|*.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
###################### CREATING PS GUI TOOL #############################
#### Form settings #################################################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "Powershell Password Generator"
$Form.Size = New-Object System.Drawing.Size(1010,400)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$Form.Icon = $Icon
#### Title - Powershell GUI Tool ###################################################
$Label = New-Object System.Windows.Forms.Label
$LabelFont = New-Object System.Drawing.Font("Calibri",18,[System.Drawing.FontStyle]::Bold)
$Label.Font = $LabelFont
$Label.Text = "PasswordProtector v.1"
$Label.AutoSize = $True
$Label.Location = New-Object System.Drawing.Size(415,40)
$Form.Controls.Add($Label)
#### Input window with "Password For PDF" label ##########################################
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(10,50)
$InputBox.Size = New-Object System.Drawing.Size(180,20)
$Form.Controls.Add($InputBox)
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "Enter Password for PDF"
$Label2.AutoSize = $True
$Label2.Location = New-Object System.Drawing.Size(15,30)
$Form.Controls.Add($Label2)
#### Group boxes for buttons ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,95)
$groupBox.size = New-Object System.Drawing.Size(180,270)
$groupBox.text = "Options"
$Form.Controls.Add($groupBox)
#### Browse #################################################################
$Browse = New-Object System.Windows.Forms.Button
$Browse.Location = New-Object System.Drawing.Size(15,30)
$Browse.Size = New-Object System.Drawing.Size(150,60)
$Browse.Text = "Browse For PDF"
$Browse.Add_Click({Get-FileName})
$Browse.Cursor = [System.Windows.Forms.Cursors]::Hand
$groupBox.Controls.Add($Browse)
#### Output #################################################################
$Password = New-Object System.Windows.Forms.Button
$Password.Location = New-Object System.Drawing.Size(15,110)
$Password.Size = New-Object System.Drawing.Size(150,60)
$Password.Text = "Set PDF Password"
$Password.Add_Click({Set-PdfPassword})
$Password.Cursor = [System.Windows.Forms.Cursors]::Hand
$groupBox.Controls.Add($Password)
###################### END BUTTONS ######################################################
#### Output Box Field ###############################################################
$outputBox = New-Object System.Windows.Forms.RichTextBox
$outputBox.Location = New-Object System.Drawing.Size(200,100)
$outputBox.Size = New-Object System.Drawing.Size(780,265)
$outputBox.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Regular)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$outputBox.Text = " `
Welcome to PDF Password Generator."
$Form.Controls.Add($outputBox)
##############################################
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
【问题讨论】:
标签: powershell user-interface powershell-4.0 pdfsharp