'iTextSharp関連の名前空間 Imports iText = iTextSharp.text Imports iPdf = iTextSharp.text.pdf Imports iFont = iTextSharp.text.Font Imports iBaseColor = iTextSharp.text.BaseColor Partial Class _Default Inherits System.Web.UI.Page Protected Sub Button1_Click (ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim strPDFName As String = "Sample.pdf" 'iText ドキュメントを作成 Dim doc As iText.Document = _ New iText.Document(iText.PageSize.A4, 50, 50, 50, 50) Dim iPDFWriter As iPdf.PdfWriter = _ iPdf.PdfWriter.GetInstance (doc, New IO.FileStream _ (Request.MapPath("") & "/" & strPDFName, IO.FileMode.Create)) Dim pageSize As iText.Rectangle = doc.PageSize iPDFWriter.SetBoxSize("art", _ New iText.Rectangle(36, 50, pageSize.Width() - 50, _ pageSize.Height() - 36)) 'フォントを設定 Dim font As iFont = New iText.Font(DefaultBaseFont(), 12, iFont.NORMAL) Dim font_RED As iFont = _ New iText.Font(DefaultBaseFont(), 12, iFont.NORMAL, iBaseColor.RED) 'ヘッダー・フッターの設定 Dim events As New HeaderFooterPage() iPDFWriter.PageEvent = events '出力開始 doc.Open() '出力するPDFに説明を付与 doc.AddAuthor("作成者") doc.AddSubject("iTextサンプル") '文書に要素を追加 doc.Add(New iText.Paragraph("おはよう", font)) doc.Add(New iText.Paragraph("こんにちは", font_RED)) '出力終了 doc.Close() '別タグに表示 Dim sbScript As System.Text.StringBuilder = New System.Text.StringBuilder With sbScript .Append("<script language='javascript'>" & vbCrLf) .Append(vbTab & "window.open('" & strPDFName & "')" & vbCrLf) .Append("</script>") End With ClientScript.RegisterClientScriptBlock _ (Me.GetType(), "openwin", sbScript.ToString) End Sub Public Function DefaultBaseFont() As iPdf.BaseFont 'フォントのファイル名からフルパスを得る Dim base As iPdf.BaseFont base = iPdf.BaseFont.CreateFont(Microsoft.Win32.Registry.CurrentUser. _ OpenSubKey("Software"). _ OpenSubKey("Microsoft"). _ OpenSubKey("Windows"). _ OpenSubKey("CurrentVersion"). _ OpenSubKey("Explorer"). _ OpenSubKey("Shell Folders"). _ GetValue("Fonts").ToString() + "\msgothic.ttc, 1", _ iPdf.BaseFont.IDENTITY_H, iPdf.BaseFont.EMBEDDED) DefaultBaseFont = base End Function End Class 'ヘッダー・フッター設定 Public Class HeaderFooterPage Inherits iPdf.PdfPageEventHelper Public Overrides Sub OnEndPage _ (ByVal writer As iPdf.PdfWriter, ByVal document As iText.Document) MyBase.OnEndPage(writer, document) ' 初期化 Dim cb As iPdf.PdfContentByte cb = writer.DirectContent Dim pageNo As String = writer.PageNumber.ToString() Dim pageSize As iText.Rectangle = document.PageSize 'ヘッダー出力 cb.BeginText() cb.SetFontAndSize(HeaderFooterBaseFont, 8) cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(30)) cb.ShowTextAligned(iText.Element.ALIGN_RIGHT, _ "ヘッダー", pageSize.Width - 20, pageSize.GetTop(30), 0) cb.EndText() ' フッター出力 cb.BeginText() cb.SetFontAndSize(HeaderFooterBaseFont, 8) cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30)) cb.ShowTextAligned(iText.Element.ALIGN_CENTER, _ pageNo, pageSize.Width / 2, pageSize.GetBottom(30), 0) cb.EndText() End Sub Public Function HeaderFooterBaseFont() As iPdf.BaseFont 'フォントのファイル名からフルパスを得る Dim base As iPdf.BaseFont base = iPdf.BaseFont.CreateFont(Microsoft.Win32.Registry.CurrentUser. _ OpenSubKey("Software"). _ OpenSubKey("Microsoft"). _ OpenSubKey("Windows"). _ OpenSubKey("CurrentVersion"). _ OpenSubKey("Explorer"). _ OpenSubKey("Shell Folders"). _ GetValue("Fonts").ToString() + "\msgothic.ttc, 1", _ iPdf.BaseFont.IDENTITY_H, iPdf.BaseFont.EMBEDDED) HeaderFooterBaseFont = base End Function End Class |