I want to create a PowerPoint presentation based on a template in C#.
I am using the OpenXML 2.0 SDK
.
My first step.
I took my .pptx template, changed it in C# code and copied it in my project.
I was able to create slides based on my templates.
Now i have some placeholders in my template i want to replace.
I found the following method, on the msdn side
void SwapPlaceholderText(SlidePart slidePart, string placeholder, string value)
{
//Find and get all the placeholder text locations.
List<Drawing.Text> textList = slidePart.Slide.Descendants<Drawing.Text>().Where(t => t.Text.Equals(placeholder)).ToList();
//Swap the placeholder text with the text from DB
foreach (Drawing.Text text in textList) text.Text = value;
}
But the size of the list is 0. Does this method call even searching in the right method of my template? The information of the placeholder i am searching for is in the following method of my template.
private void GenerateUserDefinedTagsPart5Content(UserDefinedTagsPart userDefinedTagsPart5)
{
...
Tag tag71 = new Tag(){ Name = "FIELD.CHAPTER.CONTENT", Val = "#header#" };
Tag tag72 = new Tag(){ Name = "FIELD.CHAPTER.VALUE", Val = "#header#" };
... }
What i am doing wrong? and how do i need to modify the placeholder-method?
Best regards!
EDIT : I found out some strange thing. I declared in my PowerPoint template a placeholder called "#header#" and in my program i was searching for "#header#" but after i ran the program once, the "#header#" in the PowerPoint file changed to "header" i don't know why but when i search now for "header" my list size is 1 and the header will be replaced.
But after running my program and want to start the PowerPoint presentation, it says the data is corrupted. Maybe you can see some mistakes:
This is my main method.
Template template = new Template();
template.CreatePackage("templatesource");
PresentationDocument doc = PresentationDocument.Open("templatesource", true);
PowerPoint powerPoint = new PowerPoint(doc, proj);
powerPoint.AddNewSlides();
PowerPoint-class:
public void addSlides()
{
for (int i = 0; i < proj.projects.Capacity; i++ )
{
Console.WriteLine("Slide number: " + i);
AddNewSlide(_document.PresentationPart);
}
_document.PresentationPart.Presentation.Save();
}
private void AddNewSlide(PresentationPart parent)
{
if (_slideTemplate == null) return;
var newSlidePart = parent.AddNewPart<SlidePart>("newSlide" + _slideId);
newSlidePart.FeedData(_slideTemplate.GetStream(FileMode.Open));
newSlidePart.AddPart(_slideTemplate.SlideLayoutPart, _slideTemplate.GetIdOfPart(_slideTemplate.SlideLayoutPart));
SetPlaceholder(newSlidePart, "#header#", "My new header");
newSlidePart.Slide.Save();
SlideIdList listOfSlidesIDs = parent.Presentation.SlideIdList;
uint maxSlideId = 1;
foreach (SlideId slideId in listOfSlidesIDs.ChildElements)
{
if (slideId.Id > maxSlideId) maxSlideId = slideId.Id;
}
SlideId newSlideId = new SlideId { Id = ++maxSlideId, RelationshipId = parent.GetIdOfPart(newSlidePart) };
listOfSlidesIDs.Append(newSlideId);
_slideId++;
}
Thanks for your help :)