ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3645|ตอบกลับ: 0

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

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6076

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6076




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 จะไม่ทำงาน


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

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

  6.     Private Sub btnCreateFormPDF_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateFormPDF.Click
  7.         Call CreatePdfForm()
  8.     End Sub

  9.     Private Sub CreatePdfForm()
  10.         '/ Create a new PDF document
  11.         Dim document As PdfDocument = New PdfDocument()
  12.         '/ Add a new page to the PDF document
  13.         Dim page As PdfPage = document.Pages.Add()
  14.         '/ Set the True type font.
  15.         Dim font As PdfFont = New PdfTrueTypeFont(New Font("Tahoma", 14), 12, True)
  16.         '/ Create a solid brush.
  17.         Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)
  18.         '/ Draw the string  
  19.         page.Graphics.DrawString("Job Application", font, brush, New PointF(250, 0))

  20.         '// Create TextBox
  21.         font = New PdfTrueTypeFont(New Font("Tahoma", 12), 12, True)
  22.         page.Graphics.DrawString("Name", font, brush, New PointF(10, 20))
  23.         '/ Create a text box field for name. "Name" is a name of TextBox Control.
  24.         Dim textBoxField1 As PdfTextBoxField = New PdfTextBoxField(page, "Name")
  25.         textBoxField1.Bounds = New RectangleF(10, 40, 200, 25)
  26.         textBoxField1.ToolTip = "Name"
  27.         document.Form.Fields.Add(textBoxField1)
  28.         '/
  29.         page.Graphics.DrawString("Email address", font, PdfBrushes.Black, New PointF(10, 80))
  30.         '/ Create a text box field for email address
  31.         Dim textBoxField3 As PdfTextBoxField = New PdfTextBoxField(page, "Email")
  32.         textBoxField3.Bounds = New RectangleF(10, 100, 200, 20)
  33.         textBoxField3.ToolTip = "Email address"
  34.         document.Form.Fields.Add(textBoxField3)
  35.         '//
  36.         page.Graphics.DrawString("Phone", font, PdfBrushes.Black, New PointF(10, 140))
  37.         '/ Create a text box field for phone
  38.         Dim textBoxField4 As PdfTextBoxField = New PdfTextBoxField(page, "Phone")
  39.         textBoxField4.Bounds = New RectangleF(10, 160, 200, 20)
  40.         textBoxField4.ToolTip = "Phone"
  41.         document.Form.Fields.Add(textBoxField4)

  42.         '// Create RadioButton
  43.         page.Graphics.DrawString("Gender", font, PdfBrushes.Black, New PointF(10, 200))
  44.         '/ Create radio button for gender
  45.         Dim employeesRadioList As PdfRadioButtonListField = New PdfRadioButtonListField(page, "Gender")
  46.         document.Form.Fields.Add(employeesRadioList)
  47.         page.Graphics.DrawString("Male", font, PdfBrushes.Black, New PointF(40, 220))
  48.         Dim radioButtonItem1 As PdfRadioButtonListItem = New PdfRadioButtonListItem("Male")
  49.         radioButtonItem1.Bounds = New RectangleF(10, 220, 20, 20)
  50.         page.Graphics.DrawString("Female", font, PdfBrushes.Black, New PointF(140, 220))
  51.         Dim radioButtonItem2 As PdfRadioButtonListItem = New PdfRadioButtonListItem("Female")
  52.         radioButtonItem2.Bounds = New RectangleF(110, 220, 20, 20)
  53.         employeesRadioList.Items.Add(radioButtonItem1)
  54.         employeesRadioList.Items.Add(radioButtonItem2)

  55.         '// Create ComboBox
  56.         page.Graphics.DrawString("Which position you are applying for?", font, PdfBrushes.Black, New PointF(10, 260))
  57.         '/ Create combo box for position
  58.         Dim comboBox As PdfComboBoxField = New PdfComboBoxField(page, "JobTitle")
  59.         comboBox.Bounds = New RectangleF(10, 280, 100, 20)
  60.         comboBox.BorderColor = New PdfColor(Color.Gray)
  61.         comboBox.ToolTip = "Job Title"
  62.         comboBox.Items.Add(New PdfListFieldItem("Development", "Development"))
  63.         comboBox.Items.Add(New PdfListFieldItem("Support", "Support"))
  64.         comboBox.Items.Add(New PdfListFieldItem("Documentation", "Documentation"))
  65.         document.Form.Fields.Add(comboBox)

  66.         '// Create ListBox
  67.         page.Graphics.DrawString("Languages Known", font, PdfBrushes.Black, New PointF(10, 320))
  68.         '/ Create list box field for languages
  69.         Dim listBoxField As PdfListBoxField = New PdfListBoxField(page, "Languages")
  70.         listBoxField.Bounds = New RectangleF(10, 340, 100, 50)
  71.         listBoxField.Items.Add(New PdfListFieldItem("English", "English"))
  72.         listBoxField.Items.Add(New PdfListFieldItem("French", "French"))
  73.         listBoxField.Items.Add(New PdfListFieldItem("German", "German"))
  74.         document.Form.Fields.Add(listBoxField)

  75.         '// Create CheckBox
  76.         page.Graphics.DrawString("Highest Qualification", font, PdfBrushes.Black, New PointF(10, 410))
  77.         '/ Add checked box field for associate degree
  78.         Dim checkBoxField1 As PdfCheckBoxField = New PdfCheckBoxField(page, "Associate degree")
  79.         page.Graphics.DrawString("Associate degree", font, PdfBrushes.Black, New PointF(30, 427))
  80.         checkBoxField1.ToolTip = "Associate degree"
  81.         checkBoxField1.Bounds = New RectangleF(10, 430, 10, 10)
  82.         document.Form.Fields.Add(checkBoxField1)
  83.         '/ Add checked box field for bachelor degree
  84.         Dim checkBoxField2 As PdfCheckBoxField = New PdfCheckBoxField(page, "Bachelor degree")
  85.         page.Graphics.DrawString("Bachelor degree", font, PdfBrushes.Black, New PointF(30, 447))
  86.         checkBoxField2.ToolTip = "Bachelor degree"
  87.         checkBoxField2.Bounds = New RectangleF(10, 450, 10, 10)
  88.         document.Form.Fields.Add(checkBoxField2)
  89.         '/ Add checked box field for Post Graduate
  90.         Dim checkBoxField3 As PdfCheckBoxField = New PdfCheckBoxField(page, "Post Graduate")
  91.         page.Graphics.DrawString("Post Graduate", font, PdfBrushes.Black, New PointF(30, 467))
  92.         checkBoxField3.ToolTip = "Post Graduate"
  93.         checkBoxField3.Bounds = New RectangleF(10, 470, 10, 10)
  94.         document.Form.Fields.Add(checkBoxField3)

  95.         '/ Save the document
  96.         document.Save(strPathData & "FormPDF.pdf")
  97.         '/ Close the document
  98.         document.Close(True)
  99.         '/ This will open the PDF file so, the result will be seen in default PDF Viewer.
  100.         Process.Start(strPathData & "FormPDF.pdf")
  101.     End Sub

  102.     ' / --------------------------------------------------------------------------------
  103.     ' / Get my project path
  104.     ' / AppPath = C:\My Project\bin\debug
  105.     ' / Replace "\bin\debug" with ""
  106.     ' / Return : C:\My Project\
  107.     Function MyPath(ByVal AppPath As String) As String
  108.         '/ MessageBox.Show(AppPath);
  109.         AppPath = AppPath.ToLower()
  110.         '/ Return Value
  111.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  112.         '// If not found folder then put the \ (BackSlash) at the end.
  113.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  114.     End Function

  115. End Class
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2010) ได้ที่นี่ ...


ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-5-8 06:53 , Processed in 0.080170 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้