- [C#] Giới thiệu Component WebView2 của Microsoft
- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
- [DATABASE] Hướng dẫn sử dụng Hàm ASCII trong sqlserver
- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
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!