Skip to content

Latest commit

 

History

History
134 lines (100 loc) · 5.25 KB

how-to-move-a-paragraph-from-one-presentation-to-another.md

File metadata and controls

134 lines (100 loc) · 5.25 KB
api_name api_type ms.assetid title ms.suite ms.author author ms.topic ms.date ms.localizationpriority
Microsoft.Office.DocumentFormat.OpenXML.Packaging
schema
ef817bef-27cd-4c2a-acf3-b7bba17e6e1e
How to: Move a paragraph from one presentation to another
office
o365devx
o365devx
conceptual
12/02/2024
medium

Move a paragraph from one presentation to another

This topic shows how to use the classes in the Open XML SDK for Office to move a paragraph from one presentation to another presentation programmatically.

Getting a PresentationDocument Object

In the Open XML SDK, the xref:DocumentFormat.OpenXml.Packaging.PresentationDocument class represents a presentation document package. To work with a presentation document, first create an instance of the PresentationDocument class, and then work with that instance. To create the class instance from the document call the xref:DocumentFormat.OpenXml.Packaging.PresentationDocument.Open*#documentformat-openxml-packaging-presentationdocument-open(system-string-system-boolean) method that uses a file path, and a Boolean value as the second parameter to specify whether a document is editable. To open a document for read/write, specify the value true for this parameter as shown in the following using statement. In this code, the sourceFile parameter is a string that represents the path for the file from which you want to open the document.

[!code-csharp]

[!code-vb]


[!includeUsing Statement] sourceDoc.

[!includeStructure]

Structure of the Shape Text Body

The following text from the [!includeISO/IEC 29500 URL] specification introduces the structure of this element.

This element specifies the existence of text to be contained within the corresponding shape. All visible text and visible text related properties are contained within this element. There can be multiple paragraphs and within paragraphs multiple runs of text.

© [!includeISO/IEC 29500 version]

The following table lists the child elements of the shape text body and the description of each.

Child Element Description
bodyPr Body Properties
lstStyle Text List Styles
p Text Paragraphs

The following XML Schema fragment defines the contents of this element:

    <complexType name="CT_TextBody">
       <sequence>
           <element name="bodyPr" type="CT_TextBodyProperties" minOccurs="1" maxOccurs="1"/>
           <element name="lstStyle" type="CT_TextListStyle" minOccurs="0" maxOccurs="1"/>
           <element name="p" type="CT_TextParagraph" minOccurs="1" maxOccurs="unbounded"/>
       </sequence>
    </complexType>

How the Sample Code Works

The code in this topic consists of two methods, MoveParagraphToPresentation and GetFirstSlide. The first method takes two string parameters: one that represents the source file, which contains the paragraph to move, and one that represents the target file, to which the paragraph is moved. The method opens both presentation files and then calls the GetFirstSlide method to get the first slide in each file. It then gets the first TextBody shape in each slide and the first paragraph in the source shape. It performs a deep clone of the source paragraph, copying not only the source Paragraph object itself, but also everything contained in that object, including its text. It then inserts the cloned paragraph in the target file and removes the source paragraph from the source file, replacing it with a placeholder paragraph. Finally, it saves the modified slides in both presentations.

[!code-csharp]

[!code-vb]


The GetFirstSlide method takes the PresentationDocument object passed in, gets its presentation part, and then gets the ID of the first slide in its slide list. It then gets the relationship ID of the slide, gets the slide part from the relationship ID, and returns the slide part to the calling method.

[!code-csharp]

[!code-vb]


Sample Code

The following is the complete sample code in both C# and Visual Basic.

[!code-csharp]

[!code-vb]


See also