简体   繁体   中英

Programmatically change/add thumbnail of pptx powerpoint. With Openxml sdk?

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!

If the thumbnail has never existed then the ThumbnailPart won't necessarily exist in the document and so the thumbNailPart variable in your code will be null. In that scenario, as well as setting the image for the ThumbnailPart you need to add the part itself.

Normally when using the OpenXml SDK you would call the AddPart method passing in a new ThumbnailPart but for some reason the ThumbnailPart constructor is protected internal and thus not accessible to you. Instead, there is an AddThumbnailPart method on the PresentationDocument which will create a new ThumbnailPart . The AddThumbnailPart method takes either a string for the content type or a ThumbnailPartType enum member.

Adding the following to your code should fix your issue:

if (document.ThumbnailPart == null)
    document.AddThumbnailPart(ThumbnailPartType.Jpeg);

var thumbNailPart = document.ThumbnailPart;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM