且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 OpenXML/PresentationML/C# 中的 PowerPoint (PPTX) 中插入换行符

更新时间:2023-01-18 17:19:56

Open Office SDK 2.5 的 Open XML Productivity Tool 有这个很棒的反射代码工具,可以获取您拥有的任何 OpenXML 文件的 C# 代码.但尤其是经过大量编辑后,演示文稿中的文本会被拆分为各种 Run 元素,并且 reflect code 不是很紧凑.下面是 ShapeTextBody 示例.

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") )
  )
)

注意,BreakParagraph 的子级,而不是 Run 的子级(就像在 WordprocessingML).

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

请注意,Break 会换行(PowerPoint 中的 Shift-Return),如果您想要一个新段落(PowerPoint 中的返回),则需要一个新的 Paragraph代码>.

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.