BomKhung.Com Đã Quay Trở Lại
Hoạt Động Dưới Tên Miền Hung.Pro.VN
00 Days
00 Hours
00 Minutes
00 Seconds
Hiện website đang cập nhật nội dung bài viết, nếu có lỗi gì mọi người có thể thông báo cho mình Tại đây!

[DEVEXPRESS] How to MailMerge with Image in Winform C#

Xin chào các bạn, hôm nay mình hướng dẫn các bạn cách sử dụng Mail Merge có kèm hình ảnh trên Devexpress C#, Winform.
Dưới đây, là hình ảnh giao diện demo ứng dụng:
[DEVEXPRESS] How to MailMerge with Image in Winform C#

SOURCE CODE:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DevExpress.Office.Services;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraPrinting;
using DevExpress.XtraPrinting.BarCode;
using System.IO;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using static ListBoxTemplate.Form1;

namespace MailmergeBarcode
{
    public partial class DevexpressMailMerge : Form
    {
        BarCodeStreamProvider provider;
        public Employee _employee;

        public DevexpressMailMerge(Employee employee)
        {
            InitializeComponent();
            this._employee = employee;
        }

        void RegisterBarCodeProvider(RichEditDocumentServer server, List<Employee> data)
        {
            IUriStreamService uriService = (IUriStreamService)server.GetService(typeof(IUriStreamService));
            provider = new BarCodeStreamProvider(data);
            uriService.RegisterProvider(provider);
        }

        private void btnMergeOption_Click(object sender, EventArgs e)
        {
            RichEditDocumentServer server = new RichEditDocumentServer();
            
            string appPath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"));
            string filestr = appPath + @"\certificate.docx";
            server.LoadDocument(filestr, DocumentFormat.Doc);
            var listDaa = new List<Employee>();
            listDaa.Add(_employee);
         
            RegisterBarCodeProvider(server, listDaa);
            server.Options.MailMerge.DataSource = listDaa;
            server.Options.MailMerge.ViewMergedData = true;
            server.Document.Fields.Update();
            MailMergeOptions myMergeOptions = server.Document.CreateMailMergeOptions();
            myMergeOptions.FirstRecordIndex = 0;
            myMergeOptions.LastRecordIndex = 2;
            myMergeOptions.MergeMode = MergeMode.NewSection;
            server.Document.MailMerge(myMergeOptions, richEditControl2.Document);
        }

        private void DevexpressMailMerge_Load(object sender, EventArgs e)
        {
            btnMergeOption_Click(null, null);
        }
    }
}



public class BarCodeStreamProvider : IUriStreamProvider
{
    const string prefix = "\\img://";
    List<Employee> _sampleData;
    public BarCodeStreamProvider(List<Employee> sampleData)
    {
        _sampleData = sampleData;
    }
    #region IUriStreamProvider Members
    public Stream GetStream(string uri)
    {
        if (!uri.StartsWith(prefix))
            return null;
        string value = uri.Substring(prefix.Length);
        var isExisted = _sampleData.Any(x => x.url == value);
        
        if (!isExisted) {
            return null;
        }
        
         
        return ResizeImageFromLocalUri(Application.StartupPath + "\\" + value);
        

    }

    public MemoryStream ResizeImageFromLocalUri(string uri)
    {
       
        Image originalImage = Image.FromFile(uri);    
        int newWidth = 80;
        int newHeight = (int)((float)originalImage.Height / originalImage.Width * newWidth);     
        Bitmap resizedImage = new Bitmap(newWidth, newHeight);
    
        using (Graphics g = Graphics.FromImage(resizedImage))
        {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(originalImage, 0, 0, newWidth, newHeight);
        }
        MemoryStream memoryStream = new MemoryStream();
        resizedImage.Save(memoryStream, ImageFormat.Jpeg);
        
        originalImage.Dispose();
        resizedImage.Dispose();    
        memoryStream.Position = 0;
       
        return memoryStream;
    }
    #endregion
}

Ở file word template khi các bạn muốn merge hình ảnh vào, chúng ta sử dụng: "INCLUDE PICTURE"
Chi tiết mình có hướng dẫn ở Video dưới đây:

DOWNLOAD SOURCE CODE

Đăng nhận xét

Đồng ý sữ dụng cookie
Chúng tôi sử dụng cookie trên trang web này để phân tích lưu lượng truy cập, ghi nhớ tùy chọn của bạn và tối ưu hóa trải nghiệm của bạn.
Xem thêm
Oops!
Có vẻ như kết nối internet của bạn có vấn đề. Vui lòng kết nối lại và duyệt web.
AdBlock Detected!
Chúng tôi phát hiện bạn đang sử dụng plugin chặn quảng cáo trong trình duyệt của mình.
Doanh thu chúng tôi kiếm được từ quảng cáo được sử dụng để quản lý trang web này, chúng tôi yêu cầu bạn đưa trang web của chúng tôi vào danh sách trắng trong plugin chặn quảng cáo của bạn.
Site is Blocked
Sorry! This site is not available in your country.
Kỹ thuật số thế hệ tiếp theo Chào mừng bạn đến với trò chuyện WhatsApp
Xin chào! Chúng tôi có thể giúp gì cho bạn hôm nay?
Nhập vào đây...