Creating a .pptx File Using OpenXML

I want to create a PowerPoint presentation based on a C # template. I am using the OpenXML 2.0 SDK .

My first step.

I took my .pptx template, changed it in C # code, and copied it into my project.

  • I was able to create slides based on my templates.

  • Now I have some placeholders in my template that I want to replace.

I found the following method on 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 even invoke a search using the correct method of my template? The placeholder information I'm looking 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 am I doing wrong? and how do I need to change the placeholder method?

Yours faithfully!

EDIT: I found out some weird thing. I declared the placeholder "# header #" in my PowerPoint template, and in my program I searched for "# header #", but after I ran the program once, "# header #" in the PowerPoint file was changed to "header" I don’t know why, but when I search now for the "title", my list size is 1 and the title will be replaced.

But after starting my program and starting the PowerPoint presentation, she says that the data is corrupted. Perhaps you can see some errors:

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

Thank you for your help:)

+6
source share

Source: https://habr.com/ru/post/956852/


All Articles