Hi there, I hope your appreciated help.
First of all I must say that I am a newbie when it comes to net language.
The platform is C#.
This is my problem.
With OpenXml generate powerpoint presentation but I can not adding `date of generation` in the for each individual slide.
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.
Hi,
Below is the source code for date addition on a slide.
----------------------------------------
class Program
{
static void Main(string[] args)
string fileName = @"D:\openxml\ForumPostSample\Nodate.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>();
Shape shape1 = new Shape();
NonVisualShapeProperties nonVisualShapeProperties1 = new NonVisualShapeProperties();
NonVisualDrawingProperties nonVisualDrawingProperties1 = new NonVisualDrawingProperties() { Id = (UInt32Value)4U, Name = "Date Placeholder 3" };
NonVisualShapeDrawingProperties nonVisualShapeDrawingProperties1 = new 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);
ShapeProperties shapeProperties1 = new ShapeProperties();
TextBody textBody1 = new 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 = "en-US", 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);
Hello pranay, I'd appreciate your help so very much.
I have problems with your code because I have error.
I hope your help.
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 66: new DocumentFormat.OpenXml.Presentation.NonVisualShapeDrawingProperties();
Line 67:
Line 68: A.ShapeLocks shapeLocks1 = new A.ShapeLocks() { NoGrouping = true };
Line 69:
Line 70: nonVisualShapeDrawingProperties1.Append(shapeLocks1);
string fileName = @"D:\Inetpub\wwwroot\MAC\Reportistica\public\REPORT_CMP_291211.pptx";
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.Presentation.ShapeProperties shapeProperties1 =
new DocumentFormat.OpenXml.Presentation.ShapeProperties();
DocumentFormat.OpenXml.Presentation.TextBody textBody1 = new DocumentFormat.OpenXml.Presentation.TextBody();
Hello, I solved my first problem but the date is adding only in first slide: I need adding date in all slides of the presentation, can you help me?
public void Page_Load(Object sender, EventArgs e)
string fileName = @"C:\Inetpub\wwwroot\pptx\ppt1.pptx";
DocumentFormat.OpenXml.Drawing.ShapeLocks shapeLocks1 =
new DocumentFormat.OpenXml.Drawing.ShapeLocks() { NoGrouping = true };
ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties1 =
new ApplicationNonVisualDrawingProperties();
PlaceholderShape placeholderShape1 =
new PlaceholderShape() { Type = PlaceholderValues.DateAndTime, Size = PlaceholderSizeValues.Half, Index = (UInt32Value)10U };
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();
DocumentFormat.OpenXml.Drawing.EndParagraphRunProperties endParagraphRunProperties1 =
new DocumentFormat.OpenXml.Drawing.EndParagraphRunProperties() { Language = "it-IT" };
For Adding Date to all slide you need to loop through all the slides in your pptx.
And get the slide part and then call AddDateToSlidePart(sp) for all of them.
Looping code is as below:
-----------------------------
var splist = slideIdList.ChildElements
.Cast<SlidePart>();
foreach(SlidePart sp in splist)
Now the add date in all slides working, but I have two problems in the presentation powerpoint:
I share the output, can you help me?
Can u share your code as well..
I just want to see what's going wrong at your end..
Becoz here its working perfectly fine..
Pranay
Thanks you very much for your help
This is my net 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.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>
Also share your C:\Inetpub\wwwroot\pptx\ppt1.pptx template.
Ok, many thanks.
8877.ppt1.pptx
Hi ,
your problem is fixed now.
use below code for function: AddDateToSlidePart
------------------------
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);
Red lines of code are added.
Just keep one thing in mind. That if your template have more than one shapes in in.. then you need to add the exact location of your added text (date).
And thats what I did here.For changing position of your date change x,y Cx and Cy positions.
Hope it will work at you end.
Thanks you very much for your help, I'm really happy for your quickly answer.
I have three questions for you:
I hope your appreciated help.
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 };
Download and use Open XML Productivity tool from Microsoft site and also use Vs2010 power tool for open xml.
U will get all your answers and will be able to twice all these kind of small stuffs.
Pranay...
Thanks alot, it helped me :)
I'm sorry my friend...
but I download and install Open XML SDK 2.0 for Microsoft Office www.microsoft.com/.../details.aspx
and this: http://visualstudiogallery.msdn.microsoft.com/450a00e3-5a7d-4776-be2c-8aa8cec2a75b
and this: http://visualstudiogallery.msdn.microsoft.com/8e602a8c-6714-4549-9e95-f3700344b0d9
but I don't find answer of my questions:
How to use your suggestions in visual studio 2010?