- [C#] Dependency Injection in Winform
- [SQLSERVER] Hướng dẫn tìm kiếm nâng cao trên sql
- [C#] Hướng dẫn sử dụng SetTimeOut trên Winform like Javascript
- [DATABASE] In cây thông noel bằng sqlserver
- [C#] Hướng dẫn fix lỗi hiển thị UTF-8 khi sử dụng WebClient Download String
- [DATABASE] Hướng dẫn mã hóa và giải mã sử dụng thuật toán AES 256 trên sqlserver
- [DATABASE] Base64 Encode and Decode trong Sqlserver
- [C#] Vì Mẹ anh bắt phải Fake địa chỉ MacAddress
- [C#] Hướng dẫn xuất dữ liệu từ DataGridview ra file Excel
- [C#] Hướng dẫn khởi động lại chương trình ứng dụng winform
- [C#] Sự khác nhau giữa String.IsNullOrEmpty và String.IsNullOrWhiteSpace
- [C#] Hướng dẫn đọc file hình ảnh định dạng WEBP và chuyển đổi WebP sang JPG
- [C#] Kiểm tra phiên bản Microsoft Office đang sử dụng trên máy tính
- [C#] Hướng dẫn chuyển đổi tập tin hình ảnh XPS sang Bitmap
- [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
[DEVEXPRESS] Hướng dẫn chuyển đổi File Word docx sang tập tin PDF sử dụng RichEditDocumentServer
Xin chào các bạn, bài viết hôm nay mình sẻ hướng dẫn các bạn cách chuyển đổi File Word sang File PDF sử dụng RichEditDocumentServer
trong Devexpress Winform c#.
[DEVEXPRESS] Convert a Docx to PDF File RichEditDocumentServer
Nếu bạn nào đang làm việc với Devexpress, bạn muốn chuyển đổi tập tin Word Sang file PDF các bạn chỉ cần sử dụng hàm ConvertDocx2Pdf()
như dưới đây:
Giao diện demo ứng dụng chuyển đổi file PDF C#:
Các bạn chỉ cần chọn tập tin file Word vào bấm vào nút Convert
.
Full source code C#:
using DevExpress.XtraRichEdit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Word2PDF
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
}
private void btnconvert_Click(object sender, EventArgs e)
{
ConvertDocx2Pdf();
}
private void ConvertDocx2Pdf()
{
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
try
{
wordProcessor.LoadDocument(txtInput.Text);
wordProcessor.ExportToPdf("result.pdf");
Process.Start("result.pdf");
}
catch (OperationCanceledException ex)
{
Console.WriteLine(ex.Message);
}
}
}
private void btnBrowse_Click(object sender, EventArgs e)
{
var dlg = new OpenFileDialog();
dlg.Filter = "Word files (*.docx)|*.docx|All files (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
txtInput.Text = dlg.FileName;
}
}
}
}
Nếu bạn nào đang sử dụng Devexpress phiên bản 20.2
trở lên có thể sử dụng code bên dưới để xử lý bất đồng bộ Async
private async void ConvertDocx2PdfWithCancellation()
{
//chờ 10 giây
using (CancellationTokenSource source = new CancellationTokenSource(10000))
{
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
try
{
await wordProcessor.LoadDocumentAsync("Document.docx", source.Token);
await wordProcessor.ExportToPdfAsync("result.pdf", source.Token);
}
catch (OperationCanceledException)
{
}
}
}
}
Thanks for watching!