'iTextSharp関連の名前空間
Imports iTextSharp.text
Imports iTextSharp.text.pdf
'iTextSharp.text.FontクラスがSystem.Drawing.Fontクラスと
'混在するためiFontという別名を設定
Imports iFont = iTextSharp.text.Font
'ファイルIO関連の名前空間
Imports System.IO
Imports System.Diagnostics
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
y_text.Text = My.Settings("x")
x_Text.Text = My.Settings("y")
FontSize_text.Text = My.Settings("fontsize")
ToolTip1.SetToolTip(y_text, "文字のX座標です。(原点は左下、右移動量)")
ToolTip1.SetToolTip(x_Text, "文字のY座標です。(原点は左下、上移動量)")
With psize_combo
.Items.Add("A4")
.Items.Add("A3")
.Text = "A4"
End With
With fontsize_combo
.Items.Add("MS ゴシック")
.Items.Add("MS 明朝")
.Text = My.Settings("font")
End With
With ComboBox1
.Items.Add("縦")
.Items.Add("横")
.Text = "縦"
End With
End Sub
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
My.Settings("x") = y_text.Text
My.Settings("y") = x_Text.Text
My.Settings("font") = fontsize_combo.Text
My.Settings("fontsize") = FontSize_text.Text
End Sub
Private Sub TextBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles File_text.DragDrop
'ドロップされた内容を表示する
File_text.Text = e.Data.GetData(DataFormats.FileDrop)(0)
End Sub
Private Sub TextBox2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles File_text.DragEnter
'ドラッグされている内容が文字列型に変換可能な場合
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
'コピーを許可するようにドラッグ元に通知する
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub make_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Make.Click
If File_text.Text = "" Or File_text.Text = "ここにドラックします。" Then
Exit Sub
End If
'http://bbs.wankuma.com/index.cgi?mode=al2&namber=14833&KLOG=31 参考ソースファイル
'PDFファイルを開く
Dim reader As PdfReader = New PdfReader(File_text.Text)
'ページ数の取得
Dim n As Integer = reader.NumberOfPages
'ページの角度を取得 PrimoPDF = 90 , クセロPDF =0
Dim rotation As Integer = reader.GetPageRotation(1)
'ページサイズを取得
Dim width As Integer = reader.GetPageSize(1).Width
Dim height As Integer = reader.GetPageSize(1).Height
TextBox1.Text = "page:" + Str(n) + "/rotation:" + Str(rotation) + "/width:" + Str(width) + "/height:" + Str(height)
'ドキュメントを作成
Dim doc As Document
If CheckBox1.Checked = False Then
If rotation = 0 Then
If psize_combo.Text = "A4" Then
doc = New Document(PageSize.A4) 'ファイルの出力先を設定 A4縦の設定です
Else
doc = New Document(PageSize.A3) 'ファイルの出力先を設定 A3縦の設定です
End If
Else
If psize_combo.Text = "A4" Then
doc = New Document(PageSize.A4.Rotate()) 'ファイルの出力先を設定 A4横の設定です
Else
doc = New Document(PageSize.A3.Rotate()) 'ファイルの出力先を設定 A3横の設定です
End If
End If
Else
If ComboBox1.Text = "縦" Then
If psize_combo.Text = "A4" Then
doc = New Document(PageSize.A4) 'ファイルの出力先を設定 A4縦の設定です
Else
doc = New Document(PageSize.A3) 'ファイルの出力先を設定 A3縦の設定です
End If
Else
If psize_combo.Text = "A4" Then
doc = New Document(PageSize.A4.Rotate()) 'ファイルの出力先を設定 A4横の設定です
Else
doc = New Document(PageSize.A3.Rotate()) 'ファイルの出力先を設定 A3横の設定です
End If
End If
End If
'出力ファイル名を作成
Dim OutputPdfName As String = Microsoft.VisualBasic.Left(File_text.Text, Len(File_text.Text) - 4)
'出力ファイルを開く
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(OutputPdfName + "_.pdf", FileMode.Create))
'ドキュメントを開く
doc.Open()
Dim i As Integer
For i = 1 To n
Dim page As PdfImportedPage = writer.GetImportedPage(reader, i) '1ページ読み込み
Dim cb As PdfContentByte = writer.DirectContent
Dim grx As Graphic = New Graphic
'読み込んだページを加える
If rotation = 0 Then
cb.AddTemplate(page, 0, 0) '縦
Else
cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height) '横
End If
cb.BeginText()
'フォントとフォントサイズの指定
Dim bf As BaseFont
If fontsize_combo.Text = "MS ゴシック" Then
bf = BaseFont.CreateFont("c:\windows\fonts\msgothic.ttc,0", BaseFont.IDENTITY_H, True)
Else
bf = BaseFont.CreateFont("c:\windows\fonts\msmincho.ttc,0", BaseFont.IDENTITY_H, True)
End If
cb.SetFontAndSize(bf, Val(FontSize_text.Text))
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, Mes_Text.Text, Val(y_text.Text), Val(x_Text.Text), 0)
cb.EndText()
doc.NewPage()
Next i
'ドキュメントを閉じる
doc.Close()
MessageBox.Show("完了", "PDFコメント君")
End Sub
End Class