thongkorn โพสต์ 2019-6-10 12:40:39

[VB.NET] การสร้างฟอร์ม PDF ด้วย Syncfusion PDF Net Library

http://www.g2gnet.com/webboard/images/vbnet/syncfusionformpdf.png


Syncfusion Essential PDF เป็น .NET PDF Library ที่ใช้ในการสร้าง/อ่านและแก้ไขเอกสาร PDF โดยคุณสามารถสร้างรูปแบบ PDF ให้อยู่ในรูปแบบที่โต้ตอบ (Interactive Form) ได้ทั้งภาษา C# และ VB.NET ... บทความนี้แอดมินจะนำท่านผู้ชมมาทำการสร้างไฟล์เอกสาร PDF โดยจะประกอบไปด้วย TextBox, CheckBox, ComboBox, RadioButton และ ListBox Control สิ่งต่างๆเหล่านี้เป็นการกำหนดฟิลด์ต่างๆเอาไว้ (จะมีการติดต่อกับฐานข้อมูลในตอนหน้า) แต่ในตอนนี้เราสามารถที่จะทำการป้อนข้อมูล และบันทึกข้อมูลลงไปในเอกสาร PDF ได้ครับผม ... ประโยชน์ก็คือ นำไปใช้ในเรื่องของสำนักงานไร้กระดาษ (Paperless) เช่น ใบสมัครงาน การทำข้อสอบ หรือแม้แต่ตามสถานพยาบาล และอื่นๆอีกเยอะแยะมากมาย ท่านทั้งหลายก็ไปคิดเพื่อใส่ติ่งไอเดียของตัวเองก็แล้วกันครับ ...


ดาวน์โหลดของฟรี Syncfusion Essential รุ่น Community ได้ที่นี่ (Version 17.1.0.47) ... ย้ำอีกทีว่าของฟรี 100% แน่นอน ...


Add References ...ต้องใช้ Net Framework 4.0 ขึ้นไปเท่านั้น หากเป็น Client จะไม่ทำงาน
http://www.g2gnet.com/webboard/images/vbnet/syncfusionpdfref.png

มาดูโค้ดฉบับเต็มกันเถอะ ...
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Interactive

Public Class frmCreateFormPDF
    Dim strPathData As String = MyPath(Application.StartupPath) & "data\"

    Private Sub btnCreateFormPDF_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateFormPDF.Click
      Call CreatePdfForm()
    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. "Name" is a name of TextBox Control.
      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)

      '// Create 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
      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(strPathData & "FormPDF.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(strPathData & "FormPDF.pdf")
    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) ได้ที่นี่ ...


หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] การสร้างฟอร์ม PDF ด้วย Syncfusion PDF Net Library