How to insert a newline in PowerPoint (PPTX) in OpenXML / PresentationML / C#

对着背影说爱祢 提交于 2021-01-01 04:36:27

问题


While Word and PowerPoint both use OpenXML, newlines are handled differently. In Word/WordprocessingML you can run.Append(new Break()) (see here) to insert a newline. Unfortunately in PresentationML this leads to a nonvalid presentation and with errors when loading it in PowerPoint.

How to add a newline in the TextBody of a PowerPoint Shape?


回答1:


The Open XML Productivity Tool of Open Office SDK 2.5 has this great Reflect Code tool to get C# code of whatever OpenXML file you have. But especially after much editing, the text in a presentation gets splitted in various Run elements and the reflect code isn't very compact. Here's an example of a TextBody of a Shape.

new TextBody(
  new A.BodyProperties(),
  new A.Paragraph(
    new A.Run( new A.Text("first line") ),
    new A.Break(),
    new A.Run( new A.Text("second line") )
  ),
  new A.Paragraph(
    new A.Run( new A.Text("new paragraph") )
  )
)

Note, the Break is child of the Paragraph, not child of a Run (as it is in WordprocessingML).

Note, that the Break makes a newline (Shift-Return in PowerPoint), if you want a new paragraph (Return in PowerPoint), you'll need a new Paragraph.



来源:https://stackoverflow.com/questions/32631028/how-to-insert-a-newline-in-powerpoint-pptx-in-openxml-presentationml-c-sha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!