- [C#] Hướng dẫn giải nén file *.rar với tiến trình progress bar winform
- [C#] Chia sẻ source code make Crazy Error Message Effect Bomb Windows
- [C#] Lập trình ứng dụng theo mô hình MVP Model-View-Presenter Pattern Winform
- [C#] Giới thiệu và những thứ hay ho từ thư viện System.Reactive của Microsoft
- [C#] Hướng dẫn tạo ứng dụng Chat với GPT sử dụng Open AI API
- [DEVEXPRESS] Tạo month picker trên DateEdit Winform C#
- [DATABASE] Cách sử dụng và lưu ý khi sử dụng khóa ngoại (Foreign Key) trong Sqlserver
- [C#] Garbage Collector (GC) là gì? Cách giải phóng bộ nhớ trên ứng dụng Winform khi các đối tượng không còn sử dụng
- [C#] Cách tính độ tương phản màu sắc Contrast Color mà con người có thể nhìn thấy được
- [C#] Hướng dẫn mã hóa mật khẩu tài khoản ứng dụng đúng chuẩn Men
- [C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform
- [DATABASE] Chia sẻ dữ liệu Pantone Color sql và json api
- [SQLSERVER] Tạo mã sản phẩm tự động như: SP0001, SP0002, SP0003... sử dụng Trigger
- [C#] Hướng dẫn kiểm tra phiên bản NET Framework cài đặt ở máy tính
- [C#] Hướng dẫn đọc file excel đơn giản sử dụng thư viện Epplus
- [C#] ConcurrentBag là gì và cách sử dụng nó trong lập trình bất đồng bộ
- [C#] AutoResetEvent là gì và cách sử dụng
- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
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!