windows

帳票をPDFで作成 VB.NET USE PDF Generator Net Free

帳票をPDFで作成 VB.NET USE PDF Generator Net Free

環境 Windows11 23H2 & Visual Studio 2022 & SQLServer 2022 & PDF Generator Net Free & WebView2

メイン画面

処理選択メニュー

設定メニュー

照会メニュー

照会は、指定したフォルダーのPDFを確認できます。更に、ブラウザとしても利用できます。

用紙方向の選択

用紙は、A4が基本になります。A4以外は、custom指定で処理します。

設定ファイル登録

作成されたPDFを表示

照会は、WebView2を利用しているのでPDFファイルだけでなく、サイト入力できるのでブラウザになります。

ソリューション

Form1.vb

Imports System.Data.OleDb
Imports System.Drawing.Drawing2D
Imports System.Drawing.Printing
Imports System.IO
Imports System.Net.WebRequestMethods
Imports System.Reflection
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports PdfGeneratorNetFree
Imports PdfGeneratorNetFree.PdfContentItem
Imports PdfGeneratorNetFree.PgnEnum
Imports PdfGeneratorNetFree.PgnStyle
Imports File = System.IO.File
Imports PaperSize = PdfGeneratorNetFree.PgnStyle.PaperSize

Public Class Form1

    'Pdf印刷の定義
    Private docPdf As New PdfDocument

    'pdfページサイズ
    Private pageSize As PaperSize

    'Pdfページ名
    Private pageName As String

    '最大行数( 1ページに印刷する行数 )
    Private rowMax As Integer

    'ページ数
    Private pageCount As Integer = 0

    '行数(印字する行位置)
    Private prtRow As Integer = 1

    ' 印字用フォント
    Private prtFon12 As Font = New Font("MS 明朝", 12, FontStyle.Regular)
    Private prtFon14 As Font = New Font("MS 明朝", 14, FontStyle.Regular)

    ' 印字用テキスト
    Private prtText As System.Drawing.Graphics

    ' マージン
    Private TopMargin As Integer
    Private LeftMargin As Integer

    ' 外部データ
    Private ReadFile As StreamReader
    Private LineText As String

    Private co As New OleDb.OleDbConnection
    Private rs As OleDb.OleDbDataReader
    Private cmd As New OleDb.OleDbCommand
    Private rsRet As Boolean = False

    '-------------------------------------------------------
    ' フォーム初期処理
    '-------------------------------------------------------
    Private Sub Form1_Load(ByVal sender As System.Object,
     ByVal e As System.EventArgs) Handles MyBase.Load

        INI()

        Me.KeyPreview = True

        '用紙方向セット
        TextBox3.Text = "縦"
        ComboBox3.Items.Clear()
        ComboBox3.Items.Add("縦")
        ComboBox3.Items.Add("横")

        Label2.Text = "出力フォルダーは、" + OutDir + " です。"

    End Sub

    '-------------------------------------------------------
    ' keyDown処理 Endボタンで終了
    '-------------------------------------------------------
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        If e.KeyCode = Keys.End Then
            End
        End If

    End Sub

    '-------------------------------------------------------
    ' keyPress処理 Enter = tab
    '-------------------------------------------------------
    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        If e.KeyChar = Chr(13) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If

    End Sub

    '-------------------------------------------------------
    '用紙方向コンボ処理
    '-------------------------------------------------------
    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged

        TextBox3.Text = ComboBox3.Text

    End Sub


    '-------------------------------------------------------
    ' 終了ボタン処理
    '-------------------------------------------------------
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        End

    End Sub

    '-------------------------------------------------------
    ' 印刷メニュー処理
    '-------------------------------------------------------
    Private Sub 印刷ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 印刷ToolStripMenuItem.Click

        docPdf = New PdfDocument

        'サイズは、A4Portrait, A4Landscape, Custom があります。
        If TextBox3.Text = "縦" Then
            pageSize = PaperSize.A4Portrait
        Else
            pageSize = PaperSize.A4Landscape
        End If

        pdfCreate()


    End Sub

    '-------------------------------------------------------
    ' 終了メニュー処理
    '-------------------------------------------------------
    Private Sub 終了ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 終了ToolStripMenuItem.Click

        End

    End Sub

    '-------------------------------------------------------
    ' 設定メニュー処理 Para.iniの登録へ
    '-------------------------------------------------------
    Private Sub 設定ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 設定ToolStripMenuItem1.Click

        Dim iform2 As New Form2
        iform2.ShowDialog()
        Label2.Text = "出力フォルダーは、" + OutDir + " です。"

    End Sub

    '-------------------------------------------------------
    ' 印刷ボタン処理
    '-------------------------------------------------------
    Private Sub Button1_Click(ByVal sender As System.Object,
     ByVal e As System.EventArgs) Handles Button1.Click

        docPdf = New PdfDocument

        If TextBox3.Text = "縦" Then
            pageSize = PaperSize.A4Portrait
        Else
            pageSize = PaperSize.A4Landscape
        End If

        pdfCreate()


    End Sub

    '-------------------------------------------------------
    ' 印刷の前処理
    '-------------------------------------------------------
    Private Sub docPdf_BeginPdf()

        Dim Count As Integer

        Try

            co.ConnectionString = CoString

            co.Open()

            cmd.Connection() = co

            cmd.CommandText = "select * from 取引先マスター order by 取引先コード"

            Count = cmd.ExecuteScalar()

            rs = cmd.ExecuteReader()

        Catch ex As OleDbException

            MsgBox("データベース接続エラーです。" & vbCr _
            & ex.Message & vbCr _
            & "終了します")
            Return

        End Try

        ' 用意したレコードセットにデータが無い場合印字処理を行わない
        If Count = 0 Then
            MessageBox.Show("印字データが存在しません")
            Return
        End If

    End Sub

    Private Sub pdfCreate()

        'フォント指定
        Dim windir = Environment.ExpandEnvironmentVariables("%windir%")
        docPdf.AddFont("明朝", windir + "\Fonts\msmincho.ttc", 0)

        '文字の設定
        Dim BlackText12_Cen = New TextStyle("明朝", 12, Color.Black, FontWeight.Regular, HorizontalAlign.Center, VerticalAlign.Middle)
        Dim BlackText12_Lef = New TextStyle("明朝", 12, Color.Black, FontWeight.Regular, HorizontalAlign.Left, VerticalAlign.Middle)
        Dim BlackText12_Rig = New TextStyle("明朝", 12, Color.Black, FontWeight.Regular, HorizontalAlign.Right, VerticalAlign.Middle)

        '印刷開始
        'データ獲得
        Call docPdf_BeginPdf()

        rowMax = 55
        If TextBox3.Text = "横" Then
            rowMax = 38
        End If
        pageCount = 1
        prtRow = 0
        Dim fst = 0
        Dim y = 0

        While rs.Read()

            If fst = 0 Then
                Header(pageCount)
                fst = 1
            End If

            If prtRow > rowMax Then
                pageCount += 1
                Header(pageCount)
            End If
            y = 14.4 * (rowMax - prtRow) + 28.8
            Dim detail_content() As AbstractPdfContentItem = {New Text(25, y, 85, 14.4, rs.Item("取引先コード"), BlackText12_Cen),
                                                                 New Text(110, y, 200, 14.4, rs.Item("取引先名"), BlackText12_Lef),
                                                                    New Text(310, y, 280, 14.4, rs.Item("取引先区分"), BlackText12_Cen)}
            docPdf.AddContent(pageName, detail_content)
            prtRow += 1

        End While

        '接続解除
        If Not co Is Nothing Then
            co.Close()
        End If

        Dim nowTime As Date
        Dim str_nowTime As String
        nowTime = DateTime.Now
        str_nowTime = nowTime.ToString("yyyyMMdd_HHmmss")

        File.WriteAllBytes(OutDir + "取引先マスター一覧表_" + str_nowTime + ".pdf", docPdf.GetBinary())

        strUrl = OutDir + "取引先マスター一覧表_" + str_nowTime + ".pdf"
        Dim iform3 As New Form3
        iform3.ShowDialog()

    End Sub

    Private Sub Header(page As Int16)

        pageName = "page" + Str(page)

        '印刷処理実体の作成
        'ページの設定
        docPdf.AddPage(pageName, pageSize)

        'Line 始点のX方向の値(pt), 始点のY方向の値(pt), 終点のX方向の値(pt), 終点のY方向の値(pt)
        'Rect 左下原点のXの値(pt), 左下原点のYの値(pt), 横幅の値(pt), 縦幅(pt)

        Dim DotStyle = New StrokeStyle(0.5, Color.Black, DashType.Dot)

        Dim SolidStyle = New StrokeStyle(0.2, Color.Black)

        'コンテンツ項目の塗りつぶしスタイルを作成する。
        Dim Recfill = New FillStyle(Color.Yellow)

        '文字の指定
        Dim BlueText14 = New TextStyle("明朝", 14, Color.Blue, FontWeight.Bold, HorizontalAlign.Center, VerticalAlign.Middle)
        Dim BlueText12_Cen = New TextStyle("明朝", 12, Color.Blue, FontWeight.Bold, HorizontalAlign.Center, VerticalAlign.Middle)
        Dim BlueText12_Lef = New TextStyle("明朝", 12, Color.Blue, FontWeight.Bold, HorizontalAlign.Left, VerticalAlign.Middle)
        Dim BlueText12_Rig = New TextStyle("明朝", 12, Color.Blue, FontWeight.Bold, HorizontalAlign.Right, VerticalAlign.Middle)

        '塗りつぶし

        If TextBox3.Text = "縦" Then
            Dim content5() As AbstractPdfContentItem = {New Rect(25, 14.4, 560, 14.4, fillStyle:=Recfill),
                                                                New Rect(25, 777.6, 560, 14.4, fillStyle:=Recfill)}
            docPdf.AddContent(pageName, content5)
        Else
            Dim content5() As AbstractPdfContentItem = {New Rect(25, 14.4, 560, 14.4, fillStyle:=Recfill),
                                                    New Rect(25, 532.8, 560, 14.4, fillStyle:=Recfill)}
            docPdf.AddContent(pageName, content5)
        End If

        '縦線
        If TextBox3.Text = "縦" Then
            Dim content1() As AbstractPdfContentItem = {New Line(25, 28.4, 25, 792, SolidStyle)}
            docPdf.AddContent(pageName, content1)
            Dim content2() As AbstractPdfContentItem = {New Line(110, 28.4, 110, 792, SolidStyle)}
            docPdf.AddContent(pageName, content2)
            Dim content3() As AbstractPdfContentItem = {New Line(310, 28.4, 310, 792, SolidStyle)}
            docPdf.AddContent(pageName, content3)
            Dim content4() As AbstractPdfContentItem = {New Line(585, 28.4, 585, 792, SolidStyle)}
            docPdf.AddContent(pageName, content4)
        Else
            Dim content1() As AbstractPdfContentItem = {New Line(25, 28.4, 25, 547.2, SolidStyle)}
            docPdf.AddContent(pageName, content1)
            Dim content2() As AbstractPdfContentItem = {New Line(110, 28.4, 110, 547.2, SolidStyle)}
            docPdf.AddContent(pageName, content2)
            Dim content3() As AbstractPdfContentItem = {New Line(310, 28.4, 310, 547.2, SolidStyle)}
            docPdf.AddContent(pageName, content3)
            Dim content4() As AbstractPdfContentItem = {New Line(585, 28.4, 585, 547.2, SolidStyle)}
            docPdf.AddContent(pageName, content4)
        End If

        '横線
        Dim x1 = 25
        Dim x2 = 585
        Dim y1 = 0
        Dim y2 = 0
        Dim yoko = 560
        Dim tate = 14.4
        Dim iMax = 54
        If TextBox3.Text = "横" Then
            iMax = 37
        End If
        For i = 1 To iMax
            y1 = 14.4 * i + 14.4
            y2 = y1
            Dim content() As AbstractPdfContentItem = {New Line(x1, y1, x2, y2, SolidStyle)}
            docPdf.AddContent(pageName, content)
            'y1 = 14.4 * i + 14.4
            'Dim content() As AbstractPdfContentItem = {New Rect(x1, y1, yoko, tate, SolidStyle)}
            'doc.AddContent("page1", content)
        Next

        '見出し項目
        Dim strPage = "page." + Str(page)
        If TextBox3.Text = "縦" Then
            Dim header_content() As AbstractPdfContentItem = {New Text(170, 810, 230, 15, "** 取引先マスター一覧表 **", BlueText14),
                                                            New Text(25, 778, 85, 14.4, "取引先コード", BlueText12_Cen),
                                                            New Text(110, 778, 200, 14.4, "取引先名", BlueText12_Cen),
                                                            New Text(310, 778, 280, 14.4, "取引先区分", BlueText12_Cen),
                                                            New Text(520, 810, 65, 14.4, strPage, BlueText12_Rig)}
            docPdf.AddContent(pageName, header_content)
        Else
            Dim header_content() As AbstractPdfContentItem = {New Text(170, 564, 230, 15, "** 取引先マスター一覧表 **", BlueText14),
                                                            New Text(25, 532.8, 85, 14.4, "取引先コード", BlueText12_Cen),
                                                            New Text(110, 532.8, 200, 14.4, "取引先名", BlueText12_Cen),
                                                            New Text(310, 532.8, 280, 14.4, "取引先区分", BlueText12_Cen),
                                                            New Text(520, 564, 65, 14.4, strPage, BlueText12_Rig)}
            docPdf.AddContent(pageName, header_content)
        End If

        '印字行数
        prtRow = 4

    End Sub

    Private Sub 照会ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles 照会ToolStripMenuItem1.Click

        Dim iform3 As New Form3
        iform3.ShowDialog()

    End Sub

