How to: Insert a picture into a word processing document (Open XML SDK)
Last modified: July 27, 2012
Applies to: Office 2013 | Open XML
In this article
Opening an Existing Document for Editing
The XML Representation of the Graphic Object
How the Sample Code Works
Sample Code
This topic shows how to use the classes in the Open XML SDK 2.5 for Office to programmatically add a picture to a word processing document.
The following assembly directives are required to compile the code in this topic.
To open an existing document, instantiate the WordprocessingDocument class as shown in the following using statement. In the same statement, open the word processing file at the specified filepath by using the Open(String, Boolean) method, with the Boolean parameter set to true in order to enable editing the document.
The using statement provides a recommended alternative to the typical .Create, .Save, .Close sequence. It ensures that the Dispose method (internal method that is used by the Open XML SDK to clean up resources) is automatically called when the closing brace is reached. The block that follows the using statement establishes a scope for the object that is created or named in the using statement, in this case wordprocessingDocument. Because the WordprocessingDocument class in the Open XML SDK automatically saves and closes the object as part of its System.IDisposable implementation, and because Dispose is automatically called when you exit the block, you do not have to explicitly call Save and Close─as long as you use using.
The following text from the ISO/IEC 29500 specification introduces the Graphic Object Data element.
This element specifies the reference to a graphic object within the document. This graphic object is provided entirely by the document authors who choose to persist this data within the document.[Note: Depending on the type of graphical object used not every generating application that supports the OOXML framework will have the ability to render the graphical object. end note]© ISO/IEC29500: 2008.The following XML Schema fragment defines the contents of this element
<complexType name="CT_GraphicalObjectData">
<sequence>
<any minOccurs="0" maxOccurs="unbounded" processContents="strict"/>
</sequence>
<attribute name="uri" type="xsd:token"/>
</complexType>
After you have opened the document, add the ImagePart object to the MainDocumentPart object by using a file stream as shown in the following code segment.
To add the image to the body, first define the reference of the image. Then, append the reference to the body. The element should be in a Run.
The following code example adds a picture to an existing word document. In your code, you can call the InsertAPicture method by passing in the path of the word document, and the path of the file that contains the picture. For example, the following call inserts the picture "MyPic.jpg" into the file "Word9.docx," located at the specified paths.
After you run the code, look at the file "Word9.docx" to see the inserted picture.
The following is the complete sample code in both C# and Visual Basic.
|
Contribute to this article Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub. |