|
จากตอนที่แล้วเป็น "การสร้างฟอร์ม PDF ด้วย Syncfusion PDF Net Library" มาคราวนี้เราจะทำการอ่านข้อมูลจากไฟล์ MS Access เพื่อโหลด หรือเติมข้อมูลลงไปในฟอร์ม ซึ่งแอดมินเลือกตัว Control หลักๆมาเกือบครบ ประกอบไปด้วย TextBox, CheckBox, ComboBox, RadioButton และ ListBox Control โดยมีวิธีการดังนี้คือ จะทำการโหลดฟอร์ม PDF Template (ต้นแบบ) ขึ้นมาก่อน แล้วทำการอ่านข้อมูลเข้ามา เพื่อทำการกรอกใส่เข้าไปใน Control แต่ละตัว สุดท้ายก็ทำการบันทึกชื่อไฟล์ PDF ด้วยวันที่และเวลา หรือหากท่านนำไปพัฒนาต่อก็ควรเปิด Save File Dialog เพื่อให้ Users บันทึกชื่อไฟล์เอง ...
ดาวน์โหลดของฟรี Syncfusion Essential รุ่น Community ได้ที่นี่ (Version 17.1.0.47) ... ย้ำอีกทีว่าของฟรี 100% แน่นอน ...
Add References ...
มาดูโค้ดฉบับเต็มกันเถอะ ...
- Imports System.Data
- Imports Syncfusion.Pdf
- Imports Syncfusion.Pdf.Graphics
- Imports Syncfusion.Pdf.Interactive
- Imports Syncfusion.Pdf.Parsing
- Imports System.Data.OleDb
- Imports System.IO
- Public Class frmDBtoPDF
- Dim Conn As New OleDbConnection
- Dim Cmd As New OleDbCommand
- Dim DR As OleDbDataReader
- Dim DA As New OleDbDataAdapter
- Dim DS As New DataSet
- Dim strConn As String = String.Empty
- Dim strSQL As String = String.Empty
- '//
- Dim strPathData As String = MyPath(Application.StartupPath) & "data"
- Dim strPathTemplate As String = MyPath(Application.StartupPath)
- Dim strFormFile As String = String.Empty
- ' / ---------------------------------------------------------------
- Private Sub frmDBtoPDF_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
- Try
- '// Delete all pdf file if exist.
- FileSystem.Kill(strPathData & "*pdf")
- '// Delete only the specified file.
- 'If System.IO.File.Exists(strPathData & strFormFile) = True Then
- 'System.IO.File.Delete(strPathData & strFormFile)
- 'End If
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- Finally
- If Conn.State = ConnectionState.Open Then Conn.Close()
- Me.Dispose()
- Application.Exit()
- End Try
- End Sub
- ' / ---------------------------------------------------------------
- Private Sub frmDBtoPDF_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- '// START
- Call ConnectDB()
- Call LoadDataBase()
- End Sub
- ' / ---------------------------------------------------------------
- Private Sub LoadDataBase()
- Try
- strSQL = _
- " SELECT tblPerson.PersonPK, tblPerson.PersonID, tblPerson.Name, tblPerson.Phone, tblPerson.Email, tblPerson.GenderFK, tblGender.Gender, " & _
- " tblPerson.JobTitleFK, tblJobTitle.JobTitle, tblPerson.LangFK, tblLanguage.LangName, tblPerson.QualificationFK, tblQualification.Qualification " & _
- " FROM ((tblJobTitle INNER JOIN (tblGender INNER JOIN tblPerson ON tblGender.GenderPK = tblPerson.GenderFK) ON tblJobTitle.JobTitlePK = tblPerson.JobTitleFK) " & _
- " INNER JOIN tblLanguage ON tblPerson.LangFK = tblLanguage.LangPK) INNER JOIN tblQualification ON tblPerson.QualificationFK = tblQualification.QualificationPK " & _
- " ORDER BY PersonPK "
- '// Open connection
- If Conn.State = ConnectionState.Closed Then Conn.Open()
- DA = New OleDb.OleDbDataAdapter(strSQL, Conn)
- DS = New DataSet
- DA.Fill(DS, "MyTest")
- dgvData.DataSource = DS.Tables("MyTest").DefaultView
- Call SetupDataGridView(dgvData)
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- DS.Dispose()
- DA.Dispose()
- End Sub
- ' / ---------------------------------------------------------------
- ' / Initialized DataGridView
- Private Sub SetupDataGridView(ByRef DGV As DataGridView)
- With DGV
- .RowTemplate.Height = 28
- .AllowUserToOrderColumns = True
- .AllowUserToDeleteRows = False
- .AllowUserToAddRows = False
- .ReadOnly = True
- .MultiSelect = False
- .SelectionMode = DataGridViewSelectionMode.FullRowSelect
- .Font = New Font("Tahoma", 9)
- .AlternatingRowsDefaultCellStyle.BackColor = Color.Orange
- .DefaultCellStyle.SelectionBackColor = Color.LightSkyBlue
- '/ Auto size column width of each main by sorting the field.
- .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
- End With
- End Sub
- ' / ---------------------------------------------------------------
- Private Sub dgvData_DoubleClick(sender As Object, e As System.EventArgs) Handles dgvData.DoubleClick
- If dgvData.RowCount <= 0 Then Return
- '// Read the value of the focus row.
- Dim PK = dgvData.Item(0, dgvData.CurrentRow.Index).Value '// Keep Primary Key
- strSQL = _
- " SELECT tblPerson.PersonPK, tblPerson.PersonID, tblPerson.Name, tblPerson.Phone, tblPerson.Email, tblPerson.GenderFK, tblGender.Gender, " & _
- " tblPerson.JobTitleFK, tblJobTitle.JobTitle, tblPerson.LangFK, tblLanguage.LangName, tblPerson.QualificationFK, tblQualification.Qualification " & _
- " FROM ((tblJobTitle INNER JOIN (tblGender INNER JOIN tblPerson ON tblGender.GenderPK = tblPerson.GenderFK) ON tblJobTitle.JobTitlePK = tblPerson.JobTitleFK) " & _
- " INNER JOIN tblLanguage ON tblPerson.LangFK = tblLanguage.LangPK) INNER JOIN tblQualification ON tblPerson.QualificationFK = tblQualification.QualificationPK " & _
- " WHERE PersonPK = " & PK & _
- " ORDER BY PersonPK "
- '// Load data into PDF Form.
- Call LoadDataToPDF(strSQL)
- End Sub
- ' / ---------------------------------------------------------------
- Private Sub CreatePdfForm()
- '/ Create a new PDF document
- Dim document As PdfDocument = New PdfDocument()
- '/ Add a new page to the PDF document
- Dim page As PdfPage = document.Pages.Add()
- '/ Set the True type font.
- Dim font As PdfFont = New PdfTrueTypeFont(New Font("Tahoma", 14), 12, True)
- '/ Create a solid brush.
- Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
- '/ Draw the string
- page.Graphics.DrawString("Job Application", font, brush, New PointF(250, 0))
- '// Create TextBox
- font = New PdfTrueTypeFont(New Font("Tahoma", 12), 12, True)
- page.Graphics.DrawString("Name", font, brush, New PointF(10, 20))
- '/ Create a text box field for name
- Dim textBoxField1 As PdfTextBoxField = New PdfTextBoxField(page, "Name")
- textBoxField1.Bounds = New RectangleF(10, 40, 200, 25)
- textBoxField1.ToolTip = "Name"
- document.Form.Fields.Add(textBoxField1)
- '/
- page.Graphics.DrawString("Email address", font, PdfBrushes.Black, New PointF(10, 80))
- '/ Create a text box field for email address
- Dim textBoxField3 As PdfTextBoxField = New PdfTextBoxField(page, "Email")
- textBoxField3.Bounds = New RectangleF(10, 100, 200, 20)
- textBoxField3.ToolTip = "Email address"
- document.Form.Fields.Add(textBoxField3)
- '//
- page.Graphics.DrawString("Phone", font, PdfBrushes.Black, New PointF(10, 140))
- '/ Create a text box field for phone
- Dim textBoxField4 As PdfTextBoxField = New PdfTextBoxField(page, "Phone")
- textBoxField4.Bounds = New RectangleF(10, 160, 200, 20)
- textBoxField4.ToolTip = "Phone"
- document.Form.Fields.Add(textBoxField4)
- '// Create RadioButton
- page.Graphics.DrawString("Gender", font, PdfBrushes.Black, New PointF(10, 200))
- '/ Create radio button for gender
- Dim employeesRadioList As PdfRadioButtonListField = New PdfRadioButtonListField(page, "Gender")
- document.Form.Fields.Add(employeesRadioList)
- page.Graphics.DrawString("Male", font, PdfBrushes.Black, New PointF(40, 220))
- Dim radioButtonItem1 As PdfRadioButtonListItem = New PdfRadioButtonListItem("Male")
- radioButtonItem1.Bounds = New RectangleF(10, 220, 20, 20)
- page.Graphics.DrawString("Female", font, PdfBrushes.Black, New PointF(140, 220))
- Dim radioButtonItem2 As PdfRadioButtonListItem = New PdfRadioButtonListItem("Female")
- radioButtonItem2.Bounds = New RectangleF(110, 220, 20, 20)
- employeesRadioList.Items.Add(radioButtonItem1)
- employeesRadioList.Items.Add(radioButtonItem2)
- '// Create ComboBox
- page.Graphics.DrawString("Which position you are applying for?", font, PdfBrushes.Black, New PointF(10, 260))
- '/ Create combo box for position
- Dim comboBox As PdfComboBoxField = New PdfComboBoxField(page, "JobTitle")
- comboBox.Bounds = New RectangleF(10, 280, 100, 20)
- comboBox.BorderColor = New PdfColor(Color.Gray)
- comboBox.ToolTip = "Job Title"
- comboBox.Items.Add(New PdfListFieldItem("Development", "Development"))
- comboBox.Items.Add(New PdfListFieldItem("Support", "Support"))
- comboBox.Items.Add(New PdfListFieldItem("Documentation", "Documentation"))
- document.Form.Fields.Add(comboBox)
- '// Languages in ListBox
- page.Graphics.DrawString("Languages Known", font, PdfBrushes.Black, New PointF(10, 320))
- '/ Create list box field for languages
- Dim listBoxField As PdfListBoxField = New PdfListBoxField(page, "Languages")
- listBoxField.Bounds = New RectangleF(10, 340, 100, 50)
- listBoxField.Items.Add(New PdfListFieldItem("English", "English"))
- listBoxField.Items.Add(New PdfListFieldItem("French", "French"))
- listBoxField.Items.Add(New PdfListFieldItem("German", "German"))
- document.Form.Fields.Add(listBoxField)
- '// Create CheckBox for Qualification.
- page.Graphics.DrawString("Highest Qualification", font, PdfBrushes.Black, New PointF(10, 410))
- '/ Add checked box field for associate degree
- Dim checkBoxField1 As PdfCheckBoxField = New PdfCheckBoxField(page, "Associate degree")
- page.Graphics.DrawString("Associate degree", font, PdfBrushes.Black, New PointF(30, 427))
- checkBoxField1.ToolTip = "Associate degree"
- checkBoxField1.Bounds = New RectangleF(10, 430, 10, 10)
- document.Form.Fields.Add(checkBoxField1)
- '/ Add checked box field for bachelor degree
- Dim checkBoxField2 As PdfCheckBoxField = New PdfCheckBoxField(page, "Bachelor degree")
- page.Graphics.DrawString("Bachelor degree", font, PdfBrushes.Black, New PointF(30, 447))
- checkBoxField2.ToolTip = "Bachelor degree"
- checkBoxField2.Bounds = New RectangleF(10, 450, 10, 10)
- document.Form.Fields.Add(checkBoxField2)
- '/ Add checked box field for Post Graduate
- Dim checkBoxField3 As PdfCheckBoxField = New PdfCheckBoxField(page, "Post Graduate")
- page.Graphics.DrawString("Post Graduate", font, PdfBrushes.Black, New PointF(30, 467))
- checkBoxField3.ToolTip = "Post Graduate"
- checkBoxField3.Bounds = New RectangleF(10, 470, 10, 10)
- document.Form.Fields.Add(checkBoxField3)
- '/ Save the document
- document.Save(strPathTemplate & "FormTemplate.pdf")
- '/ Close the document
- document.Close(True)
- '/ This will open the PDF file so, the result will be seen in default PDF Viewer.
- Process.Start(strPathTemplate & "FormTemplate.pdf")
- End Sub
- ' / ---------------------------------------------------------------
- Private Sub btnCreateFormPDF_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateFormPDF.Click
- Call CreatePdfForm()
- End Sub
- Private Sub btnDBtoPDF_Click(sender As System.Object, e As System.EventArgs) Handles btnDBtoPDF.Click
- '// Call event double click on DataGridView
- Call dgvData_DoubleClick(sender, e)
- End Sub
- ' / ---------------------------------------------------------------
- Private Sub LoadDataToPDF(ByVal sql As String)
- If Conn.State = ConnectionState.Closed Then Conn.Open()
- '/ Create a command and set its connection
- Cmd = New OleDbCommand(sql, Conn)
- '/ Execute command
- DR = Cmd.ExecuteReader()
- '/ Load the PDF document. (Template)
- Dim LoadedDocument As PdfLoadedDocument = New PdfLoadedDocument(strPathTemplate & "FormTemplate.pdf")
- '/ Get the loaded form
- Dim LoadedForm As PdfLoadedForm = LoadedDocument.Form
- While DR.Read()
- '// Syncfusion.Pdf.Parsing Namespace
- '// https://help.syncfusion.com/cr/cref_files/aspnet-classic/pdf/Syncfusion.Pdf.Web~Syncfusion.Pdf.Parsing_namespace.html
- '/ Get the values from database
- Dim name As String = DR("Name").ToString()
- Dim phone As String = DR("Phone").ToString()
- Dim email As String = DR("Email").ToString()
- Dim gender As String = DR("Gender").ToString
- '/ Assign values to fields
- TryCast(LoadedForm.Fields("Name"), PdfLoadedTextBoxField).Text = name
- TryCast(LoadedForm.Fields("Phone"), PdfLoadedTextBoxField).Text = phone
- TryCast(LoadedForm.Fields("Email"), PdfLoadedTextBoxField).Text = email
- '/ Read the 'Gender' radio button field
- Dim RadioButtonField As PdfLoadedRadioButtonListField = TryCast(LoadedForm.Fields("Gender"), PdfLoadedRadioButtonListField)
- TryCast(LoadedForm.Fields("Gender"), PdfLoadedRadioButtonListField).SelectedValue = gender
- '/ Read a combo box field
- Dim jobtitle As Byte = Val(DR("JobTitleFK").ToString)
- TryCast(LoadedForm.Fields("JobTitle"), PdfLoadedComboBoxField).SelectedIndex = jobtitle - 1
- '// Language FK
- Dim language As Byte = Val(DR("LangFK").ToString)
- TryCast(LoadedForm.Fields("Languages"), PdfLoadedListBoxField).SelectedIndex = New Integer(0) {language - 1}
- '// Qualification
- Dim qualification As Byte = Val(DR("QualificationFK").ToString)
- Select Case qualification
- Case 1
- TryCast(LoadedForm.Fields("Associate degree"), PdfLoadedCheckBoxField).Items(0).Checked = True
- Case 2
- TryCast(LoadedForm.Fields("Bachelor degree"), PdfLoadedCheckBoxField).Items(0).Checked = True
- Case 3
- TryCast(LoadedForm.Fields("Post Graduate"), PdfLoadedCheckBoxField).Items(0).Checked = True
- End Select
- '// Other parts, try to practice by doing yourself.
- End While
- '/ Save the document
- strFormFile = Format(Now, "ddMMyyyy-hhmmss") & ".pdf"
- LoadedDocument.Save(strPathData & strFormFile)
- '/ Close the document
- LoadedDocument.Close(True)
- '/ This will open the PDF file so, the result will be seen in default PDF Viewer
- Process.Start(strPathData & strFormFile)
- End Sub
- ' / ---------------------------------------------------------------
- Private Sub ConnectDB()
- '/ Connection string
- Dim dBFile As String = strPathData & "SampleDB.accdb"
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dBFile
- Try
- '/ Create a connection
- Conn = New OleDbConnection(strConn)
- '/ Open the connection and execute the select command
- Conn.Open()
- Catch ex As Exception
- MessageBox.Show(ex.Message)
- End Try
- End Sub
- ' / ---------------------------------------------------------------
- ' / Get my project path
- ' / AppPath = C:\My Project\bin\debug
- ' / Replace "\bin\debug" with ""
- ' / Return : C:\My Project\
- Function MyPath(ByVal AppPath As String) As String
- '/ MessageBox.Show(AppPath);
- AppPath = AppPath.ToLower()
- '/ Return Value
- MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
- '// If not found folder then put the \ (BackSlash) at the end.
- If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
- End Function
- End Class
คัดลอกไปที่คลิปบอร์ด
ดาวน์โหลดโค้ดต้นฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...
|
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน
x
|