抱歉如果这个问题已经有答案了,但我看到很多类似的帖子,但是到目前为止都没有帮助到我。
基本上,我正在构建一个应用程序,它可以使用数据库中获取的文本和图像替换 PowerPoint“模板”。大部分已经完成,但是当我替换图像时遇到了问题。模板中插入了许多空白图像,准备替换,它们全都是正方形,因为它们只是占位符。我可以轻松地替换它们,但是新图像会被垂直或水平拉伸,具体取决于我从数据库中提取的图像。它基本上填充了占位符的形状。
我必须承认,我很难理解文档模型,所以也许我正在尝试改变错误的东西,我曾尝试更改blip.parent.shape的“extents”属性,但我只是猜测,并没有得到任何帮助。
以下是代码,可以实现更换图像(原作者amos http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2291)。我不确定在哪里尝试调整大小,是在图像放置之后还是之前...任何帮助都将不胜感激。现在我不担心实际大小,我可以自己计算,只是能够改变图像大小是问题所在。
除了示例代码(可能没有什么用处)外,我还链接到了我的示例项目。样本和文件应全部放置在c:\test中才能正常工作。这只是一些图像、c#项目和program.cs文件,以及一个示例PPTX文件。 下载示例zip文件
基本上,我正在构建一个应用程序,它可以使用数据库中获取的文本和图像替换 PowerPoint“模板”。大部分已经完成,但是当我替换图像时遇到了问题。模板中插入了许多空白图像,准备替换,它们全都是正方形,因为它们只是占位符。我可以轻松地替换它们,但是新图像会被垂直或水平拉伸,具体取决于我从数据库中提取的图像。它基本上填充了占位符的形状。
我必须承认,我很难理解文档模型,所以也许我正在尝试改变错误的东西,我曾尝试更改blip.parent.shape的“extents”属性,但我只是猜测,并没有得到任何帮助。
以下是代码,可以实现更换图像(原作者amos http://atakala.com/browser/Item.aspx?user_id=amos&dict_id=2291)。我不确定在哪里尝试调整大小,是在图像放置之后还是之前...任何帮助都将不胜感激。现在我不担心实际大小,我可以自己计算,只是能够改变图像大小是问题所在。
除了示例代码(可能没有什么用处)外,我还链接到了我的示例项目。样本和文件应全部放置在c:\test中才能正常工作。这只是一些图像、c#项目和program.cs文件,以及一个示例PPTX文件。 下载示例zip文件
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.
}
}
}
}
}
}
Blip
等。 - Alexander Derck