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

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

[VB.NET] การสร้างเอกสาร PDF จาก DataTable ด้วย Syncfusion PDF .NET Library

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

310

กระทู้

501

โพสต์

6041

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6041









สำหรับโค้ดชุดนี้เป็นการใช้งาน Syncfusion PDF .NET Library ของฟรีจากค่าย Syncfusion ในการนำข้อมูลจาก DataTable มาแสดงผลในรูปแบบของตาราง รวมไปถึงวิธีการเพิ่มโลโก้กราฟิค และข้อความเข้ามาใส่ในเอกสาร แล้วแปลง (Convert) ให้เอกสารอยู่ในรูปแบบ PDF (Portable Document Format) ...

มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Imports Syncfusion.Pdf
  2. Imports Syncfusion.Windows.Forms
  3. Imports Syncfusion.Pdf.Parsing
  4. Imports Syncfusion.Windows.Forms.Grid
  5. Imports Syncfusion.Pdf.Graphics
  6. Imports Syncfusion.Pdf.Tables

  7. '/ H E L P
  8. '/ https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Tables.PdfLightTable.html

  9. Public Class frmPdfLightTable
  10.     '/ Customize the application path.
  11.     Dim strPathPDF As String = MyPath(Application.StartupPath)

  12.     ' / --------------------------------------------------------------------------------
  13.     ' / Get my project path
  14.     ' / AppPath = C:\My Project\bin\debug
  15.     ' / Replace "\bin\debug" with ""
  16.     ' / Return : C:\My Project\
  17.     Function MyPath(ByVal AppPath As String) As String
  18.         '/ Return Value
  19.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "")
  20.         '// Put the \ (BackSlash) at the end.
  21.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  22.     End Function

  23.     '/ START HERE
  24.     Private Sub frmPdfLightTable_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  25.         Call SamplePDF()
  26.     End Sub

  27.     ' / --------------------------------------------------------------------------------
  28.     ' / Create or Generate PDF with Syncfusion.
  29.     Private Sub SamplePDF()
  30.         '/ Create a new PDF Document.
  31.         Dim Doc As New PdfDocument()
  32.         Doc.PageSettings.Size = PdfPageSize.A4
  33.         Doc.PageSettings.Orientation = PdfPageOrientation.Portrait
  34.         '/ Size A5 and Landscape
  35.         'Doc.PageSettings.Size = PdfPageSize.A5
  36.         'Doc.PageSettings.Orientation = PdfPageOrientation.Landscape
  37.         '/ Add a Page.
  38.         Dim Page As PdfPage = Doc.Pages.Add()

  39.         '/ Create a PdfLightTable  
  40.         Dim PdfLightTable As New PdfLightTable
  41.         Try
  42.             '/ Assign data source.
  43.             PdfLightTable.DataSource = GetDataTable()
  44.             '/ Set Column Headers of PdfLightTable
  45.             With PdfLightTable
  46.                 .Columns(0).ColumnName = "Primary Key"
  47.                 .Columns(1).ColumnName = "รหัสสินค้า"
  48.                 .Columns(2).ColumnName = "ชื่อสินค้า"
  49.                 .Columns(3).ColumnName = "ราคา"
  50.             End With
  51.             '/ Create PDF graphics for the page
  52.             Dim logo As PdfGraphics = Page.Graphics
  53.             '/ Load the image from the disk.
  54.             Dim image As New PdfBitmap(strPathPDF & "g2gnet.png")
  55.             '/ Draw the image.
  56.             logo.DrawImage(image, 10, 0)
  57.             '/ Create Text Header.
  58.             Dim HeaderFont As PdfFont = New PdfTrueTypeFont(New Font("Century Gothic", 18, FontStyle.Bold), True)
  59.             '/ Create PDF graphics for the page.
  60.             Dim graphics As PdfGraphics = Page.Graphics
  61.             '/ Draw the text.
  62.             graphics.DrawString("PDF Light Table Syncfusion (PDF .NET Library)", HeaderFont, PdfBrushes.Black, New PointF(100, 10))
  63.             '/ Set new Font.
  64.             HeaderFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 20, FontStyle.Bold), True)    '/ True = Unicode
  65.             graphics.DrawString("ทดสอบภาษาไทย ประถมศึกษาชั้นปีที่ 1", HeaderFont, PdfBrushes.Black, New PointF(130, 35))

  66.             '/ Declare font and define the header style in the table.
  67.             Dim Font As PdfFont = New PdfTrueTypeFont(New Font("Arial Unicode MS", 12, FontStyle.Regular), True)
  68.             Dim MyStyle = New PdfCellStyle(Font, PdfBrushes.Black, PdfPens.Orange)
  69.             Dim HeaderStyle As New PdfCellStyle(Font, PdfBrushes.White, PdfPens.Orange)
  70.             HeaderStyle.BackgroundBrush = PdfBrushes.Orange
  71.             With PdfLightTable
  72.                 .Style.HeaderStyle = HeaderStyle
  73.                 .Style.DefaultStyle = MyStyle
  74.                 '/ Set cell padding, Spacing.
  75.                 .Style.CellPadding = 5
  76.                 .Style.CellSpacing = 0
  77.             End With

  78.             '/ Create a new string format.
  79.             Dim Format As New PdfStringFormat
  80.             For i = 0 To 2
  81.                 '/ Set the text Alignment columns 0- 2.
  82.                 Format.Alignment = PdfTextAlignment.Left
  83.                 Format.LineAlignment = PdfVerticalAlignment.Middle
  84.                 PdfLightTable.Columns(i).StringFormat = Format
  85.             Next
  86.             '/ Set new stringformat.
  87.             Format = New PdfStringFormat
  88.             With Format
  89.                 .Alignment = PdfTextAlignment.Right
  90.                 .LineAlignment = PdfVerticalAlignment.Middle
  91.             End With
  92.             '/ Add the string format to PdfLightTable column.
  93.             PdfLightTable.Columns(3).StringFormat = Format

  94.             '/ Show header in the table.
  95.             PdfLightTable.Style.ShowHeader = True
  96.             '/ Draw grid to the Page of PDF Document.
  97.             PdfLightTable.Draw(Page, New PointF(10, 76))

  98.             '/ Save the Document
  99.             Doc.Save(strPathPDF & "Output.pdf")
  100.             '/ Close the Document
  101.             Doc.Close(True)
  102.             '/ Open PDF on PDFViewerControl
  103.             Me.PdfViewerControl1.Load(strPathPDF & "Output.pdf", "")    '/ "" No Password

  104.         Catch ex As Exception
  105.             MessageBoxAdv.MessageBoxStyle = MessageBoxAdv.Style.Metro
  106.             MessageBoxAdv.MessageFont = New Font("Tahoma", 12, FontStyle.Regular)
  107.             MessageBoxAdv.Show(ex.Message, "รายงานความผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  108.         End Try

  109.     End Sub

  110.     ' / --------------------------------------------------------------------------------
  111.     '// SAMPLE DATATABLE
  112.     Private Function GetDataTable() As DataTable
  113.         Dim DT As New DataTable
  114.         '// Add Columns
  115.         With DT.Columns
  116.             .Add("PK", GetType(Integer))
  117.             .Add("ProductID", GetType(String))
  118.             .Add("ProductName", GetType(String))
  119.             .Add("UnitPrice", GetType(String))
  120.         End With
  121.         '// Add Rows
  122.         With DT.Rows
  123.             '// PK, ProductID, ProductName, UnitPrice
  124.             .Add(1, "01", "Classic Chicken", "45.00")
  125.             .Add(2, "02", "Mexicana", "85.00")
  126.             .Add(3, "03", "Lemon Shrimp", "110.00")
  127.             .Add(4, "04", "Bacon", "90.00")
  128.             .Add(5, "05", "Spicy Shrimp", "120.00")
  129.             .Add(6, "06", "Tex Supreme", "80.00")
  130.             .Add(7, "07", "Fish", "100.00")
  131.             .Add(8, "08", "Pepsi Small", "20.00")
  132.             .Add(9, "09", "Coke Small", "20.00")
  133.             .Add(10, "10", "7Up Small", "20.00")
  134.             .Add(11, "11", "มอคค่า", "85.00")
  135.             .Add(12, "12", "อเมริกาโน่", "95.00")
  136.             .Add(13, "13", "เอ็กซ์เพรสโซ่", "115.00")
  137.             .Add(14, "14", "ชานมไข่มุก", "45.00")
  138.             .Add(15, "15", "เหล้าเป็ก", "99.00")
  139.         End With
  140.         Return DT
  141.     End Function

  142.     Private Sub frmPdfLightTable_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  143.         Me.Dispose()
  144.         GC.SuppressFinalize(Me)
  145.         Application.Exit()
  146.     End Sub

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

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

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

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

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

2

โพสต์

10

เครดิต

Newbie

Rank: 1

เครดิต
10
โพสต์ 2022-8-2 14:51:28 | ดูโพสต์ทั้งหมด

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

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

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

GMT+7, 2024-4-24 05:04 , Processed in 0.106718 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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