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

使用PowerShell向多文件夹下的多个PDF文件添加指定PDF页面

用PowerShell内置工具批量合并PDF页面

由于PowerShell没有专门内置的PDF合并命令,我们可以借助Windows系统自带的.NET框架System.IO.Packaging组件实现需求,具体步骤如下:

实现脚本

# 加载.NET必要程序集
Add-Type -AssemblyName WindowsBase

# 定义PDF合并函数
function Merge-Pdf {
    param(
        [Parameter(Mandatory=$true)][string]$SourcePdf,
        [Parameter(Mandatory=$true)][string]$AddPdf,
        [Parameter(Mandatory=$true)][string]$OutputPdf
    )

    # 复制源PDF到输出路径,避免直接修改原文件时的权限或锁定问题
    Copy-Item -Path $SourcePdf -Destination $OutputPdf -Force

    # 打开输出PDF包
    $outputPackage = [System.IO.Packaging.Package]::Open($OutputPdf, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite)
    try {
        # 打开要添加的PDF包
        $addPackage = [System.IO.Packaging.Package]::Open($AddPdf, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
        try {
            # 统计源PDF的页面数量,用于生成新页面的编号
            $sourcePageCount = ($outputPackage.GetParts() | Where-Object { $_.Uri -match '/page(\d+)\.frag$' } | Measure-Object).Count

            # 遍历要添加的PDF的所有页面部件
            foreach ($part in $addPackage.GetParts()) {
                if ($part.Uri -match '/page(\d+)\.frag$') {
                    $newPageNum = $sourcePageCount + [int]$matches[1]
                    $newUri = New-Object System.Uri("/page$newPageNum.frag", [System.UriKind]::Relative)
                    $newPart = $outputPackage.CreatePart($newUri, $part.ContentType, [System.IO.Packaging.CompressionOption]::Normal)

                    # 复制页面内容到新部件
                    $sourceStream = $part.GetStream()
                    $targetStream = $newPart.GetStream()
                    $sourceStream.CopyTo($targetStream)
                    $sourceStream.Close()
                    $targetStream.Close()
                }
            }
        }
        finally {
            $addPackage.Close()
        }
    }
    finally {
        $outputPackage.Close()
    }
}

# 配置路径参数
$fixedPagePdf = "C:\Files\page2.pdf"
$targetFolder = "C:\Folders"

# 遍历目标文件夹下所有PDF,筛选单页文件并合并
Get-ChildItem -Path $targetFolder -Filter "*.pdf" -Recurse | ForEach-Object {
    $currentPdf = $_.FullName

    # 检查当前PDF是否为单页
    $package = [System.IO.Packaging.Package]::Open($currentPdf, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
    try {
        $pageCount = ($package.GetParts() | Where-Object { $_.Uri -match '/page(\d+)\.frag$' } | Measure-Object).Count
        if ($pageCount -eq 1) {
            # 使用临时文件中转,避免原文件被锁定
            $tempPdf = Join-Path -Path $env:TEMP -ChildPath "$([System.Guid]::NewGuid()).pdf"
            Merge-Pdf -SourcePdf $currentPdf -AddPdf $fixedPagePdf -OutputPdf $tempPdf
            
            # 替换原文件
            Copy-Item -Path $tempPdf -Destination $currentPdf -Force
            Remove-Item -Path $tempPdf -Force

            Write-Host "已完成合并: $currentPdf"
        }
    }
    finally {
        $package.Close()
    }
}

注意事项

  1. 兼容性限制:该方法依赖PDF的标准包结构规范,部分加密、采用特殊压缩方式的PDF可能无法正常合并,完成后建议抽查文件完整性。
  2. 权限要求:运行脚本时需要拥有目标文件夹的读写权限,避免因权限不足导致文件无法替换。
  3. 临时文件:脚本会自动使用系统临时目录生成中转文件,合并完成后自动删除,无需手动清理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 00:36:13