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

如何使用Microsoft PowerShell跨Windows和Mac移除PDF元数据?

跨平台PowerShell移除PDF元数据方案

当然可以用PowerShell实现跨Windows和Mac的PDF元数据移除,前提是使用PowerShell 7+(它是跨平台版本,支持Windows、macOS和Linux)。以下提供两种实用方案:

方案一:使用跨平台iText 7库

iTextSharp是.NET Framework专属的旧版本,而iText 7是完全兼容.NET Core/.NET 5+的跨平台版本,可在Windows和Mac上运行。

操作步骤:

  1. 从NuGet获取iText 7的核心dll(iText.Kernel.dll和iText.IO.dll),下载对应平台的版本后放到本地目录。
  2. 运行以下PowerShell脚本:
# 加载iText 7的跨平台组件
Add-Type -Path "./iText.Kernel.dll"
Add-Type -Path "./iText.IO.dll"

# 定义文件路径
$inputPath = "./source.pdf"
$outputPath = "./source_no_metadata.pdf"

# 初始化PDF读写对象
$pdfReader = [iText.Kernel.Pdf.PdfReader]::new($inputPath)
$writerProps = [iText.Kernel.Pdf.PdfWriterProperties]::new()
$writerProps.SetXmpMetadata($null) # 清除XMP元数据
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($outputPath, $writerProps)

# 处理PDF文档,清除所有元数据
$pdfDoc = [iText.Kernel.Pdf.PdfDocument]::new($pdfReader, $pdfWriter)
$pdfDoc.GetDocumentInfo().Clear() # 清除文档属性元数据
$pdfDoc.Close()

Write-Host "元数据移除完成,输出文件:$outputPath"

方案二:使用qpdf命令行工具(更简便)

qpdf是一款跨平台的PDF处理工具,无需依赖.NET库,直接通过PowerShell调用即可,维护成本更低。

操作步骤:

  1. 安装qpdf:
    • Windows:使用Chocolatey执行 choco install qpdf,或下载官方二进制包
    • macOS:使用Homebrew执行 brew install qpdf
  2. 在PowerShell中执行移除元数据的命令:
# 直接移除所有元数据
qpdf --remove-all-metadata "input.pdf" "output_no_metadata.pdf"

# 或通过创建空白文档+复制页面的方式彻底清除元数据(更彻底)
qpdf --empty --pages "input.pdf" -- "output_no_metadata.pdf"

方案对比

  • iText 7适合需要自定义PDF处理逻辑的场景(比如仅移除特定元数据字段)
  • qpdf操作简单,无需编写复杂脚本,适合快速批量处理

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 16:42:55