Cum să convertești rapid zeci de documente Word în PDF folosind PowerShell / Как быстро конвертировать десятки документов Word в PDF с помощью PowerShell

Примеры PowerShell команд
  
    $sourceFolder = "E:\DocumenteWord"
$destFolder   = "E:\DocumentePDF"

if (!(Test-Path $destFolder)) {
    New-Item -ItemType Directory -Path $destFolder | Out-Null
}

$wdFormatPDF = 17

$word = New-Object -ComObject Word.Application
$word.Visible = $false

$extensions = @("*.doc", "*.docx")

foreach ($ext in $extensions) {
    foreach ($file in Get-ChildItem -Path $sourceFolder -Filter $ext) {
        try {
            $pdfPath = Join-Path $destFolder ($file.BaseName + ".pdf")
            Write-Host "Convertim: $($file.FullName) -> $pdfPath"

            $doc = $word.Documents.Open($file.FullName, $false, $true)

            # Conversie cu string .NET pur
            $doc.SaveAs([string]$pdfPath, [ref]$wdFormatPDF)

            $doc.Close($false)
        }
        catch {
            Write-Host "Eroare la fișierul: $($file.FullName) - $($_.Exception.Message)" -ForegroundColor Red
        }
    }
}

$word.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($word) | Out-Null

Write-Host "Conversia a fost finalizată!"


    

Комментариев нет: