You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PowerShell向Word文档插入多页PDF/TIFF(解决仅插首页问题)

解决PowerShell插入多页PDF/TIFF到Word文档的问题

一、处理多页TIFF图片

TIFF是多页图像格式,Word的InlineShapes.AddPicture默认只插入第一页。可以通过.NET的System.Drawing类拆分TIFF为单页图片,再逐页插入:

步骤与代码示例

# 加载System.Drawing程序集
Add-Type -AssemblyName System.Drawing

# 初始化Word对象
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Add()

# 定义TIFF文件路径
$tiffPath = "C:\path\to\your\multi-page.tiff"

# 加载TIFF文件
$tiffImage = [System.Drawing.Image]::FromFile($tiffPath)

# 遍历每一页
for ($pageIndex = 0; $pageIndex -lt $tiffImage.GetFrameCount([System.Drawing.Imaging.FrameDimension]::Page); $pageIndex++) {
    $tiffImage.SelectActiveFrame([System.Drawing.Imaging.FrameDimension]::Page, $pageIndex)
    
    # 保存当前页为临时PNG文件
    $tempPath = "C:\temp\tiff_page_$pageIndex.png"
    $tiffImage.Save($tempPath, [System.Drawing.Imaging.ImageFormat]::Png)
    
    # 插入到Word文档
    $doc.Content.InlineShapes.AddPicture($tempPath)
    
    # 插入换行符,避免页面重叠
    $doc.Content.InsertAfter("`r`n")
}

# 清理临时文件和资源
$tiffImage.Dispose()
Remove-Item "C:\temp\tiff_page_*.png" -Force

# 保存文档
$doc.SaveAs("C:\path\to\output.docx")

二、处理多页PDF文件

Word原生方法无法直接插入PDF所有页面,需要先拆分PDF为单页,再插入。推荐两种方案:

方案1:将PDF每页转为图片后插入

用Ghostscript命令行工具(需提前安装)将PDF拆为单页图片,再插入Word:

# 初始化Word对象
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Add()

# 定义PDF路径和临时图片输出目录
$pdfPath = "C:\path\to\your\multi-page.pdf"
$tempDir = "C:\temp\pdf_pages"
New-Item -Path $tempDir -ItemType Directory -Force | Out-Null

# 用Ghostscript拆分PDF为单页PNG(需确保gs.exe在PATH中)
& "gs.exe" -dNOPAUSE -dBATCH -sDEVICE=png16m -r300 -sOutputFile="$tempDir\page_%03d.png" $pdfPath

# 遍历所有生成的图片文件并插入
Get-ChildItem -Path $tempDir -Filter "page_*.png" | Sort-Object Name | ForEach-Object {
    $doc.Content.InlineShapes.AddPicture($_.FullName)
    $doc.Content.InsertAfter("`r`n")
}

# 清理临时文件
Remove-Item $tempDir -Recurse -Force

# 保存文档
$doc.SaveAs("C:\path\to\output.docx")

方案2:拆分PDF为单页PDF后插入OLE对象

用PdfSharp库(需下载PdfSharp.dll)拆分PDF为单页,再用AddOLEObject插入:

# 加载PdfSharp程序集(需将PdfSharp.dll放在当前目录或指定路径)
Add-Type -Path "C:\path\to\PdfSharp.dll"

# 初始化Word对象
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Add()

# 定义PDF路径和临时目录
$pdfPath = "C:\path\to\your\multi-page.pdf"
$tempDir = "C:\temp\single_page_pdfs"
New-Item -Path $tempDir -ItemType Directory -Force | Out-Null

# 加载PDF并拆分
$pdfDocument = [PdfSharp.Pdf.IO.PdfReader]::Open($pdfPath)
for ($pageNum = 0; $pageNum -lt $pdfDocument.PageCount; $pageNum++) {
    $singlePageDoc = New-Object PdfSharp.Pdf.PdfDocument
    $singlePageDoc.AddPage($pdfDocument.Pages[$pageNum])
    $singlePagePath = "$tempDir\page_$pageNum.pdf"
    $singlePageDoc.Save($singlePagePath)
    $singlePageDoc.Dispose()
}
$pdfDocument.Dispose()

# 插入每个单页PDF作为OLE对象
Get-ChildItem -Path $tempDir -Filter "page_*.pdf" | Sort-Object Name | ForEach-Object {
    $doc.Content.InlineShapes.AddOLEObject($null, $_.FullName, $false, $true, $null)
    $doc.Content.InsertAfter("`r`n")
}

# 清理临时文件
Remove-Item $tempDir -Recurse -Force

# 保存文档
$doc.SaveAs("C:\path\to\output.docx")

注意事项

  • 处理TIFF时,PowerShell 5.x默认支持System.Drawing,PowerShell Core需安装System.Drawing.Common NuGet包
  • Ghostscript需自行下载安装,并将其bin目录加入系统PATH
  • PdfSharp库需从官方渠道下载,注意版本兼容性

内容的提问来源于stack exchange,提问作者sohail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 17:10:33