- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
- [DATABASE] Tách số và chữ từ chuỗi - hàm tối ưu hóa tách số và chữ trong Sqlserver
- [C#] Tìm kiếm gần đúng Full Text Search sử dụng thư viện Lucene.NET
- [C#] Chia sẻ tài liệu, sdk và source code máy chấm công dòng máy ZKTeco
- [C#] Memory Cache là gì, và sử dụng trong ứng dụng Winform
- [DATABASE] Khóa chính Primary Key trong Sqlserver
[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!