简体   繁体   English

以编程方式更改/添加pptx powerpoint的缩略图。 使用Openxml SDK?

[英]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. 我将openxml sdk 2.5与Eric White的电动工具结合使用。 I've managed to create dynamic pptx presentations using template files. 我设法使用模板文件创建了动态的pptx演示文稿。 (In C#) Unfortunately the thumbnail gets lost during the process. (在C#中)不幸的是,缩略图在此过程中丢失了。
Is there any way to (re)create the thumbnail of a pptx-file using openxml or power tools? 有什么方法可以使用openxml或电动工具来(重新)创建pptx文件的缩略图?
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 . 但是,当没有缩略图时,它将给我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. 编辑:我意识到之前曾有人问过这个问题( 如何在C#中为PPTX文件生成缩略图? ),答案是“保存演示文稿时启用预览屏幕截图”,但这意味着我必须打开每个pptx并手动设置此标志。 I would appreciate a C# solution. 我将不胜感激C#解决方案。

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. 如果缩略图从不存在,则ThumbnailPart不一定存在于文档中,因此代码中的thumbNailPart变量将为null。 In that scenario, as well as setting the image for the ThumbnailPart you need to add the part itself. 在这种情况下,除了为ThumbnailPart设置图像外,还需要添加部件本身。

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. 通常,在使用OpenXml SDK时,您将调用AddPart方法,以传入new ThumbnailPart但由于某种原因ThumbnailPart构造函数protected internal ,因此您无法访问。 Instead, there is an AddThumbnailPart method on the PresentationDocument which will create a new ThumbnailPart . 而是在PresentationDocument上有一个AddThumbnailPart方法,该方法将创建一个新的ThumbnailPart The AddThumbnailPart method takes either a string for the content type or a ThumbnailPartType enum member. AddThumbnailPart方法采用内容类型的字符串或ThumbnailPartType枚举成员。

Adding the following to your code should fix your issue: 在代码中添加以下内容可以解决您的问题:

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

var thumbNailPart = document.ThumbnailPart;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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