NEWS

[DEVEXPRESS] Hướng dẫn chuyển đổi File Word docx sang tập tin PDF sử dụng RichEditDocumentServer

[DEVEXPRESS] Hướng dẫn chuyển đổi File Word docx sang tập tin PDF sử dụng RichEditDocumentServer
Đăng bởi: Thảo Meo - Lượt xem: 3439 13:12:15, 17/05/2021C#   In bài viết

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#:

convert_pdf_csharp

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!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn chuyển đổi File Word docx sang tập tin PDF sử dụng RichEditDocumentServer
Đăng bởi: Thảo Meo - Lượt xem: 3439 13:12:15, 17/05/2021C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.