Adding date in the OpenXML Powerpoint
-
jeudi 5 avril 2012 17:27
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);
Toutes les réponses
-
vendredi 6 avril 2012 11:52
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; } } } } }
-
mardi 10 avril 2012 06:50Modérateur
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
-
mardi 17 avril 2012 09:37
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
- Marqué comme réponse cms9651 mardi 3 juillet 2012 09:06
-
mercredi 18 avril 2012 14:51
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>
-
mercredi 18 avril 2012 20:21
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); }

