none
Adding date in the OpenXML Powerpoint RRS feed

  • Question

  • Hi there, I hope your appreciated help.

    First of all I must say that I am a newbie when it comes to net language.

    This is my problem.

    With OpenXml generate powerpoint presentation but I can not adding `date in the for each individual slide.

    Can you help me? thank you in advance.
    Your help would be very appreciated

    <%@ Page Title="Home page" Language="C#" AutoEventWireup="true" %>
    
    <%@ Import Namespace="DocumentFormat.OpenXml" %>
    <%@ Import Namespace="DocumentFormat.OpenXml.Presentation" %>
    <%@ Import Namespace="DocumentFormat.OpenXml.Packaging" %>
    <%@ Import Namespace="DocumentFormat.OpenXml.Drawing" %>
    
    
        public static void CreatePresentation(string OutputFile)
           {
                PresentationDocument presentationDoc = PresentationDocument
                    .Create(OutputFile, PresentationDocumentType.Presentation);
                PresentationPart presentationPart = presentationDoc
                   .AddPresentationPart();
                
                presentationPart.Presentation = new Presentation();
                CreatePresentationParts(presentationPart);
    
                presentationDoc.Close();
    
            }
    
    
       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";
    
    ...
    
    }
    
    ...
           
           Chart1.DataSource = objCmd.ExecuteReader();
           string OutputFile = @"D:\Inetpub\wwwroot\public\safety.pptx";
           this.Chart1.SaveImage(ChartPNG, ChartImageFormat.Png);
           PPTReplaceImageOnSlide(string OutputFile, "Safety", ChartPNG);

    Thursday, April 5, 2012 5:27 PM

Answers

  • Hi cms9651,

    Thanks for posting in the MSDN Froum.

    I would recommend you clarify your goal. In your snippet I can't find any will to add date in to slide. What do you want to do?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Hi Tom, I'd appreciate your help so very much. 

    I need the date in to slides of my powerpoint presentation.

    thank you

    • Marked as answer by cms9651 Tuesday, July 3, 2012 9:06 AM
    Tuesday, April 17, 2012 9:37 AM

All replies

  • Hi.

    This is the function for replace image in power point slides.

    I need add date in the alls slides.

    Can you help me?

       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;
                       }
                   }
               }
           }
       }
    

    Friday, April 6, 2012 11:52 AM
  • Hi cms9651,

    Thanks for posting in the MSDN Froum.

    I would recommend you clarify your goal. In your snippet I can't find any will to add date in to slide. What do you want to do?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by cms9651 Tuesday, April 10, 2012 7:58 PM
    • Unmarked as answer by cms9651 Tuesday, April 17, 2012 9:35 AM
    Tuesday, April 10, 2012 6:50 AM
    Moderator
  • Hi cms9651,

    Thanks for posting in the MSDN Froum.

    I would recommend you clarify your goal. In your snippet I can't find any will to add date in to slide. What do you want to do?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Hi Tom, I'd appreciate your help so very much. 

    I need the date in to slides of my powerpoint presentation.

    thank you

    • Marked as answer by cms9651 Tuesday, July 3, 2012 9:06 AM
    Tuesday, April 17, 2012 9:37 AM
  • I try this code for add date in powerpoint presentation, but I have error.

    Why?

    Can you help me?

    Compilation Error 
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
    
    Compiler Error Message: CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
    
    Source Error:
    
     
    
    Line 65:             new DocumentFormat.OpenXml.Presentation.NonVisualShapeDrawingProperties();
    Line 66: 
    Line 67:         A.ShapeLocks shapeLocks1 = new A.ShapeLocks() { NoGrouping = true };
    Line 68: 
    Line 69:         nonVisualShapeDrawingProperties1.Append(shapeLocks1);
     
    
    <script runat="server">    
           
        //start
        static void Main(string[] args)
        {
            string fileName = @"D:\Inetpub\wwwroot\public\291211.pptx";
            using (PresentationDocument oPDoc = PresentationDocument.Open(fileName, true))
            {
                PresentationPart oPPart = oPDoc.PresentationPart;
                SlideIdList slideIdList = oPPart.Presentation.SlideIdList;
                SlidePart sp = slideIdList.ChildElements
                   .Cast<SlideId>()
                   .Select(x => oPPart.GetPartById(x.RelationshipId))
                   .Cast<SlidePart>().First();
                AddDateToSlidePart(sp);
            }
        }
    
        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();
    
            A.ShapeLocks shapeLocks1 = new A.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.Presentation.TextBody textBody1 = new DocumentFormat.OpenXml.Presentation.TextBody();
            A.BodyProperties bodyProperties1 = new A.BodyProperties();
            A.ListStyle listStyle1 = new A.ListStyle();
            A.Paragraph paragraph1 = new A.Paragraph();
            A.Field field1 = new A.Field() { Id = "{528B97E8-8E4B-4D32-BA17-4F287283DFD6}", Type = "datetime1" };
            A.RunProperties runProperties1 = new A.RunProperties() { Language = "it-IT", SmartTagClean = false };
            A.Text text1 = new A.Text();
            text1.Text = DateTime.Now.ToString("dd/mm/yyyy");
            field1.Append(runProperties1);
            field1.Append(text1);
    
            A.EndParagraphRunProperties endParagraphRunProperties1 = new A.EndParagraphRunProperties() { Language = "en-US" };
    
            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);
    
        }
               
        //end
    
    </script>

    • Proposed as answer by Wasim J Wednesday, August 27, 2014 3:10 PM
    Wednesday, April 18, 2012 2:51 PM
  • Hello.

    I solved my first problem but the date is adding only in first slide.

    I need adding date in all slides of the powerpoint presentation, can you help me?

       public 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;
    
                SlidePart sp = slideIdList.ChildElements
    
               .Cast<SlideId>()
    
               .Select(x => oPPart.GetPartById(x.RelationshipId))
    
               .Cast<SlidePart>().First();
    
                AddDateToSlidePart(sp);
    
           }
    
       }
    
       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.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() { Language = "it-IT", 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);
    
       }

    Wednesday, April 18, 2012 8:21 PM
  • I try this code for add date in powerpoint presentation, but I have error.

    Why?

    Can you help me?

    Compilation Error 
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
    
    Compiler Error Message: CS0246: The type or namespace name 'A' could not be found (are you missing a using directive or an assembly reference?)
    
    Source Error:
    
     
    
    Line 65:             new DocumentFormat.OpenXml.Presentation.NonVisualShapeDrawingProperties();
    Line 66: 
    Line 67:         A.ShapeLocks shapeLocks1 = new A.ShapeLocks() { NoGrouping = true };
    Line 68: 
    Line 69:         nonVisualShapeDrawingProperties1.Append(shapeLocks1);
     
    
    <script runat="server">    
           
        //start
        static void Main(string[] args)
        {
            string fileName = @"D:\Inetpub\wwwroot\public\291211.pptx";
            using (PresentationDocument oPDoc = PresentationDocument.Open(fileName, true))
            {
                PresentationPart oPPart = oPDoc.PresentationPart;
                SlideIdList slideIdList = oPPart.Presentation.SlideIdList;
                SlidePart sp = slideIdList.ChildElements
                   .Cast<SlideId>()
                   .Select(x => oPPart.GetPartById(x.RelationshipId))
                   .Cast<SlidePart>().First();
                AddDateToSlidePart(sp);
            }
        }
    
        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();
    
            A.ShapeLocks shapeLocks1 = new A.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.Presentation.TextBody textBody1 = new DocumentFormat.OpenXml.Presentation.TextBody();
            A.BodyProperties bodyProperties1 = new A.BodyProperties();
            A.ListStyle listStyle1 = new A.ListStyle();
            A.Paragraph paragraph1 = new A.Paragraph();
            A.Field field1 = new A.Field() { Id = "{528B97E8-8E4B-4D32-BA17-4F287283DFD6}", Type = "datetime1" };
            A.RunProperties runProperties1 = new A.RunProperties() { Language = "it-IT", SmartTagClean = false };
            A.Text text1 = new A.Text();
            text1.Text = DateTime.Now.ToString("dd/mm/yyyy");
            field1.Append(runProperties1);
            field1.Append(text1);
    
            A.EndParagraphRunProperties endParagraphRunProperties1 = new A.EndParagraphRunProperties() { Language = "en-US" };
    
            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);
    
        }
               
        //end
    
    </script>

    Add 

    using A = DocumentFormat.OpenXml.Drawing;

    to get rid of this error.

    Wednesday, August 27, 2014 3:11 PM