End Class

Form2.vb

Public Class Form2
    Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim stCurrentDir As String = System.IO.Directory.GetCurrentDirectory()
        Dim n2 As Integer = FreeFile()
        Dim st As String

        Me.KeyPreview = True

        Try

            n = FreeFile()
            FileOpen(n, stCurrentDir & "\PARA.ini", OpenMode.Input)
            TextBox1.Text = LineInput(n)
            TextBox2.Text = LineInput(n)
            TextBox3.Text = LineInput(n)
            TextBox4.Text = LineInput(n)
            TextBox5.Text = LineInput(n)
            TextBox6.Text = LineInput(n)
            TextBox7.Text = LineInput(n)
            FileClose(n)

        Catch

            FileOpen(n2, stCurrentDir & "\PARA.ini", OpenMode.Output)
            st = ""
            PrintLine(n2, st)

            PrintLine(n2, st)

            PrintLine(n2, st)

            PrintLine(n2, st)

            PrintLine(n2, st)

            PrintLine(n2, st)

            PrintLine(n2, st)

            FileClose(n2)


        End Try
        ComboBox1.Items.Clear()

        Dim PrinterName As String
        For Each PrinterName _
            In Printing.PrinterSettings.InstalledPrinters
            ComboBox1.Items.Add(PrinterName)
        Next

    End Sub

    '-------------------------------------------------------
    'プリンター名コンボ処理
    '-------------------------------------------------------
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        TextBox6.Text = ComboBox1.Text

    End Sub

    '-------------------------------------------------------
    '登録ボタン処理
    '-------------------------------------------------------
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim stCurrentDir As String = System.IO.Directory.GetCurrentDirectory()
        Dim n2 As Integer = FreeFile()
        Dim st As String

        FileOpen(n2, stCurrentDir & "\PARA.ini", OpenMode.Output)
        st = TextBox1.Text
        PrintLine(n2, st)
        st = TextBox2.Text
        PrintLine(n2, st)
        st = TextBox3.Text
        PrintLine(n2, st)
        st = TextBox4.Text
        PrintLine(n2, st)
        st = TextBox5.Text
        PrintLine(n2, st)
        st = TextBox6.Text
        PrintLine(n2, st)
        st = TextBox7.Text
        PrintLine(n2, st)
        FileClose(n2)

        SYSTEM_NAME = TextBox1.Text
        USER_NAME = TextBox2.Text
        PASSWORD = TextBox3.Text
        SERVER = TextBox4.Text
        LIBL = TextBox5.Text
        prtName = TextBox6.Text
        OutDir = TextBox7.Text + "\"

        CoString = "Provider=SQLOLEDB.1;Data Source=" & SYSTEM_NAME _
                 & ";Initial Catalog=" & LIBL _
                 & ";Password=" & PASSWORD & ";User ID=" & USER_NAME & ";"

    End Sub

    '-------------------------------------------------------
    '終了ボタン処理
    '-------------------------------------------------------
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Me.Close()

    End Sub

    '-------------------------------------------------------
    'keyDown処理
    '-------------------------------------------------------
    Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        If e.KeyCode = Keys.End Then
            Me.Close()
        End If

    End Sub

    '-------------------------------------------------------
    'keyPress処理
    '-------------------------------------------------------
    Private Sub Form2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        If e.KeyChar = Chr(13) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        'FolderBrowserDialogクラスのインスタンスを作成
        Dim fbd As New FolderBrowserDialog

        '上部に表示する説明テキストを指定する
        fbd.Description = "フォルダを指定してください。"
        'ルートフォルダを指定する
        'デフォルトでDesktop
        fbd.RootFolder = Environment.SpecialFolder.Desktop
        '最初に選択するフォルダを指定する
        'RootFolder以下にあるフォルダである必要がある
        fbd.SelectedPath = "C:\Windows"
        'ユーザーが新しいフォルダを作成できるようにする
        'デフォルトでTrue
        fbd.ShowNewFolderButton = True

        'ダイアログを表示する
        If fbd.ShowDialog(Me) = DialogResult.OK Then
            '選択されたフォルダを表示する
            TextBox7.Text = fbd.SelectedPath
        End If

    End Sub

