【发布时间】:2020-08-12 00:15:43
【问题描述】:
抱歉,如果这个问题已经得到回答,但我已经看到很多类似的帖子,但到目前为止没有任何帮助。
基本上,我正在构建一个应用程序,它采用 PowerPoint“模板”并替换来自数据库的文本和图像。其中大部分已经完成,但是当我替换图像时,我正在努力解决一个问题。该模板插入了许多空白图像,可以替换,它们都是方形的,因为它们只是占位符。我可以毫无问题地替换它们,但新图像会根据我从数据库中提取的图像垂直或水平拉伸。它基本上是填充占位符形状。
我必须承认我很难理解文档模型所以也许我试图改变错误的东西,我试图改变 blip.parent.shape 的“范围”属性但我正在猜测,并没有得到快乐。
以下是用于交换图像的代码(感谢原作者 amos http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2291)。一旦图像到位或之前,我什至不确定在哪里尝试调整大小......任何帮助将不胜感激。我现在不担心实际大小,我可以自己计算,只是能够改变图像大小才是问题。
除了示例代码(可能没那么有用)之外,我还链接到我的示例项目。样本和文件都应该放在 c:\test 中才能工作。这只是几张图片、c# 项目和 program.cs 文件,以及一个示例 PPTX 文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using d = DocumentFormat.OpenXml.Drawing;
using System.IO;
namespace PPTX_Image_Replace_test
{
class Program
{
static void Main(string[] args)
{
TestImageReplace(@"C:\test\TestImageReplaceAndResize.pptx");
}
public static void TestImageReplace(string fileName)
{
// make a copy of the original and edit that
string outputFileName = Path.Combine(Path.GetDirectoryName(fileName), "New_" + Path.GetFileName(fileName));
File.Copy(fileName, outputFileName,true);
using (PresentationDocument document = PresentationDocument.Open(outputFileName, true))
{
ReplaceFirstImageMatchingAltText(document, @"C:\test\Arrow_UP.png", "changeme");
}
}
public static void ReplaceFirstImageMatchingAltText(PresentationDocument presentationDocument, string newImagePath, string alternateTextToFind)
{
OpenXmlElementList slideIds = presentationDocument.PresentationPart.Presentation.SlideIdList.ChildElements;
foreach (SlideId sID in slideIds) // loop thru the SlideIDList
{
string relId = sID.RelationshipId; // get first slide relationship
SlidePart slide = (SlidePart)presentationDocument.PresentationPart.GetPartById(relId); // Get the slide part from the relationship ID.
var pictures = slide.Slide.Descendants<ShapeTree>().First().Descendants<Picture>().ToList();
foreach (var picture in pictures)
{
// get photo desc to see if it matches search text
var nonVisualPictureProperties = picture.Descendants<NonVisualPictureProperties>().FirstOrDefault();
if (nonVisualPictureProperties == null)
continue;
var nonVisualDrawingProperties99 = nonVisualPictureProperties.Descendants<NonVisualDrawingProperties>().FirstOrDefault();
if (nonVisualDrawingProperties99 == null)
continue;
var desc = nonVisualDrawingProperties99.Description;
if (desc == null || desc.Value == null || !desc.Value.Contains(alternateTextToFind))
continue;
BlipFill blipFill = picture.Descendants<BlipFill>().First();
var blip = blipFill.Descendants<DocumentFormat.OpenXml.Drawing.Blip>().First();
string embedId = blip.Embed; // now we need to find the embedded content and update it.
// find the content
ImagePart imagePart = (ImagePart)slide.GetPartById(embedId);
if (imagePart != null)
{
using (FileStream fileStream = new FileStream(newImagePath, FileMode.Open))
{
imagePart.FeedData(fileStream);
fileStream.Close();
}
return; // found the photo and replaced it.
}
}
}
}
}
}
【问题讨论】:
-
我在文字图像方面遇到了类似的问题,也许我的回答 here 会有所帮助。我没有使用 powerpoint 的经验,但使用
Blip等看起来和我有点相似。 -
感谢 Alexander,我曾尝试过这种方法,但我似乎无法找到绘图对象。不过感谢您的关注.. 我会继续努力并希望!
标签: c# powerpoint openxml