- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
Hướng dẫn chuyển đổi file Word sang file PDF hỗ trợ (.doc and .docx)
Hôm nay, mình xin hướng dẫn các bạn 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.
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
Vậy là xong, Chúc các bạn thành công. Thanks for watching!