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

Ala*_*eld 6 c# powerpoint openxml

抱歉,如果这已经得到回答,但我已经看到很多类似的帖子,到目前为止没有任何帮助。

基本上,我正在构建一个应用程序,它采用 PowerPoint“模板”并替换来自数据库的文本和图像。大部分内容已完成,但我在替换图像时正在努力解决问题。该模板插入了大量空白图像,随时可以替换,它们都是方形的,因为它们只是占位符。我可以毫无问题地替换它们,但是根据我从数据库中提取的图像,新图像会垂直或水平拉伸。它基本上是填充占位符形状。

我必须承认我发现很难理解文档模型,所以也许我试图改变错误的东西,我曾试图改变 blip.parent.shape 的“范围”属性,但我在猜测和没有快乐。

以下是将图像换出的代码(归功于原作者 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. 
                }
            }
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Ala*_*eld 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. 
                }
Run Code Online (Sandbox Code Playgroud)

我想我只是只见树木不见森林而已……