以编程方式更改/添加pptx powerpoint的缩略图.使用Openxml SDK? [英] Programmatically change/add thumbnail of pptx powerpoint. With Openxml sdk?
问题描述
我将openxml sdk 2.5与Eric White的电动工具结合使用.我设法使用模板文件创建了动态的pptx演示文稿. (在C#中)
不幸的是,缩略图在此过程中丢失了.
有什么方法可以使用openxml或电动工具(重新)创建pptx文件的缩略图?
我成功编写了一些代码来更改带有图像的现有缩略图.但是,当没有缩略图时,它会给我一个 System.NullReferenceException .这是代码:
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);
}
}
}
我知道之前曾有人问过这个问题(如何生成C#中PPTX文件的缩略图?),答案是保存演示文稿时启用预览屏幕截图",但这意味着我必须打开每个pptx并手动设置此标志.我将感谢C#解决方案.
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.
提前谢谢!
推荐答案
如果缩略图不存在,则ThumbnailPart
不一定存在于文档中,因此代码中的thumbNailPart
变量将为null.在这种情况下,除了为ThumbnailPart
设置图像外,您还需要添加零件本身.
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.
通常,在使用OpenXml SDK时,您会调用AddPart
方法并传入new ThumbnailPart
,但由于某些原因PresentationDocument上的> AddThumbnailPart
方法,这将创建一个新的ThumbnailPart
. AddThumbnailPart
方法采用内容类型的字符串或
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;
这篇关于以编程方式更改/添加pptx powerpoint的缩略图.使用Openxml SDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!