Hi there, I hope your appreciated help.
First of all I must say that I am a newbie when it comes to OpenXML.
The platform is C#.
This is my problem.
I need replace the value of DateTime.Now.ToString("dd/mm/yyyy") in my powerpoint presentation generate in OpenXml.
Now if try my net code the new value of DateTime.Now.ToString("dd/mm/yyyy") is superimposed in the old value of DateTime.Now.ToString("dd/mm/yyyy")
If you have link for similar task, please give it me.
Can you help me? thank you in advance.Your help would be very appreciated
Thanks for your time and hints.
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);
Hi,
Would you like to change the date format? If your question is changing the text here in, then look at the following code.
Assuming you have one slide and one shape in the slide,
using (PresentationDocument prstDoc = PresentationDocument.Open("Presentation1.pptx", true))
{
Slide firstSlide = prstDoc.PresentationPart.SlideParts.ElementAt(0).Slide;
Shape firstShape = firstSlide.CommonSlideData.ShapeTree.ChildElements.OfType<Shape>().ElementAt(0);
OD.Paragraph para = firstShape.TextBody.ChildElements.OfType<OD.Paragraph>().ElementAt(0);
OD.Text t = para.ChildElements.OfType<OD.Run>().ElementAt(0).Text;
t.Text = DateTime.Now.ToString("mm/dd/yyyy"); // if you want to change the format you can change here.
prstDoc.PresentationPart.Presentation.Save();
}
I have attached a sample solution. Let me know if you need any help.
Thanks
Hello PRAMODHEGDE
I'd appreciate your help so very much.
But I don't understand your help.
I need replace old date value with new date value in all slides in my powerpoint presentation.
I don't know where to apply your suggestion in my code.
I try your code but the result is the new date in the first slide title...
I hope your help.
My code:
<%@ 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" %> <!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 runat="server"> <title>HoPage</title> <script runat="server"> 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; var splist = slideIdList.ChildElements .Cast<SlideId>() .Select(x => oPPart.GetPartById(x.RelationshipId)) .Cast<SlidePart>(); foreach (SlidePart sp in splist) 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.Drawing.Transform2D transform1 = new DocumentFormat.OpenXml.Drawing.Transform2D(); DocumentFormat.OpenXml.Drawing.Offset offset1 = new DocumentFormat.OpenXml.Drawing.Offset() { X = 763588L, Y = 5906638L }; 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() { 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); } </script> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
as you said, "you need replace old date value with new date value in all slides in your powerpoint presentation", you have 'Date Placeholder 3' in all the slides, you just have to replace the text within this placeholder. Currently you are actually creating a new Shape and trying to add to the slide. Since you already have the Shape, you just have to replace its value with a new value.
string newValue = DateTime.Now.ToString("mm/dd/yyyy");
foreach(SlidePart sPart in prstDoc.PresentationPart.SlideParts)
Slide s = sPart.Slide;
Shape datePlaceholder = s.CommonSlideData.ShapeTree.ChildElements.OfType<Shape>().Where(
s=>s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Date Placeholder 3").FirstOrDefault();
if(datePlaceholder!=null)
datePlaceholder.InsertNewValue(newValue);
..
........
public static class Extensions
public static void InsertNewValue(this Shape shape, string value)
Paragraph para = shape.TextBody.ChildElements.OfType<Paragraph>().ElementAt(0);
Run run = para.ChildElements.OfType<Run>().ElementAt(0);
run.Text.Text = value;
hope you can find this helpful.
I try your code but the result is the new error in my net page.
I hope your help, please. Thanks.
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: CS1109: Extension method must be defined in a top level static class; Extensions is a nested class Source Error: Line 144: public static class Extensions Line 145: { Line 146: public static void InsertNewValue(this DocumentFormat.OpenXml.Presentation.Shape shape, string value) Line 147: { Line 148: Paragraph para = shape.TextBody.ChildElements.OfType<Paragraph>().ElementAt(0); <%@ 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" %> <!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 runat="server"> <title>HoPage</title> <script runat="server"> 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); // START NEW CODE string newValue = DateTime.Now.ToString("mm/dd/yyyy"); foreach (SlidePart sPart in oPDoc.PresentationPart.SlideParts) { Slide s = sPart.Slide; DocumentFormat.OpenXml.Presentation.Shape datePlaceholder = s.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where( s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Date Placeholder 3").FirstOrDefault(); if (datePlaceholder != null) { datePlaceholder.InsertNewValue(newValue); } } } } // 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); //start 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 = 763588L, Y = 5906638L }; //DocumentFormat.OpenXml.Drawing.Extents extents1 = //new DocumentFormat.OpenXml.Drawing.Extents() { Cx = 9070975L, Cy = 2030412L }; 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); //end 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); } // START NEW CODE public static class Extensions { public static void InsertNewValue(this DocumentFormat.OpenXml.Presentation.Shape shape, string value) { Paragraph para = shape.TextBody.ChildElements.OfType<Paragraph>().ElementAt(0); Run run = para.ChildElements.OfType<Run>().ElementAt(0); run.Text.Text = value; } } // END NEW CODE </script> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
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: CS1109: Extension method must be defined in a top level static class; Extensions is a nested class Source Error: Line 144: public static class Extensions Line 145: { Line 146: public static void InsertNewValue(this DocumentFormat.OpenXml.Presentation.Shape shape, string value) Line 147: { Line 148: Paragraph para = shape.TextBody.ChildElements.OfType<Paragraph>().ElementAt(0);
<%@ 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" %> <!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 runat="server"> <title>HoPage</title> <script runat="server"> 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); // START NEW CODE string newValue = DateTime.Now.ToString("mm/dd/yyyy"); foreach (SlidePart sPart in oPDoc.PresentationPart.SlideParts) { Slide s = sPart.Slide; DocumentFormat.OpenXml.Presentation.Shape datePlaceholder = s.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where( s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Date Placeholder 3").FirstOrDefault(); if (datePlaceholder != null) { datePlaceholder.InsertNewValue(newValue); } } } } // 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); //start 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 = 763588L, Y = 5906638L }; //DocumentFormat.OpenXml.Drawing.Extents extents1 = //new DocumentFormat.OpenXml.Drawing.Extents() { Cx = 9070975L, Cy = 2030412L }; 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); //end 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); } // START NEW CODE public static class Extensions { public static void InsertNewValue(this DocumentFormat.OpenXml.Presentation.Shape shape, string value) { Paragraph para = shape.TextBody.ChildElements.OfType<Paragraph>().ElementAt(0); Run run = para.ChildElements.OfType<Run>().ElementAt(0); run.Text.Text = value; } } // END NEW CODE </script> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
I have attached a dll. Please include it in your code (<%@ Import Namespace="DocumentFormat.OpenXml.Extensions" %>) and remove the following code from your page,
// START NEW CODE
public static void InsertNewValue(this DocumentFormat.OpenXml.Presentation.Shape shape, string value)
} // END NEW CODE
Let me know if you any issue.
Thanks alot for your help.
I'm sorry but I have new error in your code in a local variable named 's' .
I hope your appreciated help, please.
<%@ Page Title="Home page" Culture="it-IT" Language="C#" AutoEventWireup="true" %> 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: CS0136: A local variable named 's' cannot be declared in this scope because it would give a different meaning to 's', which is already used in a 'parent or current' scope to denote something else Source Error: Line 48: DocumentFormat.OpenXml.Presentation.Shape datePlaceholder Line 49: = s.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where( Line 50: s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Date Placeholder 3").FirstOrDefault(); Line 51: if (datePlaceholder != null) Line 52: { <%@ 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 runat="server"> <title>HoPage</title> <script runat="server"> 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); // START NEW CODE string newValue = DateTime.Now.ToString("mm/dd/yyyy"); foreach (SlidePart sPart in oPDoc.PresentationPart.SlideParts) { Slide s = sPart.Slide; DocumentFormat.OpenXml.Presentation.Shape datePlaceholder = s.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where( s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Date Placeholder 3").FirstOrDefault(); if (datePlaceholder != null) { datePlaceholder.InsertNewValue(newValue); } } } } // 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>
<%@ Page Title="Home page" Culture="it-IT" Language="C#" AutoEventWireup="true" %> 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: CS0136: A local variable named 's' cannot be declared in this scope because it would give a different meaning to 's', which is already used in a 'parent or current' scope to denote something else Source Error: Line 48: DocumentFormat.OpenXml.Presentation.Shape datePlaceholder Line 49: = s.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where( Line 50: s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Date Placeholder 3").FirstOrDefault(); Line 51: if (datePlaceholder != null) Line 52: { <%@ 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 runat="server"> <title>HoPage</title> <script runat="server"> 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);
// START NEW CODE string newValue = DateTime.Now.ToString("mm/dd/yyyy"); foreach (SlidePart sPart in oPDoc.PresentationPart.SlideParts) { Slide s = sPart.Slide; DocumentFormat.OpenXml.Presentation.Shape datePlaceholder = s.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where( s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Date Placeholder 3").FirstOrDefault(); if (datePlaceholder != null) { datePlaceholder.InsertNewValue(newValue); } } } } // 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>
It is telling you already have a variable s. So you cannot have two variables with same name and of different type.
replace, // START NEW CODE
foreach (SlidePart sPart in oPDoc.PresentationPart.SlideParts)
DocumentFormat.OpenXml.Presentation.Shape datePlaceholder
= s.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where(
s => s.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value == "Date Placeholder 3").FirstOrDefault();
if (datePlaceholder != null)
// END NEW CODE
with
Slide sld = sPart.Slide;
= sld.CommonSlideData.ShapeTree.ChildElements.OfType<DocumentFormat.OpenXml.Presentation.Shape>().Where(
I'm sorry but I have new error in your code in a Specified argument was out of the range of valid values.
Specified argument was out of the range of valid values. Parameter name: index Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index Source Error: Line 52: if (datePlaceholder != null) Line 53: { Line 54: datePlaceholder.InsertNewValue(newValue); Line 55: } Line 56: } <%@ 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 runat="server"> <title>HoPage</title> <script runat="server"> 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); // START NEW CODE string newValue = DateTime.Now.ToString("mm/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 == "Date Placeholder 3").FirstOrDefault(); if (datePlaceholder != null) { datePlaceholder.InsertNewValue(newValue); } } } } // 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>
I have attached a sample solution. Please have a look. It actually replaces the old date with new date in all the slides.
Before:Check the placeholder id's to match,After:
Hi, thanks for your new reply.
With your last suggestion now I don't have error, but the replace of the date not working.
The dates (new and old) are overlapping.
I have attached your solution and the powerpoint presentation.
If you open my ppt1.pptx you can verify that there are two overlapping dates for each slides.
2248.Desktop.zip
I saw the generated pptx file. It is actually creating new shapes and appending to the slide. but all you need is find the shape and replace the text in it.
Can you comment out following code and check?
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);
Hi.
I check your code, this is the debug.
With this code I add the date for each slide in presentation powerpoint.
And I verified that by changing the date e.g.:
DateTime dateTime = new DateTime(DateTime.Now.AddYears(-1).Year, 12, DateTime.Now.Day); string s3 = dateTime.ToString("MMM yyy"); text1.Text = s3; field1.Append(runProperties1); field1.Append(text1);
The dates in slides is always today: 21/04/2012
Because this part of code insert always the date now.
If I try the code without the part of code, I don't have error but I don'have date in the slides.
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);
Because you are not updating the shape text. You are creating another shape, which results in two shapes overlapped on each other, resulting invalid output.
Comment
var splist = slideIdList.ChildElements.Cast<SlideId>().Select(x => oPPart.GetPartById(x.RelationshipId))
and see the output.
You are right, thanks... but how to resolve this problem?
The output are two overlapping dates for each slides. :(