Repository navigation
Expand file tree
/
Copy pathPDFText.ps1
More file actions
139 lines (134 loc) · 6.55 KB
/
Copy pathPDFText.ps1
File metadata and controls
139 lines (134 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
param (
[Parameter(Mandatory = $true)] [string] $sSrcPath,
[Parameter(Mandatory = $true)] [string] $sDstPath,
[Parameter(Mandatory = $false)] [long] $lTgtPageStt = 1,
[Parameter(Mandatory = $false)] [long] $lTgtPageEnd = 0,
[Parameter(Mandatory = $true)] [string] $sAnnText,
[Parameter(Mandatory = $false)] [double] $dAnnTextSize = 12,
[Parameter(Mandatory = $false)] [string] $sAnnTextColor = "black",
[Parameter(Mandatory = $false)] [double] $dAnnTextPosX = 0,
[Parameter(Mandatory = $false)] [double] $dAnnTextPosY = 0
)
# セットアップ
function local:Setup() {
if (-not (Get-Command scoop -ErrorAction SilentlyContinue)) {
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
}
scoop bucket add extras
scoop install nuget
# その他のセットアップ
$ToolDir = [System.IO.Path]::Combine($PSScriptRoot, "tool")
$null = New-Item $ToolDir -ItemType Directory -ErrorAction SilentlyContinue
## PDFSharp
$ToolPath01 = [System.IO.Path]::Combine($ToolDir, "PDFsharp-GDI")
if ((Test-Path $ToolPath01) -eq $false) {
nuget install PDFsharp-GDI -Version 6.1.1 -OutputDirectory $ToolDir -Source 'https://api.nuget.org/v3/index.json'
}
}
<#
.SYNOPSIS
PDFファイルに指定したPDFスタンプを捺印します
.PARAMETER sSrcPath
スタンプを捺印されるPDFファイルのパスを指定します
.PARAMETER sDstPath
スタンプ後の出力先PDFファイルのパスを指定します
.PARAMETER lTgtPageStt
スタンプを追加する開始ページの番号を指定します
ページ番号は1から始まり0以下は最終ページとして扱われます
.PARAMETER lTgtPageEnd
スタンプを追加する終了ページの番号を指定します
ページ番号は1から始まり0以下は最終ページとして扱われます
.PARAMETER sAnnText
描画文字列
.PARAMETER dAnnTextSize
描画文字サイズ
.PARAMETER sAnnTextColor
描画文字色
.PARAMETER dAnnTextPosX
描画文字横軸の割合を指定します
0%は左端で100%は右端に位置します
.PARAMETER dAnnTextPosY
描画文字縦軸の割合を指定します
ページの短編を基準とした割合を指定します
.EXAMPLE
AnnotatePDFText "$($ENV:USERPROFILE)\Desktop\src.pdf" "$($ENV:USERPROFILE)\Desktop\dst.pdf" 1 0 "text" 32 "black" 50 50
#>
function local:AnnotatePDFText {
param (
[Parameter(Mandatory = $true)] [string] $sSrcPath,
[Parameter(Mandatory = $true)] [string] $sDstPath,
[Parameter(Mandatory = $false)] [long] $lTgtPageStt = 1,
[Parameter(Mandatory = $false)] [long] $lTgtPageEnd = 0,
[Parameter(Mandatory = $true)] [string] $sAnnText,
[Parameter(Mandatory = $false)] [double] $dAnnTextSize = 12,
[Parameter(Mandatory = $false)] [string] $sAnnTextColor = "black",
[Parameter(Mandatory = $false)] [double] $dAnnTextPosX = 0,
[Parameter(Mandatory = $false)] [double] $dAnnTextPosY = 0
)
begin {
[System.Reflection.Assembly]::LoadFrom([System.IO.Path]::Combine($PSScriptRoot,"tool","PDFsharp-GDI.6.1.1","lib","net472","PdfSharp-gdi.dll"))
}
process {
# 入力ファイルを開く
$pdfrdr = [PdfSharp.Pdf.IO.PdfReader]::Open([string]$sSrcPath, [PdfSharp.Pdf.IO.PdfDocumentOpenMode]::Import)
# 制御対象ページを解釈
# ・利便性のために最終ページを0ページ目として指定する事ができるようにしておく
$pagecount = $pdfrdr.pagecount
if ($lTgtPageStt -le 0){ $lTgtPageStt = $pagecount }
if ($lTgtPageEnd -le 0){ $lTgtPageEnd = $pagecount }
$lTgtPageStt = [System.Math]::Max($lTgtPageStt, 1)
$lTgtPageStt = [System.Math]::Min($lTgtPageStt, $pagecount)
$lTgtPageEnd = [System.Math]::Max($lTgtPageEnd, 1)
$lTgtPageEnd = [System.Math]::Min($lTgtPageEnd, $pagecount)
# 描画本体
$pdfdoc = New-Object PdfSharp.Pdf.PdfDocument
$pdffnt = [PdfSharp.Drawing.XFont]::new("Yu Mincho", $dAnnTextSize)
for($idx = 1; $idx -le $pagecount; $idx++) {
$page = $pdfdoc.AddPage($pdfrdr.Pages[$idx - 1])
if (($idx -ge $lTgtPageStt) -And ($idx -le $lTgtPageEnd)) {
# 出力座標を当該ページレイアウトに合わせる
$gfx = [PdfSharp.Drawing.XGraphics]::FromPdfPage($page)
$rot = ((($page.Rotate % 360) + 360 ) % 360)
switch ( $rot ) # 座標系
{
0 { $gfx.RotateTransform( 0); $gfx.TranslateTransform( 0, 0); }
90 { $gfx.RotateTransform( -90); $gfx.TranslateTransform(-$page.Height.Point, 0); }
180 { $gfx.RotateTransform(-180); $gfx.TranslateTransform( -$page.Width.Point,-$page.Height.Point); }
270 { $gfx.RotateTransform(-270); $gfx.TranslateTransform( 0, -$page.Width.Point); }
}
switch ( $rot ) # 領域サイズ
{
0 { $gwd=$page.Width.Point; $ght=$page.Height.Point; }
90 { $gwd=$page.Height.Point; $ght=$page.Width.Point; }
180 { $gwd=$page.Width.Point; $ght=$page.Height.Point; }
270 { $gwd=$page.Height.Point; $ght=$page.Width.Point; }
}
# 文字描画
$tx = $sAnnText
$tx = $tx.Replace("[cpage]", $idx)
$tx = $tx.Replace("[epage]", $pagecount)
## 領域
$px = $gwd * ($dAnnTextPosX / 100.0)
$py = $ght * ($dAnnTextPosY / 100.0)
$rt = New-Object PdfSharp.Drawing.XRect($px, $py, 0, 0)
## 色
$bc = [System.Drawing.ColorTranslator]::FromHtml($sAnnTextColor)
$br = New-Object PdfSharp.Drawing.XSolidBrush([PdfSharp.Drawing.XColor]::FromArgb($bc.A,$bc.R,$bc.G,$bc.B))
## 文字描画
$gfx.DrawString($tx, $pdffnt, $br, $rt, [PdfSharp.Drawing.XStringFormats]::Center)
}
}
$pdfdoc.Save($sDstPath)
}
end {}
}
try {
$null = Write-Host "---PDFText---"
AnnotatePDFText $sSrcPath $sDstPath $lTgtPageStt $lTgtPageEnd $sAnnText $dAnnTextSize $sAnnTextColor $dAnnTextPosX $dAnnTextPosY
}
catch {
$null = Write-Host "---例外発生---"
$null = Write-Host $_.Exception.Message
$null = Write-Host $_.ScriptStackTrace
$null = Write-Host "--------------"
}