End Class

Form3.vb


'-------------------------------------------------------
' フォーム初期処理
'-------------------------------------------------------
Imports System.Security.Cryptography

Public Class Form3
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Me.KeyPreview = True

        If Len(strUrl) > 0 Then
            Me.WebView21.Source = New Uri(strUrl)
        End If

    End Sub

    '-------------------------------------------------------
    ' keyDown処理 Endボタンで終了
    '-------------------------------------------------------
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

        If e.KeyCode = Keys.End Then
            Me.Close()
        End If

    End Sub

    '-------------------------------------------------------
    ' keyPress処理 Enter = tab
    '-------------------------------------------------------
    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

        If e.KeyChar = Chr(13) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If

    End Sub

    '-------------------------------------------------------
    ' Textbox1 KeyDown処理
    '-------------------------------------------------------
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        If e.KeyCode = Keys.Enter And TextBox1.Text IsNot String.Empty Then

            Me.WebView21.Source = New Uri(TextBox1.Text)

        End If

    End Sub


    '-------------------------------------------------------
    ' 終了ボタン処理
    '-------------------------------------------------------
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Me.Close()

    End Sub

    '-------------------------------------------------------
    ' 参照ボタン処理
    '-------------------------------------------------------
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim ofd As OpenFileDialog = New OpenFileDialog()

        ofd.InitialDirectory = OutDir
        ofd.Filter = "PDFファイル(*.pdf)|*.pdf"

        If ofd.ShowDialog() = DialogResult.OK Then

            WebView21.Source = New Uri(ofd.FileName)
            TextBox1.Text = ofd.FileName

        End If

    End Sub

    '-------------------------------------------------------
    ' <<  Pre ボタン処理
    '-------------------------------------------------------
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        If WebView21.CanGoBack Then

            WebView21.GoBack()

        End If

    End Sub

    '-------------------------------------------------------
    ' Next  >> ボタン処理
    '-------------------------------------------------------
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        If WebView21.CanGoForward Then

            WebView21.GoForward()

        End If

    End Sub
    '-------------------------------------------------------
    ' Web Change 処理
    '-------------------------------------------------------
    Private Sub webView21_SourceChanged(sender As Object, e As Microsoft.Web.WebView2.Core.CoreWebView2SourceChangedEventArgs) Handles WebView21.SourceChanged

        Button3.Enabled = WebView21.CanGoBack
        Button4.Enabled = WebView21.CanGoForward

        TextBox1.Text = WebView21.Source.ToString()

    End Sub

