# 批量给pdf添加页码 # 导入必要的程序集 Add-Type -AssemblyName System.Drawing Add-Type -AssemblyName System.Windows.Forms # 加载 iTextSharp.dll # 假设iTextSharp.dll位于C:\path\to\iTextSharp目录 $iTextSharpPath = "C:\Users\Administrator\source\repos\ConsoleApp2\packages\iTextSharp.5.5.13.4\lib\net461\iTextSharp.dll" # 加载iTextSharp.dll Add-Type -Path $iTextSharpPath # 假设BouncyCastle的DLL位于C:\path\to\BouncyCastle目录 $bouncyCastlePath = "C:\Python34\psLib\BouncyCastle.Cryptography.dll" # 加载BouncyCastle.Crypto.dll Add-Type -Path $bouncyCastlePath # 定义PDF处理函数 function Add-PdfPageNumbers { param ( [string]$inputFolder, [string]$outputFolder ) # 确保输出文件夹存在 if (-Not (Test-Path -Path $outputFolder)) { New-Item -ItemType Directory -Path $outputFolder | Out-Null } # 获取所有PDF文件 $pdfFiles = Get-ChildItem -Path $inputFolder -Filter *.pdf foreach ($pdfFile in $pdfFiles) { $inputFilePath = $pdfFile.FullName $outputFilePath = Join-Path -Path $outputFolder -ChildPath $pdfFile.Name # 打开PDF文档 $pdfDocument = [iTextSharp.text.pdf.PdfReader]::new($inputFilePath) $pageCount = $pdfDocument.NumberOfPages # 创建PDF写入器 $pdfStamper = [iTextSharp.text.pdf.PdfStamper]::new($pdfDocument, [System.IO.File]::Create($outputFilePath)) # 设置字体 $baseFont = [iTextSharp.text.pdf.BaseFont]::CreateFont([iTextSharp.text.pdf.BaseFont]::HELVETICA, [iTextSharp.text.pdf.BaseFont]::WINANSI, $false) $fontSize = 12 for ($i = 1; $i -le $pageCount; $i++) { $pdfContentByte = $pdfStamper.GetOverContent($i) $pdfContentByte.BeginText() $pdfContentByte.SetFontAndSize($baseFont, $fontSize) $pdfContentByte.ShowTextAligned([iTextSharp.text.pdf.PdfContentByte]::ALIGN_CENTER, "Page $i of $pageCount", 520, 20, 0) $pdfContentByte.EndText() } # 关闭PDF写入器 $pdfStamper.Close() $pdfDocument.Close() } } # 调用函数,指定输入和输出文件夹路径 $inputFolder = "C:\Users\Administrator\Desktop\Sign\SO2411160009-3\1BB0110A" $outputFolder = "C:\Users\Administrator\Desktop\Sign\SO2411160009-3\1BB0110A\output" Add-PdfPageNumbers -inputFolder $inputFolder -outputFolder $outputFolder
标签:Shell,Power,text,dll,iTextSharp,Path,pdf,outputFolder From: https://www.cnblogs.com/Tonyyang/p/18670890