Pau*_*aul 2 pdf powershell itextsharp
我有一个包含数千个PDF文件的文件夹.我需要根据文件名(将这些文件分组为2个或更多PDF文件)过滤这些文件,然后将这2个以上的PDF合并为1个PDF.
我可以对文件进行分组,但不确定将这些文件合并为1个PDF的最佳方法.我研究了iTextSharp,但无法在PowerShell中使用它.
iTextSharp是最好的方法吗?任何有关此代码的帮助将非常感激.
非常感谢保罗
已经看到一些这些PowerShell标记的问题也用itextsharp标记,并且总是想知道为什么在.NET中给出了答案,除非提问的人开始熟悉PowerShell,否则这可能会非常混乱.无论如何,这是一个简单的工作PowerShell脚本,可以帮助您入门:
$workingDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path;
$pdfs = ls $workingDirectory -recurse | where {-not $_.PSIsContainer -and $_.Extension -imatch "^\.pdf$"};
[void] [System.Reflection.Assembly]::LoadFrom(
[System.IO.Path]::Combine($workingDirectory, 'itextsharp.dll')
);
$output = [System.IO.Path]::Combine($workingDirectory, 'output.pdf');
$fileStream = New-Object System.IO.FileStream($output, [System.IO.FileMode]::OpenOrCreate);
$document = New-Object iTextSharp.text.Document;
$pdfCopy = New-Object iTextSharp.text.pdf.PdfCopy($document, $fileStream);
$document.Open();
foreach ($pdf in $pdfs) {
$reader = New-Object iTextSharp.text.pdf.PdfReader($pdf.FullName);
$pdfCopy.AddDocument($reader);
$reader.Dispose();
}
$pdfCopy.Dispose();
$document.Dispose();
$fileStream.Dispose();
Run Code Online (Sandbox Code Playgroud)
去测试:
不确定您打算如何根据文件名对PDF进行分组过滤,或者如果这是您的意图(无法判断您是否只是通过扩展名选择PDF),但这不应该太难添加.
祝好运.:)
| 归档时间: |
|
| 查看次数: |
5693 次 |
| 最近记录: |