I use openxml sdk 2.5 in combination with the power tools by Eric White. I've managed to create dynamic pptx presentations using template files. (In C#)
Unfortunately the thumbnail gets lost during the process.
Is there any way to (re)create the thumbnail of a pptx-file using openxml or power tools?
I successfully wrote some code that changes an existing thumbnail with an image. But when there is is no thumbnail it gives me a System.NullReferenceException. Here is the code:
using OpenXmlPowerTools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml.Packaging;
namespace ConsoleApplication1
{
class AddThumbnail_
{
public static void ReplaceThumbnail(ThumbnailPart thumbnailPart, string newThumbnail)
{
using (
FileStream imgStream = new FileStream(newThumbnail, FileMode.Open, FileAccess.Read))
{
thumbnailPart.FeedData(imgStream);
}
}
static void Main(string[] args)
{
var templatePresentation = "Modified.pptx";
var outputPresentation = "Modified.pptx";
var baPresentation = File.ReadAllBytes(templatePresentation);
var pmlMainPresentation = new PmlDocument("Main.pptx", baPresentation);
OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(pmlMainPresentation);
PresentationDocument document = streamDoc.GetPresentationDocument();
var thumbNailPart = document.ThumbnailPart;
ReplaceThumbnail(thumbNailPart, @"C:\Path\to\image\image.jpg");
document.SaveAs(outputPresentation);
}
}
}
EDIT: I realize this question has been asked before (How to generate thumbnail image for a PPTX file in C#?) and the answer is "enable preview screenshot when saving the presentation" but this would mean I'd have to open every pptx and manually set this flag. I would appreciate a C# solution.
Thank you in advance!