PowerShellからiTextSharpを呼び出してPDFを右綴じに変換します。
オリジナル(拙作)は
https://qiita.com/acknpop/items/e85c91947a407bc5a91a
ですが、iTextSharpの利用が信条的に受け入れがたい人のために。
下準備
まず始めに pdfsharp.dll を入手してください。
nugetするのが本筋ですが、下記はスクリプトを置くフォルダにlibというサブフォルダを作って、その下に pdfsharp.dll を入れる方式です。
スクリプト
以下を ConvertTo-RightBinding.ps1 として保存。
ConvertTo-RightBinding.ps1
# Convert PDF To Right-bindings
param(
[parameter(mandatory=$true)][string]$sourceDataPath,
[parameter(mandatory=$true)][string]$destinationPath
)
function ConvertTo-RightBinding($sourceDataPath, $destinationPath)
{
# path of pdfsharp.dll
[System.Reflection.Assembly]::LoadFrom((Join-Path (Split-Path $script:MyInvocation.MyCommand.Path) "\lib\PdfSharp.dll")) | Out-Null
$pr = [PdfSharp.Pdf.IO.PdfReader]
$mode = [PdfSharp.Pdf.IO.PdfDocumentOpenMode]
$input = $pr::Open([string]$sourceDataPath, $mode::Import)
try {
$doc = New-Object PdfSharp.Pdf.PdfDocument
#$input.Pages | %{$doc.AddPage($_)}
for($i = 0; $i -lt $input.PageCount; $i++){
$doc.AddPage($input.Pages[$i]) | Out-Null
}
$doc.ViewerPreferences.Direction = [PdfSharp.Pdf.PdfReadingDirection]::RightToLeft
$doc.Save([string]$destinationPath)
$input.Close()
}
catch {
Write-Error("Error: " + $_.Exception)
}
}
ConvertTo-RightBinding (Convert-Path $sourceDataPath) $destinationPath
使用例
PowerShellを立ち上げて、
PS> ConvertTo-RightBinding.ps1 元PDFファイル (Join-Path $PWD 出力先PDFファイル)
みたいに使います。
あとがき
なぜか、
https://stackoverflow.com/questions/27307124/returning-net-objects-in-powershell-functions?rq=1
みたいにパイプで繋ぐとうまく動かないので、forで回してます。