Windows Forms: How to Populate PowerPoint with values in C#

This post shows you How to Populate PowerPoint with values in C#

In some projects, you may work with powerpoint files, such as filling in data from database to PowerPoint template file. You can find this article helpful.

Creating a simple powerpoint template as shows below.

powerpoint template

Next, Create a simple form allows you to enter data, then populate data to powerpoint template file.

c# powerpoint

You need to download PptxTemplater from github website, then copy the PptxTemplater folder you downloaded into your project.

Adding a click event handler to the FillData button allows you to fill data from textbox to powerpoint file.

private void btnFillData_Click(object sender, EventArgs e)
{
    using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "PowerPoint |*.pptx" })
    {
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            FileInfo fileInfo = new FileInfo(ofd.FileName);
            string fileName = $"{fileInfo.Directory}\\{fileInfo.Name.Replace(fileInfo.Extension, "")}_data{fileInfo.Extension}";
            File.Copy(ofd.FileName, fileName);
            Pptx pptx = new Pptx(fileName, FileAccess.ReadWrite);
            int totalSlide = pptx.SlidesCount();
            if (totalSlide > 0)
            {
                PptxSlide slide = pptx.GetSlide(0);//default slide 0
                slide.ReplaceTag("{{fullname}}", txtFullName.Text, PptxSlide.ReplacementType.Global);
                slide.ReplaceTag("{{email}}", txtEmail.Text, PptxSlide.ReplacementType.Global);
                slide.ReplaceTag("{{address}}", txtAddress.Text, PptxSlide.ReplacementType.Global);
                pptx.Close();
                Process.Start(fileName);
            }
        }
    }
}

We will open the powerpoint template file, then copy it to another file and finally fill the data into the fields that we have defined.

Related