- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
- [C#] Thiết kế ứng dụng Console đẹp với thư viện Spectre.Console
- [C#] Thiết kế ứng dụng Single Instance và đưa ứng dụng lên trước nếu kiểm tra ứng dụng đang chạy
- [C#] Giới thiệu JSON Web Token và cách đọc chuỗi token
- [C#] Cách tăng giảm font chữ tất cả các control trên winform
- [DEVEXPRESS] Tích hợp chức năng Tìm kiếm Search vào CheckedComboboxEdit
- [C#] Gởi email Metting Calendar Reminder kèm nhắc thời gian lịch họp
- [C#] Tìm kiếm xem danh sách từ khóa có tồn tại trong đoạn văn bản hay không
- [C#] Thiết kế giao diện ứng dụng trên Console sử dụng thư viện Terminal.Gui
- [C#] Hướng dẫn tạo mã VietQR Payment API Winform
- [C#] Sử dụng thư viện BenchmarkDotNet đo hiệu năng của hảm Method
- [DEVEXPRESS] Tìm kiếm không dấu tô màu highlight có dấu trên C# Winform
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!