wordpress hit counter
Re: [OpenXml SDK] Replacement old date value with new date value in slide powerpoint - Open XML SDK 2.0 - Development Tools - OpenXML Developer

Re: [OpenXml SDK] Replacement old date value with new date value in slide powerpoint

Development Tools

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

[OpenXml SDK] Replacement old date value with new date value in slide powerpoint

  • rated by 0 users
  • This post has 18 Replies |
  • 2 Followers
  • Please comment out the following lines from your code. These lines are used to add a new shape, instead of replacing the text within an existing shape. So either remove these lines, or comment them.

       SlideIdList slideIdList = oPPart.Presentation.SlideIdList;
       var splist = slideIdList.ChildElements.Cast<SlideId>().Select(x => oPPart.GetPartById(x.RelationshipId))
                                  .Cast<SlidePart>();

        foreach (SlidePart sp in splist)
        AddDateToSlidePart(sp);

  • Ok Sir, I follow your suggestion. Many thanks for your reply.

    I add the date in my powerpoint presentation with this code part  #1 and I have for each slide `21/04/2012` with the public static void AddDateToSlidePart(SlidePart slidePart1):

     

    // START NEW CODE
        private void Page_Load(Object sender, EventArgs e)
        {
            string fileName = @"C:\Inetpub\wwwroot\pptx\ppt1.pptx";
            using (PresentationDocument oPDoc = PresentationDocument.Open(fileName, true))
            {            
                PresentationPart oPPart = oPDoc.PresentationPart;
                
                SlideIdList slideIdList = oPPart.Presentation.SlideIdList;
                var splist = slideIdList.ChildElements.Cast<SlideId>().Select(x => oPPart.GetPartById(x.RelationshipId)).Cast<SlidePart>();                
                foreach (SlidePart sp in splist)
                AddDateToSlidePart(sp);      
    // END NEW CODE
    
            }
        }

     

    After I remove the code part  #1 and for update the date value `21/04/2012` to `aprile 12, 2012`  in each slide I use the code part #2:

     

    // START NEW CODE
        private void Page_Load(Object sender, EventArgs e)
        {
            string fileName = @"C:\Inetpub\wwwroot\pptx\ppt1.pptx";
            using (PresentationDocument oPDoc = PresentationDocument.Open(fileName, true))
            {            
                PresentationPart oPPart = oPDoc.PresentationPart;
                
                    string newValue = DateTime.Now.ToString("MMMM dd, yyyy");             
                    foreach (SlidePart sPart in oPDoc.PresentationPart.SlideParts)
                    {
                        Slide sld = sPart.Slide;
                        DocumentFormat.OpenXml.Presentation.Shape datePlaceholder
                            = sld.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where(
                        s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Content Placeholder 3").FirstOrDefault();
                        if (datePlaceholder != null)
                        {
                            datePlaceholder.InsertNewValue(newValue);
                        }
                    }
                    oPDoc.PresentationPart.Presentation.Save();     
    // END NEW CODE
    
            }
        } 

     

    With the code part #2 I don't have error but in each slide the date not change in `aprile 12, 2012`.

    I hope your help.

     

    <%@ Page Title="Home page" Culture="it-IT" Language="C#" AutoEventWireup="true" %>
    
    <%@ Import Namespace="System" %>
    <%@ Import Namespace="System.Globalization" %>
    <%@ 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" %>
    <%@ Import Namespace="DocumentFormat.OpenXml.Extensions" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>HoPage</title>
    
    <script runat="server">
    
    // START NEW CODE
        private void Page_Load(Object sender, EventArgs e)
        {
            string fileName = @"C:\Inetpub\wwwroot\pptx\ppt1.pptx";
            using (PresentationDocument oPDoc = PresentationDocument.Open(fileName, true))
            {            
                PresentationPart oPPart = oPDoc.PresentationPart;
                
                    string newValue = DateTime.Now.ToString("MMMM dd, yyyy");             
                    foreach (SlidePart sPart in oPDoc.PresentationPart.SlideParts)
                    {
                        Slide sld = sPart.Slide;
                        DocumentFormat.OpenXml.Presentation.Shape datePlaceholder
                            = sld.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where(
                        s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Content Placeholder 3").FirstOrDefault();
                        if (datePlaceholder != null)
                        {
                            datePlaceholder.InsertNewValue(newValue);
                        }
                    }
                    oPDoc.PresentationPart.Presentation.Save();     
    // END NEW CODE
    
            }
        }  
    
        public static void AddDateToSlidePart(SlidePart slidePart1)
        {
    
            Slide slide1 = slidePart1.Slide;
            CommonSlideData commonSlideData1 = slide1.GetFirstChild<CommonSlideData>();
            ShapeTree shapeTree1 = commonSlideData1.GetFirstChild<ShapeTree>();
    
            DocumentFormat.OpenXml.Presentation.Shape shape1 =
                new DocumentFormat.OpenXml.Presentation.Shape();
            DocumentFormat.OpenXml.Presentation.NonVisualShapeProperties nonVisualShapeProperties1 =
                new DocumentFormat.OpenXml.Presentation.NonVisualShapeProperties();
            DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties nonVisualDrawingProperties1 =
                new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Date Placeholder 3" };
            DocumentFormat.OpenXml.Presentation.NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 =
                new DocumentFormat.OpenXml.Presentation.NonVisualShapeDrawingProperties();
    
            DocumentFormat.OpenXml.Drawing.ShapeLocks shapeLocks1 =
                new DocumentFormat.OpenXml.Drawing.ShapeLocks() { NoGrouping = true };
    
            nonVisualShapeDrawingProperties1.Append(shapeLocks1);
            ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 = new ApplicationNonVisualDrawingProperties();
            PlaceholderShape placeholderShape1 = new PlaceholderShape() { Type = PlaceholderValues.DateAndTime, Size = PlaceholderSizeValues.Half, Index = (UInt32Value)10U };
    
            applicationNonVisualDrawingProperties1.Append(placeholderShape1);
            nonVisualShapeProperties1.Append(nonVisualDrawingProperties1);
            nonVisualShapeProperties1.Append(nonVisualShapeDrawingProperties1);
            nonVisualShapeProperties1.Append(applicationNonVisualDrawingProperties1);
    
            DocumentFormat.OpenXml.Presentation.ShapeProperties shapeProperties1 =
                 new DocumentFormat.OpenXml.Presentation.ShapeProperties();
            DocumentFormat.OpenXml.Drawing.Transform2D transform1 =
                new DocumentFormat.OpenXml.Drawing.Transform2D();
            DocumentFormat.OpenXml.Drawing.Offset offset1 =
                new DocumentFormat.OpenXml.Drawing.Offset() { X = 1453588L, Y = 5346638L };
            DocumentFormat.OpenXml.Drawing.Extents extents1 =
                new DocumentFormat.OpenXml.Drawing.Extents() { Cx = 9070975L, Cy = 2030412L };
    
            transform1.Append(offset1);
            transform1.Append(extents1);
            shapeProperties1.Append(transform1);
    
            DocumentFormat.OpenXml.Presentation.TextBody textBody1 =
                new DocumentFormat.OpenXml.Presentation.TextBody();
            DocumentFormat.OpenXml.Drawing.BodyProperties bodyProperties1 =
                new DocumentFormat.OpenXml.Drawing.BodyProperties();
            DocumentFormat.OpenXml.Drawing.ListStyle listStyle1 =
                new DocumentFormat.OpenXml.Drawing.ListStyle();
            DocumentFormat.OpenXml.Drawing.Paragraph paragraph1 =
                new DocumentFormat.OpenXml.Drawing.Paragraph();
            DocumentFormat.OpenXml.Drawing.Field field1 =
                new DocumentFormat.OpenXml.Drawing.Field() { Id = "{528B97E8-8E4B-4D32-BA17-4F287283DFD6}", Type = "datetime1" };
            DocumentFormat.OpenXml.Drawing.RunProperties runProperties1 =
                new DocumentFormat.OpenXml.Drawing.RunProperties() { FontSize = 1100, Language = "it-IT", Dirty = false, SmartTagClean = false };
            DocumentFormat.OpenXml.Drawing.Text text1 =
                new DocumentFormat.OpenXml.Drawing.Text();
    
            text1.Text = DateTime.Now.ToString("dd/MM/yyyy");
            field1.Append(runProperties1);
            field1.Append(text1);
    
            DocumentFormat.OpenXml.Drawing.EndParagraphRunProperties endParagraphRunProperties1 =
                new DocumentFormat.OpenXml.Drawing.EndParagraphRunProperties() { Language = "it-IT" };
    
            paragraph1.Append(field1);
            paragraph1.Append(endParagraphRunProperties1);
            textBody1.Append(bodyProperties1);
            textBody1.Append(listStyle1);
            textBody1.Append(paragraph1);
    
            shape1.Append(nonVisualShapeProperties1);
            shape1.Append(shapeProperties1);
            shape1.Append(textBody1);
            shapeTree1.Append(shape1);
    
        }
        
    </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>
             
  • ReplaceTextInParagraph.rar

    Hi,

       I figured out the problem.  Let me explain you with few snapshots.

    1. Multiple 'Date Placeholders'. Look at this slide.xml file. You have 2 'Date Placeholder 3' shapes in the slide. Though it does not give any error, it is not a good practice to have multiple object with same Id. 




    2. In the code, you are referring the wrong placeholder. Instead of 'Date Placeholder 3', you are checking for 'Content Placeholder 3'. I am actually referring the pptx file which you have attached in one of the earlier threads.


    3. I have changed this code.
                 using (PresentationDocument prstDoc = PresentationDocument.Open("ppt1.pptx", true))
                  {
                      string newValue = DateTime.Now.ToString("MMMM dd, yyyy");
                      foreach (SlidePart sPart in prstDoc.PresentationPart.SlideParts)
                      {
                          Slide sld = sPart.Slide;
                          var datePlaceholders = sld.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().
                                                 Where(s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Date Placeholder 3");
                          if (datePlaceholders != null)
                          {
                              datePlaceholders.ToList().ForEach(datePlaceholder=>
                              {
                                  datePlaceholder.InsertNewValue(newValue);
                              });                        
                          }
                      }
                      prstDoc.PresentationPart.Presentation.Save();
                  } 
      Since, you had multiple shapes with same Id/Name, I modified the code to handle such a situation. Also, please note that "...Name.Value == "Content Placeholder 3");.." is changed to "...Name.Value == "Date Placeholder 3");.."
    4. I actually removed one 'Date Placeholder 3' shape, from each of the slides (i explained the reason before.).






      I ran the code. The problem did not get solved. It was actually replacing the text. But there was something else, which was changing it to a default format (dd/mm/yyyy).


      It was the Type property of Field object. Observe the 'type' attribute in both above and below snaps...
      I have changed  type="datetime1" to type="".




      After these changes, look at the output.



      I have attached the modified code. Also, I modified DocumentFormat.OpenXml.Extensions library (i have attached this also).
      Please do look at the latest solution and use it.  Let me know if you still face any problem.


      Thanks 
  • Thanks you very much for your help.

    I'm really happy for your quickly answer.

    Good bye

Page 2 of 2 (19 items) 12