PowerShellスクリプトでテキスト文書のPDFファイルを新規に作成する

コンピュータ
itextsharp.dll
PowershellでZIPファイルをPDFファイルに変換する
はじめに このスクリプトは画像ファイル(.jpg.png)を含むZIPファイルをPDFファイルに変換します。 PowerShellスクリプトを初めて実行する場合 PowerShellスクリプトの実行を許可してください。 iTextSharp...

スクリプト

#
# 新規にPDFファイルの文書を作成するサンプル
#
# ファイル名:Create-NewPDF.ps1
#



$ErrorActionPreference = "stop"

[void][System.Reflection.Assembly]::LoadFrom((Join-Path $env:USERPROFILE "/Documents/Modules/pdf/itextsharp.dll"));



function CreatePdf
{
    param(
        [string] $pdfpath = "h:/script/sample3.pdf" # PDFファイルのパス
    )

    # A4横でドキュメントを作成
    #$pagesize = [iTextSharp.text.PageSize]::A4.Rotate()

    # A4でドキュメントを作成
    $pagesize = [iTextSharp.text.PageSize]::A4
    $doc = New-Object iTextSharp.text.Document($pagesize)
    
    # 出力ストリームを作成(ファイル)
    $stream = [System.IO.FileStream]::new($pdfpath, [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write, [System.IO.FileShare]::ReadWrite + [System.IO.FileShare]::Delete);

    # ファイルの出力先を設定
    $writer = [iTextSharp.text.pdf.PdfWriter]::GetInstance($doc, $stream);

    # ドキュメントを開く
    $doc.Open()

    # フォント
    $BaseFont = [iTextSharp.text.pdf.BaseFont]::CreateFont("C:\windows\fonts\msmincho.ttc,0",[iTextSharp.text.pdf.BaseFont]::"IDENTITY_H",$True)

    $pdfContentByte = $writer.DirectContent
    $pdfContentByte.SetFontAndSize($BaseFont, 20)
    
    # 文字を透明にする。
    # $pdfContentByte.SetTextRenderingMode([iTextSharp.text.pdf.PdfContentByte]::TEXT_RENDER_MODE_INVISIBLE) 

    # 文字列を表示する領域を確保
    $columntext = New-Object iTextSharp.text.pdf.ColumnText($pdfContentByte)
    $columntext.SetSimpleColumn(0, 0, $pagesize.width, $pagesize.height)
    

    # 文字列をセット
    $paragraph = New-Object iTextSharp.text.Paragraph("1行目`r`n2行目`r`n3行目`r`n4行目`r`n5行目`r`n6行目`r`n7行目`r`n8行目`r`n9行目`r`n10行目、123456890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890行末で自動的に改行されるかテストです。", $BaseFont)

    $pdfContentByte.BeginText()

    # 
    # PDFの編集ルーチンをここに記述
    # 

    #$pdfContentByte.ShowTextAligned([iTextSharp.text.pdf.PdfContentByte]::ALIGN_LEFT,"はじめまして世界",10,500,0)
    #文字寄せ、文字列、x,y,回転


    $columntext.AddElement($paragraph)
    $columntext.Go()


    $pdfContentByte.EndText()

    # ドキュメントを閉じる
    $doc.Close()

    # 出力ストリームを閉じる
    $stream.Close()
}

CreatePdf

コメント