1

我正在编写一个从 PowerShell 创建 PDF 的脚本。我有它使用 Word,使用 ComObject。但这意味着我运行它的计算机必须有 Word。如果有办法从记事本制作 PDF 文件,我可能会徘徊。这是我从 Word 制作 PDF 的代码。

<`#make pdf

# Required Word Variables
$wdExportFormatPDF = 17
$wdDoNotSaveChanges = 0

# Create a hidden Word window
$word = New-Object -ComObject word.application
$word.visible = $false

# Add a Word document
$doc = $word.documents.add()

# Put the text into the Word document
$txt = Get-Content $txtPath
$selection = $word.selection
foreach($line in $txt){
$selection.typeText($line) | Format-wide
$selection.typeparagraph()
}
# Export the PDF file and close without saving a Word document
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF) 
if($?){write-host 'Users and Groups saved to ' $pdfPath -ForegroundColor Cyan}
$doc.close([ref]$wdDoNotSaveChanges)
$word.Quit()`>
4

0 回答 0