0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerShellでPDFを右綴じに変換 (PDFSharp版)

Posted at

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で回してます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?