NEWS

[C#] Làm việc với Microsoft PowerPoint file template trong Winform

[C#] Làm việc với Microsoft PowerPoint file template trong Winform
Đăng bởi: Thảo Meo - Lượt xem: 7890 10:23:16, 07/07/2020C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn làm việc với Microsoft PowerPoint với C# Winform, thay thế thông tin template mẫu File pptx.

[C#]  Working Template Microsoft PowerPoint Winform

Đầu tiên, mình có cần tạo 1 file template từ powerpoint như hình bên dưới:

template_powerpoint

Và dưới đây là giao diện nhập thông tin phần mềm để chúng ta merge dữ liệu từ winform vào template.

demo_powerpoint_csharp

Sau khi nhập thông tin và chọn hình xong => vào Merge File => chúng ta sẽ được kết quả mong muốn như hình bên dưới:

profile_nguyenthao

Đầu tiên để sử dụng các bạn cần Import thư viện: pptxTemplater và DocumentFormat.OpenXML

Hai file trên các bạn có thể download trong source code project của mình.

Các bạn cần cài đặt Open XML SDK 2.0 for Microsoft Office 

Full source code C# working with PowerPoint:

using PptxTemplater;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PowerPointDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_browser_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                pic_avatar.Image = Image.FromFile(dlg.FileName);
            }
        }

        private void btn_merge_Click(object sender, EventArgs e)
        {
            const string srcFileName = "template.pptx";
            const string dstFileName = "final.pptx";
            File.Delete(dstFileName);
            File.Copy(srcFileName, dstFileName);
            Pptx pptx = new Pptx(dstFileName, FileAccess.ReadWrite);
            int nbSlides = pptx.SlidesCount();
            PptxSlide slide = pptx.GetSlide(0);
            slide.ReplaceTag("{{fullname}}", txt_fullname.Text.ToUpper(), PptxSlide.ReplacementType.Global);
            slide.ReplaceTag("{{dateofbirth}}", txt_dateofbirth.Text.ToUpper(), PptxSlide.ReplacementType.Global);
            slide.ReplaceTag("{{phone}}", txtPhone.Text.ToUpper(), PptxSlide.ReplacementType.Global);
            slide.ReplaceTag("{{skills}}", txtSkill.Text.ToUpper(), PptxSlide.ReplacementType.Global);
            slide.ReplacePicture("{{avatar}}", ImageToByteArray(pic_avatar.Image), "image/png");
            pptx.Close();

            Process.Start("final.pptx");
        }

        public byte[] ImageToByteArray(System.Drawing.Image imageIn)
        {
            using (var ms = new MemoryStream())
            {
                imageIn.Save(ms, imageIn.RawFormat);
                return ms.ToArray();
            }
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Làm việc với Microsoft PowerPoint file template trong Winform
Đăng bởi: Thảo Meo - Lượt xem: 7890 10:23:16, 07/07/2020C#   In bài viết

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

Đọc tiếp
.