代码提交
This commit is contained in:
1 parent
49af497085
commit
3eb307c5c5
155 files changed
+4706
-100
No files matched your search
@@ -0,0 +1,38 @@
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$InputDocx,
|
||||
[Parameter(Mandatory = $true)][string]$OutputPdf
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$word = $null
|
||||
$doc = $null
|
||||
try {
|
||||
$inputPath = (Resolve-Path -LiteralPath $InputDocx).Path
|
||||
$outputPath = [System.IO.Path]::GetFullPath($OutputPdf)
|
||||
$outputDir = [System.IO.Path]::GetDirectoryName($outputPath)
|
||||
[System.IO.Directory]::CreateDirectory($outputDir) | Out-Null
|
||||
|
||||
$word = New-Object -ComObject Word.Application
|
||||
$word.Visible = $false
|
||||
$word.DisplayAlerts = 0
|
||||
$doc = $word.Documents.Open($inputPath, $false, $false)
|
||||
|
||||
foreach ($toc in $doc.TablesOfContents) {
|
||||
$toc.Update() | Out-Null
|
||||
}
|
||||
$doc.Fields.Update() | Out-Null
|
||||
$doc.Save()
|
||||
$doc.ExportAsFixedFormat($outputPath, 17)
|
||||
}
|
||||
finally {
|
||||
if ($null -ne $doc) {
|
||||
$doc.Close(0)
|
||||
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($doc)
|
||||
}
|
||||
if ($null -ne $word) {
|
||||
$word.Quit()
|
||||
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($word)
|
||||
}
|
||||
[GC]::Collect()
|
||||
[GC]::WaitForPendingFinalizers()
|
||||
}
|
||||
Reference in new issue
Block a user