[Visual Basic] Chuyển đổi file Word sang file PDF
Chào các bạn, hôm nay mình sẽ hướng dẫn mọi người viết chương trình đơn giản để chuyển đổi file
Viết một ứng dụng chuyển file word sang file pdf, ứng dụng nhỏ này có thể tích hợp vào các phần mềm của bạn khi bạn có nhu cầu chuyển đổi.
Các bạn đừng có comment với mình là giờ đây các phiên bản word có thể save lại được thành file pdf, sao còn viết ứng dụng này gì nữa. laugh
- Đầu tiên để có thể chuyển đổi file word sang file pdf ta sẽ import thư viện
Các bạn nhớ add Reference
Như vậy đã hoàn thành, chúc mọi người thành công.
Word
sang file PDF
nhanh nhất mà không cần phải sữ dụng nhiều thứ khác.Viết một ứng dụng chuyển file word sang file pdf, ứng dụng nhỏ này có thể tích hợp vào các phần mềm của bạn khi bạn có nhu cầu chuyển đổi.
Các bạn đừng có comment với mình là giờ đây các phiên bản word có thể save lại được thành file pdf, sao còn viết ứng dụng này gì nữa. laugh
Chương trình có giao diện như hình bên dưới:
- Đầu tiên để có thể chuyển đổi file word sang file pdf ta sẽ import thư viện
Microsoft.Office.Interop.Word
, code imports thư viện interpop.word
vào:Imports Microsoft.Office.Interop.Word
Các bạn nhớ add Reference
Microsoft.office.intertop.Word
vào project nha.
- Ta viết 1 thuộc tính set và get cho đối tượng WordDocument()
và khai báo 1 biến m_wordDocument
như sau:Public Property wordDocument() As Microsoft.Office.Interop.Word.Document Get Return m_wordDocument End Get Set(value As Microsoft.Office.Interop.Word.Document) m_wordDocument = value End Set End Property Private m_wordDocument As Microsoft.Office.Interop.Word.Document
- Viết sự kiện chọn file word:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim dlg As New OpenFileDialog() ' Set filter for file extension and default file extension dlg.DefaultExt = ".doc" dlg.Filter = "Word documents (.doc)|*.docx;*.doc" ' Display OpenFileDialog by calling ShowDialog method Dim result As Nullable(Of Boolean) = dlg.ShowDialog() ' Get the selected file name and display in a TextBox If result = True Then ' Open document Dim filename As String = dlg.FileName FileNameTextBox.Text = filename End If End Sub
- Viết sự kiện convert file word sang file pdf:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim appWord As New Microsoft.Office.Interop.Word.Application() wordDocument = appWord.Documents.Open(FileNameTextBox.Text) Dim sfd As New SaveFileDialog() sfd.Filter = "PDF Documents|*.pdf" Try If sfd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Dim ext As String = System.IO.Path.GetExtension(sfd.FileName) Select Case ext Case ".pdf" wordDocument.ExportAsFixedFormat(sfd.FileName, WdExportFormat.wdExportFormatPDF) Exit Select End Select pdfFileName.Text = sfd.FileName End If System.Windows.Forms.MessageBox.Show("File Converted Successfully..") Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.Message) End Try System.Diagnostics.Process.Start(sfd.FileName) End Sub
Như vậy đã hoàn thành, chúc mọi người thành công.
Theo LapTrinhVB.Net