Open Xml How To Initialize Documentformat.openxml.drawing.inline

10 min read Oct 06, 2024
Open Xml How To Initialize Documentformat.openxml.drawing.inline

The DocumentFormat.OpenXml.Drawing.Inline class is an integral part of working with embedded images within Open XML documents, particularly in scenarios where you're dealing with Word (.docx) files. This class represents an inline image, which is an image positioned within the flow of text.

Understanding how to initialize and work with the DocumentFormat.OpenXml.Drawing.Inline class is crucial for manipulating and creating images embedded in your Open XML documents. This guide will walk you through the process of initializing and using this class effectively.

Understanding Inline Images in Open XML

Before delving into the initialization process, let's first clarify what inline images are in the Open XML context.

Inline images, as opposed to floating images, are treated as part of the document's text flow. Think of them as regular characters or symbols within your text. This means they're positioned within the normal flow of your document and will resize and move alongside the surrounding text.

Initializing DocumentFormat.OpenXml.Drawing.Inline

To use the DocumentFormat.OpenXml.Drawing.Inline class, you need to create an instance of it and set its various properties. Here's a breakdown of the essential steps:

1. Create a New Instance of Inline:

DocumentFormat.OpenXml.Drawing.Inline inline = new DocumentFormat.OpenXml.Drawing.Inline(); 

2. Set the Extent of the Image:

The Extent property specifies the image's width and height within the document. Use the Extent class to define these dimensions:

DocumentFormat.OpenXml.Drawing.Extent extent = new DocumentFormat.OpenXml.Drawing.Extent() { Cx = 914400, Cy = 567000 }; // Example dimensions
inline.Extent = extent; 

Note: Cx and Cy represent the width and height respectively, expressed in EMUs (English Metric Units). 1 EMU equals 1/914400 of an inch.

3. Add a Graphic Element:

The Graphic element holds information about the embedded image, including its source and the image properties:

DocumentFormat.OpenXml.Drawing.Graphic graphic = new DocumentFormat.OpenXml.Drawing.Graphic();
inline.Graphic = graphic;

4. Set the GraphicData Element:

Inside the Graphic element, the GraphicData element contains the actual image data:

DocumentFormat.OpenXml.Drawing.GraphicData graphicData = new DocumentFormat.OpenXml.Drawing.GraphicData();
graphic.GraphicData = graphicData; 

5. Specify the Image Type:

The Uri attribute of the GraphicData element determines the image type (e.g., PNG, JPEG, GIF). You need to specify the appropriate URI based on the image format:

graphicData.Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"; // For PNG, JPEG, or GIF images

6. Provide Image Properties:

Within the GraphicData element, use a Picture element to provide details about the image:

DocumentFormat.OpenXml.Drawing.Picture picture = new DocumentFormat.OpenXml.Drawing.Picture();
graphicData.Append(picture);

// Optional: Set the image non-volatile (for images from a file)
DocumentFormat.OpenXml.Drawing.NonVisualPictureProperties nonVisualPictureProperties = new DocumentFormat.OpenXml.Drawing.NonVisualPictureProperties();
DocumentFormat.OpenXml.Drawing.NonVisualDrawingProperties nonVisualDrawingProperties = new DocumentFormat.OpenXml.Drawing.NonVisualDrawingProperties() { Id = (uint)1, Name = "Picture 1" }; // Example ID and name
nonVisualPictureProperties.Append(nonVisualDrawingProperties);
DocumentFormat.OpenXml.Drawing.NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties = new DocumentFormat.OpenXml.Drawing.NonVisualPictureDrawingProperties();
nonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
picture.Append(nonVisualPictureProperties);

DocumentFormat.OpenXml.Drawing.BlipFill blipFill = new DocumentFormat.OpenXml.Drawing.BlipFill();
DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip() { Embed = "rId1" }; // Replace "rId1" with the actual relationship ID of the image
blipFill.Append(blip);
DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
blipFill.Append(stretch);
picture.Append(blipFill); 

DocumentFormat.OpenXml.Drawing.ShapeProperties shapeProperties = new DocumentFormat.OpenXml.Drawing.ShapeProperties();
DocumentFormat.OpenXml.Drawing.Transform2D transform2D = new DocumentFormat.OpenXml.Drawing.Transform2D();
DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset() { X = 0, Y = 0 };
transform2D.Append(offset);
DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents() { Cx = 914400, Cy = 567000 }; // Example dimensions
transform2D.Append(extents);
shapeProperties.Append(transform2D);
picture.Append(shapeProperties);

7. Add the Inline to a Drawing Element:

Finally, the inline element needs to be added to a Drawing element within your Open XML document's structure:

DocumentFormat.OpenXml.Drawing.Drawing drawing = new DocumentFormat.OpenXml.Drawing.Drawing();
drawing.Append(inline);
// ... (Place the 'drawing' element at the appropriate location in your document)

