wordpress hit counter
Re: Replace Image On Slide - .Net - Development Tools - OpenXML Developer

Re: Replace Image On Slide

Development Tools

Discussions about working with Open XML using a wide range of development tools

Replace Image On Slide

  • rated by 0 users
  • This post has 3 Replies |
  • 3 Followers
  • Hi there, I'm trying the following code to Replace Image On Slide, I don't have error but the image not registered in the poerpoint presentation.

    Any suggestions would be greatly appreciated.

    Cheers.

    <%@ Page Title="Home page" Language="C#" AutoEventWireup="true" %>
    
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.Odbc" %>
    <%@ Import Namespace="System.Drawing" %>
    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Import Namespace="System.Linq" %>
    <%@ Import Namespace="System.Configuration" %>
    <%@ Import Namespace="System.Security.Principal" %>
    <%@ Import Namespace="System.Text" %>
    <%@ Import Namespace="DocumentFormat.OpenXml" %>
    <%@ Import Namespace="DocumentFormat.OpenXml.Presentation" %>
    <%@ Import Namespace="DocumentFormat.OpenXml.Packaging" %>
    <%@ Import Namespace="DocumentFormat.OpenXml.Drawing" %>
    
    ...
    
    <script runat="server">    
               
        private void Page_Load(object sender, System.EventArgs e)
        {
               string PNG = @"X:\Open\chart.png";
               string PPT = @"X:\Open\myPPT.pptx";
    
              PPTReplaceImageOnSlide(PPT, "", PNG);
        }
    
       public static void PPTReplaceImageOnSlide(string fileName, string slideTitle, string imagePath)
       {
           const string presentationmlNamespace = "http://schemas.openxmlformats.org/presentationml/2006/main";
           const string drawingmlNamespace = "http://schemas.openxmlformats.org/drawingml/2006/main";
           const string relationshipNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships";
    
           using (PresentationDocument pptPackage = PresentationDocument.Open(fileName, true))
           {
               PresentationPart presentationPart = pptPackage.PresentationPart;
    
               System.Xml.NameTable nt = new System.Xml.NameTable();
               System.Xml.XmlNamespaceManager nsManager = new System.Xml.XmlNamespaceManager(nt);
               nsManager.AddNamespace("p", presentationmlNamespace);
               nsManager.AddNamespace("a", drawingmlNamespace);
               nsManager.AddNamespace("r", relationshipNamespace);
    
               List<SlidePart> slideParts = new List<SlidePart>();
               presentationPart.GetPartsOfType<SlidePart>(slideParts);
    
               foreach (SlidePart slidePart in slideParts)
               {
                   System.Xml.XmlDocument slideDoc = new System.Xml.XmlDocument(nt);
                   slideDoc.Load(slidePart.GetStream());
    
                   System.Xml.XmlNode titleNode = slideDoc.SelectSingleNode("//p:sp//p:ph[@type='title' or @type='ctrTitle']", nsManager);
                   if (titleNode != null)
                   {
                       string titleText = titleNode.ParentNode.ParentNode.ParentNode.InnerText;
    
                       if (string.Compare(titleText, slideTitle, true) == 0)
                       {
                           List<ImagePart> imageParts = new List<ImagePart>();
    
                           slidePart.GetPartsOfType<ImagePart>(imageParts);
                           foreach (ImagePart imagePart in imageParts)
                           {
                               if (imagePart != null)
                               {
                                   string oldRelID = slidePart.GetIdOfPart(imagePart);
                                   string imageFile = System.IO.Path.GetFileName(imagePath);
                                   Uri newImageUri = new Uri("C:/" + imageFile, UriKind.Relative);
                                   ImagePart newImagePart = slidePart.AddImagePart(ImagePartType.Jpeg);
    
                                   using (Stream outputStream = newImagePart.GetStream())
                                   {
                                       using (FileStream inputStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
                                       {
                                           int len = Convert.ToInt32(inputStream.Length);
                                           byte[] bytes = new byte[len];
                                           int bytesRead = inputStream.Read(bytes, 0, len);
                                           if (bytesRead == len)
                                           {
                                               outputStream.Write(bytes, 0, len);
                                           }
                                       }
                                   }
                                   string searchString = string.Format("//p:pic//a:blip[@r:embed='{0}']", oldRelID);
                                   System.Xml.XmlNode relNode = slideDoc.SelectSingleNode(searchString, nsManager);
                                   if (relNode != null)
                                   {
                                       relNode.Attributes["r:embed"].Value = slidePart.GetIdOfPart(newImagePart);
                                   }
                                   slidePart.DeletePart(imagePart);
                                   break;
                               }
                           }
    
                           slideDoc.Save(slidePart.GetStream());
                           break;
                       }
                   }
               }
           }
       }
    
    

  • I ran this code as a stand-alone C# project and it worked great. I'm not sure what you mean by "the image not registered." Perhaps when you look at it in PowerPoint, it is still looking at the old file. I don't know the context for your page load processing or how you are viewing it in PowerPoint. I suggest that you make the change with this code, then shutdown your web pages, then load in PowerPoint, if you haven't done that already.

    If there is still a problem, then I suggest you examine the XML of your presentation to see if the image was actually replaced. If the title didn't match or the image path isn't quite right, then the code won't make a change. If you don't have Visual Studio with the Package Editor Power Tool, then you will need to rename the file extension to .zip and then you can examine the XML files.

    I hope that helps.

  • replace image.rar

    Please see the attached files. I have written a simple method which replaces the image in the slide. Hope that helps :)

  • thank you for your help!

Page 1 of 1 (4 items)