- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
[C#] Làm việc với Microsoft PowerPoint file template trong Winform
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:
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.
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:
Đầ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!