如何使用c#和OpenXML在PowerPoint演示文稿中调整图像大小

7
抱歉如果这个问题已经有答案了,但我看到很多类似的帖子,但是到目前为止都没有帮助到我。
基本上,我正在构建一个应用程序,它可以使用数据库中获取的文本和图像替换 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. 
                }
            }
        }
    }
}
}

我曾经遇到过类似的问题,也许我的答案在这里能够帮到你。我没有使用PowerPoint的经验,但是它看起来对我来说有点相似,例如Blip等。 - Alexander Derck
谢谢你,亚历山大。这正是我尝试过的方法,但似乎无法获取绘图对象。感谢你的帮助,我会继续尝试并保持希望! - Alan Schofield
1个回答

3
通常在几天内毫无进展后,一旦我发布了问题,我就找到了答案!最终结果非常简单。 我已经有了图片的参考,所以我只需修改那里的转换,最后一段代码现在看起来像这样...
                // find the content 
                ImagePart imagePart = (ImagePart)slide.GetPartById(embedId);
                if (imagePart != null)
                {

                    d.Transform2D transform =  picture.Descendants<d.Transform2D>().First();
                    transform.Extents.Cx = 800000; // replace with correct calcualted values eventually
                    transform.Extents.Cy = 400000; // replace with correct calculated values eventually

                    using (FileStream fileStream = new FileStream(newImagePath, FileMode.Open))
                    {
                        imagePart.FeedData(fileStream);
                        fileStream.Close();
                    }

                    return; // found the photo and replaced it. 
                }

我想我只是看不到树木背后的森林......


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接