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

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

[VB.NET] โค้ดการ Convert แปลงไฟล์ MS Word ให้เป็น PDF จากของฟรีค่าย Syncfusion

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

311

กระทู้

502

โพสต์

6060

เครดิต

ผู้ดูแลระบบ

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

Rank: 9Rank: 9Rank: 9

เครดิต
6060

โค้ดการ Convert แปลงไฟล์ MS Word ให้เป็น PDF จากของฟรีค่าย Syncfusion ... สำหรับภาษาอังกฤษทำได้ค่อนข้างสมบูรณ์แบบ แต่ภาษาไทยจะมีปัญหาเรื่องของการตัดคำในแต่พารากราฟครับผม ...





Add References ... กรณีที่ Syncfusion ต่างเวอร์ชั่นกัน จะได้เอาไว้ดูเปรียบเทียบ ...

มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Imports Syncfusion.Pdf
  2. Imports Syncfusion.DocIO.DLS
  3. Imports Syncfusion.DocIO
  4. Imports Syncfusion.DocToPDFConverter
  5. Imports Syncfusion.OfficeChart
  6. Imports Syncfusion.OfficeChartToImageConverter
  7. Imports System.IO

  8. Public Class frmDoc2Pdf

  9.     Private strFileName As String

  10.     Private Sub btnWordDoc_Click(sender As System.Object, e As System.EventArgs) Handles btnWordDoc.Click
  11.         Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()
  12.         ' / Open File Dialog
  13.         With dlgOpenFile
  14.             .InitialDirectory = MyPath(Application.StartupPath) & "Doc"
  15.             .Title = "เลือกไฟล์เอกสาร Word Document"
  16.             .Filter = "Document (*.doc;*.docx)|*.doc;*.docx"
  17.             .FilterIndex = 1
  18.             .RestoreDirectory = True
  19.         End With
  20.         '/ Select OK after Browse ...
  21.         If dlgOpenFile.ShowDialog() = DialogResult.OK Then
  22.             txtFileName.Text = dlgOpenFile.FileName
  23.             strFileName = Path.GetFileNameWithoutExtension(dlgOpenFile.FileName)
  24.             'btnConvert.Focus()
  25.             Call btnConvert_Click(sender, e)
  26.         End If
  27.     End Sub

  28.     ' / --------------------------------------------------------------------------------
  29.     ' / Convert MS Word to PDF.
  30.     ' / --------------------------------------------------------------------------------
  31.     Private Sub btnConvert_Click(sender As System.Object, e As System.EventArgs) Handles btnConvert.Click
  32.         If txtFileName.Text.Trim.Length = 0 Then Return
  33.         Dim dlgSaveFile As New SaveFileDialog()
  34.         With dlgSaveFile
  35.             .Filter = "PDF|*.pdf"
  36.             .Title = "Export to PDF File"
  37.             .DefaultExt = "pdf"
  38.             .InitialDirectory = MyPath(Application.StartupPath) & "Doc"
  39.             .RestoreDirectory = True
  40.             .FileName = strFileName
  41.         End With
  42.         If dlgSaveFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
  43.             '/ Loads an existing Word document.
  44.             Dim wordDocument As New WordDocument(txtFileName.Text, FormatType.Docx)
  45.             '/ Initializes the ChartToImageConverter for converting charts during Word to pdf conversion.
  46.             wordDocument.ChartToImageConverter = New ChartToImageConverter()
  47.             '/ Sets the scaling mode for charts (Normal mode reduces the Pdf file size).
  48.             wordDocument.ChartToImageConverter.ScalingMode = ScalingMode.Normal
  49.             '/ Creates an instance of the DocToPDFConverter - responsible for Word to PDF conversion.
  50.             Dim converter As New DocToPDFConverter()
  51.             '/ Sets true to embed TrueType fonts.
  52.             converter.Settings.EmbedFonts = True
  53.             '/ Sets true to embed complete TrueType fonts.
  54.             converter.Settings.EmbedCompleteFonts = True
  55.             '/ Sets true to enable the fast rendering using direct PDF conversion.
  56.             converter.Settings.EnableFastRendering = True
  57.             '/ Converts Word document into PDF document.
  58.             Dim pdfDocument As PdfDocument = converter.ConvertToPDF(wordDocument)

  59.             '/ Save the document.
  60.             Dim pdfFile As String = dlgSaveFile.FileName
  61.             pdfDocument.Save(pdfFile)
  62.             pdfDocument.Close(True)
  63.             wordDocument.Close()
  64.             '/ Load PDF into PDFViewerControl.
  65.             Me.PdfViewerControl1.Load(pdfFile, "")
  66.         End If

  67.     End Sub

  68.     Private Sub txtFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFileName.KeyPress
  69.         ' / Prevents pressing any keys.
  70.         e.Handled = True
  71.     End Sub

  72.     Private Sub frmDoc2Pdf_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  73.         Me.Dispose()
  74.         GC.SuppressFinalize(Me)
  75.         Application.Exit()
  76.     End Sub

  77. #Region "FUNCTION"
  78.     Function MyPath(ByVal AppPath As String) As String
  79.         '/ Return Value
  80.         MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
  81.         '// If not found folder then put the \ (BackSlash) at the end.
  82.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  83.     End Function
  84. #End Region
  85. End Class
คัดลอกไปที่คลิปบอร์ด

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

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

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

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

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

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

GMT+7, 2024-4-30 03:25 , Processed in 0.192589 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

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