Example: Embedding an Image in a Word Document

Here's a complete example of embedding an image within a Word document using the DocumentFormat.OpenXml.Drawing.Inline class:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;

namespace OpenXmlInlineImageExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new Word document
            string documentPath = "MyWordDocument.docx";
            WordprocessingDocument wordDocument = WordprocessingDocument.Create(documentPath, WordprocessingDocumentType.Document);

            // Create a new main document part
            MainDocumentPart mainDocumentPart = wordDocument.AddMainDocumentPart();

            // Create a body for the document
            Body body = new Body();
            mainDocumentPart.Document = new Document(body);

            // Add a paragraph with some text
            Paragraph paragraph = new Paragraph(new Run(new Text("This is some text with an embedded image.")));
            body.Append(paragraph);

            // Add a new drawing element
            Drawing drawing = new Drawing();
            paragraph.Append(drawing);

            // Create an inline element
            DocumentFormat.OpenXml.Drawing.Inline inline = new DocumentFormat.OpenXml.Drawing.Inline();
            drawing.Append(inline);

            // Set the extent of the image
            DocumentFormat.OpenXml.Drawing.Extent extent = new DocumentFormat.OpenXml.Drawing.Extent() { Cx = 914400, Cy = 567000 }; // Example dimensions
            inline.Extent = extent;

            // Add a graphic element
            DocumentFormat.OpenXml.Drawing.Graphic graphic = new DocumentFormat.OpenXml.Drawing.Graphic();
            inline.Graphic = graphic;

            // Set the graphic data element
            DocumentFormat.OpenXml.Drawing.GraphicData graphicData = new DocumentFormat.OpenXml.Drawing.GraphicData();
            graphic.GraphicData = graphicData;

            // Specify the image type (PNG, JPEG, GIF, etc.)
            graphicData.Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture";

            // Provide image properties (example: PNG image)
            DocumentFormat.OpenXml.Drawing.Picture picture = new DocumentFormat.OpenXml.Drawing.Picture();
            graphicData.Append(picture);

            DocumentFormat.OpenXml.Drawing.NonVisualPictureProperties nonVisualPictureProperties = new DocumentFormat.OpenXml.Drawing.NonVisualPictureProperties();
            DocumentFormat.OpenXml.Drawing.NonVisualDrawingProperties nonVisualDrawingProperties = new DocumentFormat.OpenXml.Drawing.NonVisualDrawingProperties() { Id = (uint)1, Name = "Picture 1" }; 
            nonVisualPictureProperties.Append(nonVisualDrawingProperties);
            DocumentFormat.OpenXml.Drawing.NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties = new DocumentFormat.OpenXml.Drawing.NonVisualPictureDrawingProperties();
            nonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
            picture.Append(nonVisualPictureProperties);

            DocumentFormat.OpenXml.Drawing.BlipFill blipFill = new DocumentFormat.OpenXml.Drawing.BlipFill();
            DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip() { Embed = "rId1" }; // Replace "rId1" with the actual relationship ID of the image
            blipFill.Append(blip);
            DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
            blipFill.Append(stretch);
            picture.Append(blipFill); 

            DocumentFormat.OpenXml.Drawing.ShapeProperties shapeProperties = new DocumentFormat.OpenXml.Drawing.ShapeProperties();
            DocumentFormat.OpenXml.Drawing.Transform2D transform2D = new DocumentFormat.OpenXml.Drawing.Transform2D();
            DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset() { X = 0, Y = 0 };
            transform2D.Append(offset);
            DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents() { Cx = 914400, Cy = 567000 }; 
            transform2D.Append(extents);
            shapeProperties.Append(transform2D);
            picture.Append(shapeProperties);

            // Add an image part to the document (replace "image.png" with your image file)
            ImagePart imagePart = mainDocumentPart.AddImagePart(ImagePartType.Png);
            using (FileStream imageStream = File.OpenRead("image.png"))
            {
                imagePart.FeedData(imageStream);
            }
            // Set the relationship ID for the image
            imagePart.RelationshipId = "rId1";

            // Save the document
            wordDocument.Save();
        }
    }
}

Key Considerations

  • Image Dimensions: The Extent property should reflect the desired size of the inline image within the document.
  • Relationship ID: Ensure the Embed attribute of the Blip element correctly matches the relationship ID of the image part within your document.
  • Image Type: Use the correct URI in the GraphicData element based on the image format (PNG, JPEG, GIF, etc.).

Conclusion

The DocumentFormat.OpenXml.Drawing.Inline class is a powerful tool for embedding images within Open XML documents. By understanding how to initialize and set its properties, you can effectively integrate and manipulate images to create visually enriched documents. This guide provides a comprehensive overview of the initialization process and includes a practical example for embedding images in a Word document.

Latest Posts