Files
Large-scale_Agent_Network_S…/export_documents_pdf.ps1
T
2026-08-13 16:17:59 +08:00

28 lines
955 B
PowerShell

param(
[Parameter(Mandatory=$true)][string]$InputDirectory,
[Parameter(Mandatory=$true)][string]$PdfDirectory
)
$ErrorActionPreference = 'Stop'
New-Item -ItemType Directory -Force -Path $PdfDirectory | Out-Null
$word = New-Object -ComObject Word.Application
$word.Visible = $false
$word.DisplayAlerts = 0
try {
foreach ($file in Get-ChildItem -LiteralPath $InputDirectory -Filter '*.docx') {
$doc = $word.Documents.Open($file.FullName, $false, $true)
try {
$doc.Repaginate()
$pdf = Join-Path $PdfDirectory ($file.BaseName + '.pdf')
$doc.ExportAsFixedFormat($pdf, 17)
} finally {
$doc.Close($false)
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($doc) | Out-Null
}
}
} finally {
$word.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($word) | Out-Null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}