notes/snippet/powershell/convert.ps1

28 lines
817 B
PowerShell

function WordToPDF {
param(
[Parameter(Mandatory, ValueFromPipeline)]
[string] $WordFile
)
if ([string]::IsNullOrWhiteSpace($WordFile)) {
Write-Host "No file specified" -ForegroundColor Red
return;
}
$PDFFile = [System.IO.Path]::ChangeExtension($WordFile, ".pdf")
try {
$WordApp = New-Object -ComObject Word.Application
$Doc = $WordApp.Documents.Open($WordFile)
# https://learn.microsoft.com/zh-cn/office/vba/api/word.saveas2
# 设置原始文档的密码
# $Doc.Password = '123456'
# $Doc.WritePassword = '123456'
$Doc.SaveAs2([ref] $PDFFile, [ref] 17)
# FIXME: 尝试给 pdf 加密码,没有成功
# $Doc.SaveAs2([ref] $PDFFile, [ref] 17, $False, '123456', $False, '123456')
$Doc.Close()
$WordApp.Quit()
} catch {
Write-Host $_
}
}