End Class

module1.vb

Module Module1

    Public F1 As Form1

    Public fname As System.IO.StreamReader
    Public SYSTEM_NAME As String
    Public USER_NAME As String
    Public PASSWORD As String
    Public SERVER As String
    Public LIBL As String
    Public prtName As String
    Public OutDir As String
    Public F2Str As String
    Public strUrl As String
    Public n As Integer = FreeFile()

    Public CoString As String

    Public Sub INI()

        Try
            Dim stCurrentDir As String = System.IO.Directory.GetCurrentDirectory()
            n = FreeFile()
            FileOpen(n, stCurrentDir & "\PARA.ini", OpenMode.Input)
            SYSTEM_NAME = LineInput(n)
            USER_NAME = LineInput(n)
            PASSWORD = LineInput(n)
            SERVER = LineInput(n)
            LIBL = LineInput(n)
            prtName = LineInput(n)
            OutDir = LineInput(n) + "\"
            FileClose(n)
        Catch
            MsgBox("設定ファイルエラー:エラーNO:" &
            Err.Number & vbCr & Err.Description & vbCr &
            "設定してください。")

        End Try

        CoString = "Provider=SQLOLEDB.1;Data Source=" & SYSTEM_NAME _
                 & ";Initial Catalog=" & LIBL _
                 & ";Password=" & PASSWORD & ";User ID=" & USER_NAME & ";"

    End Sub

End Module

-windows

PAGE TOP