DEV Community

Broggi Quentin
Broggi Quentin

Posted on • Updated on

Insert images in a PowerPoint template with BroggiSoft.OfficeExport

Here is an example of using BroggiBoft.OfficeExport to insert an image into a PowerPoint template:

First, you need to create a PowerPoint template containing a tag for the image that you want to insert. To do this, you must create a Rectangle shape of the desired size and put a {{Img=YourNameValue}} tag inside. For this in PowerPoint go to Insert > Shapes > Rectangle.
For this example the value of YourNameValue is logo.

PowerPoint template with tags

Next you need to convert the image you want to insert to base64. You can use the Convert.ToBase64String class to convert the image to a base64 string.

Create a dictionary that contains the image data with the key name that matches the tag in the template.

var imageBase64 = Convert.ToBase64String(File.ReadAllBytes("logo.png"));
Dictionary<string, string> data = new Dictionary<string, string>();
data.Add("logo", imageBase64);
data.Add("title", "OfficeExport Example");

Enter fullscreen mode Exit fullscreen mode

Install the nuget package BroggiSoft.OfficeExport from: https://www.nuget.org/packages/BroggiSoft.OfficeExport/

Finally, use the ExportFromDictionary method of the OfficeExport class to replace the tags in the template with the image data.

OfficeExport.ExportFromDictionary("template.docx", "output.docx", data);
Enter fullscreen mode Exit fullscreen mode

The output file "output.pptx" will now contain the image inserted into the Shape Rectangle named "logo" in the template, and the title will have been replaced by its corresponding value (here: OfficeExport Example).

The powerpoint output after replacing the tags

It's important to note that you can use the same steps to insert images into Word and Excel templates.

You can find a test project on GitHub: OfficeExportTest

Oldest comments (0)