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


Add References ... กรณีที่ Syncfusion ต่างเวอร์ชั่นกัน จะได้เอาไว้ดูเปรียบเทียบ ...
มาดูโค้ดฉบับเต็มกันเถอะ ...
- Imports Syncfusion.Pdf
- Imports Syncfusion.DocIO.DLS
- Imports Syncfusion.DocIO
- Imports Syncfusion.DocToPDFConverter
- Imports Syncfusion.OfficeChart
- Imports Syncfusion.OfficeChartToImageConverter
- Imports System.IO
- Public Class frmDoc2Pdf
- Private strFileName As String
- Private Sub btnWordDoc_Click(sender As System.Object, e As System.EventArgs) Handles btnWordDoc.Click
- Dim dlgOpenFile As OpenFileDialog = New OpenFileDialog()
- ' / Open File Dialog
- With dlgOpenFile
- .InitialDirectory = MyPath(Application.StartupPath) & "Doc"
- .Title = "เลือกไฟล์เอกสาร Word Document"
- .Filter = "Document (*.doc;*.docx)|*.doc;*.docx"
- .FilterIndex = 1
- .RestoreDirectory = True
- End With
- '/ Select OK after Browse ...
- If dlgOpenFile.ShowDialog() = DialogResult.OK Then
- txtFileName.Text = dlgOpenFile.FileName
- strFileName = Path.GetFileNameWithoutExtension(dlgOpenFile.FileName)
- 'btnConvert.Focus()
- Call btnConvert_Click(sender, e)
- End If
- End Sub
- ' / --------------------------------------------------------------------------------
- ' / Convert MS Word to PDF.
- ' / --------------------------------------------------------------------------------
- Private Sub btnConvert_Click(sender As System.Object, e As System.EventArgs) Handles btnConvert.Click
- If txtFileName.Text.Trim.Length = 0 Then Return
- Dim dlgSaveFile As New SaveFileDialog()
- With dlgSaveFile
- .Filter = "PDF|*.pdf"
- .Title = "Export to PDF File"
- .DefaultExt = "pdf"
- .InitialDirectory = MyPath(Application.StartupPath) & "Doc"
- .RestoreDirectory = True
- .FileName = strFileName
- End With
- If dlgSaveFile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
- '/ Loads an existing Word document.
- Dim wordDocument As New WordDocument(txtFileName.Text, FormatType.Docx)
- '/ Initializes the ChartToImageConverter for converting charts during Word to pdf conversion.
- wordDocument.ChartToImageConverter = New ChartToImageConverter()
- '/ Sets the scaling mode for charts (Normal mode reduces the Pdf file size).
- wordDocument.ChartToImageConverter.ScalingMode = ScalingMode.Normal
- '/ Creates an instance of the DocToPDFConverter - responsible for Word to PDF conversion.
- Dim converter As New DocToPDFConverter()
- '/ Sets true to embed TrueType fonts.
- converter.Settings.EmbedFonts = True
- '/ Sets true to embed complete TrueType fonts.
- converter.Settings.EmbedCompleteFonts = True
- '/ Sets true to enable the fast rendering using direct PDF conversion.
- converter.Settings.EnableFastRendering = True
- '/ Converts Word document into PDF document.
- Dim pdfDocument As PdfDocument = converter.ConvertToPDF(wordDocument)
- '/ Save the document.
- Dim pdfFile As String = dlgSaveFile.FileName
- pdfDocument.Save(pdfFile)
- pdfDocument.Close(True)
- wordDocument.Close()
- '/ Load PDF into PDFViewerControl.
- Me.PdfViewerControl1.Load(pdfFile, "")
- End If
- End Sub
- Private Sub txtFileName_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtFileName.KeyPress
- ' / Prevents pressing any keys.
- e.Handled = True
- End Sub
- Private Sub frmDoc2Pdf_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
- Me.Dispose()
- GC.SuppressFinalize(Me)
- Application.Exit()
- End Sub
- #Region "FUNCTION"
- Function MyPath(ByVal AppPath As String) As String
- '/ Return Value
- MyPath = AppPath.ToLower.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
- '// 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 Region
- End Class
คัดลอกไปที่คลิปบอร์ด
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...
|
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน
x
|