Compartir a través de


Insertar una imagen en un documento de procesamiento de texto

En este tema se muestra cómo usar las clases del SDK de Open XML para Office para agregar mediante programación una imagen a un documento de procesamiento de texto.


Abrir un documento existente para su edición

Para abrir un documento existente, cree una instancia de la WordprocessingDocument clase como se muestra en la instrucción siguiente using . En la misma instrucción, abra el archivo de procesamiento de texto en el especificado filepath mediante el Open(String, Boolean) método , con el parámetro booleano establecido true en para habilitar la edición del documento.

using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true))

Con v3.0.0+ el Close() método se ha quitado en favor de confiar en la instrucción using. Garantiza que se llama automáticamente al Dispose() método cuando se alcanza la llave de cierre. El bloque que sigue a la instrucción using establece un ámbito para el objeto que se crea o se nombra en la instrucción using. Dado que la WordprocessingDocument clase del SDK de Open XML guarda y cierra automáticamente el objeto como parte de su IDisposable implementación y, dado Dispose() que se llama automáticamente al salir del bloque, no es necesario llamar Save() explícitamente o siempre que Dispose() use una using instrucción.


La representación XML del objeto gráfico

El siguiente texto de la especificación ISO/IEC 29500 presenta el elemento de datos de objeto gráfico.

Este elemento especifica la referencia a un objeto gráfico en el documento. Este objeto gráfico lo proporcionan en su totalidad los autores del documento que desean conservar estos datos en el documento.

[Nota: según el tipo de objeto gráfico que se usa, es posible que no todas las aplicaciones de generación compatibles con el marco OOXML tengan la posibilidad de representar el objeto gráfico. Fin de la nota]

© ISO/IEC 29500: 2016

El siguiente fragmento de esquema XML define el contenido de este elemento

    <complexType name="CT_GraphicalObjectData">
       <sequence>
           <any minOccurs="0" maxOccurs="unbounded" processContents="strict"/>
       </sequence>
       <attribute name="uri" type="xsd:token"/>
    </complexType>

Funcionamiento del código de ejemplo

Después de abrir el documento, agregue el ImagePart objeto al MainDocumentPart objeto mediante una secuencia de archivos, como se muestra en el siguiente segmento de código.

MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
    imagePart.FeedData(stream);
}

AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));

Para agregar la imagen al cuerpo, primero debe definir la referencia de la imagen. A continuación, anexe la referencia al cuerpo. El elemento debe estar en .Run

// Define the reference of the image.
var element =
     new Drawing(
         new DW.Inline(
             new DW.Extent() { Cx = 990000L, Cy = 792000L },
             new DW.EffectExtent()
             {
                 LeftEdge = 0L,
                 TopEdge = 0L,
                 RightEdge = 0L,
                 BottomEdge = 0L
             },
             new DW.DocProperties()
             {
                 Id = (UInt32Value)1U,
                 Name = "Picture 1"
             },
             new DW.NonVisualGraphicFrameDrawingProperties(
                 new A.GraphicFrameLocks() { NoChangeAspect = true }),
             new A.Graphic(
                 new A.GraphicData(
                     new PIC.Picture(
                         new PIC.NonVisualPictureProperties(
                             new PIC.NonVisualDrawingProperties()
                             {
                                 Id = (UInt32Value)0U,
                                 Name = "New Bitmap Image.jpg"
                             },
                             new PIC.NonVisualPictureDrawingProperties()),
                         new PIC.BlipFill(
                             new A.Blip(
                                 new A.BlipExtensionList(
                                     new A.BlipExtension()
                                     {
                                         Uri =
                                            "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                     })
                             )
                             {
                                 Embed = relationshipId,
                                 CompressionState =
                                 A.BlipCompressionValues.Print
                             },
                             new A.Stretch(
                                 new A.FillRectangle())),
                         new PIC.ShapeProperties(
                             new A.Transform2D(
                                 new A.Offset() { X = 0L, Y = 0L },
                                 new A.Extents() { Cx = 990000L, Cy = 792000L }),
                             new A.PresetGeometry(
                                 new A.AdjustValueList()
                             )
                             { Preset = A.ShapeTypeValues.Rectangle }))
                 )
                 { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
         )
         {
             DistanceFromTop = (UInt32Value)0U,
             DistanceFromBottom = (UInt32Value)0U,
             DistanceFromLeft = (UInt32Value)0U,
             DistanceFromRight = (UInt32Value)0U,
             EditId = "50D07946"
         });

if (wordDoc.MainDocumentPart is null || wordDoc.MainDocumentPart.Document.Body is null)
{
    throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}

// Append the reference to body, the element should be in a Run.
wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));

Código de ejemplo

En el ejemplo de código siguiente se agrega una imagen a un documento de Word existente. En el código, puede llamar al InsertAPicture método pasando la ruta de acceso del documento de word y la ruta de acceso del archivo que contiene la imagen. Por ejemplo, la siguiente llamada inserta la imagen.

string documentPath = args[0];
string picturePath = args[1];

InsertAPicture(documentPath, picturePath);

Después de ejecutar el código, examine el archivo para ver la imagen insertada.

A continuación se incluye el código de ejemplo completo en C# y Visual Basic.

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.IO;
using A = DocumentFormat.OpenXml.Drawing;
using DW = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using PIC = DocumentFormat.OpenXml.Drawing.Pictures;


static void InsertAPicture(string document, string fileName)
{
    using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(document, true))
    {
        if (wordprocessingDocument.MainDocumentPart is null)
        {
            throw new ArgumentNullException("MainDocumentPart is null.");
        }

        MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

        ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);

        using (FileStream stream = new FileStream(fileName, FileMode.Open))
        {
            imagePart.FeedData(stream);
        }

        AddImageToBody(wordprocessingDocument, mainPart.GetIdOfPart(imagePart));
    }
}

static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
{
    // Define the reference of the image.
    var element =
         new Drawing(
             new DW.Inline(
                 new DW.Extent() { Cx = 990000L, Cy = 792000L },
                 new DW.EffectExtent()
                 {
                     LeftEdge = 0L,
                     TopEdge = 0L,
                     RightEdge = 0L,
                     BottomEdge = 0L
                 },
                 new DW.DocProperties()
                 {
                     Id = (UInt32Value)1U,
                     Name = "Picture 1"
                 },
                 new DW.NonVisualGraphicFrameDrawingProperties(
                     new A.GraphicFrameLocks() { NoChangeAspect = true }),
                 new A.Graphic(
                     new A.GraphicData(
                         new PIC.Picture(
                             new PIC.NonVisualPictureProperties(
                                 new PIC.NonVisualDrawingProperties()
                                 {
                                     Id = (UInt32Value)0U,
                                     Name = "New Bitmap Image.jpg"
                                 },
                                 new PIC.NonVisualPictureDrawingProperties()),
                             new PIC.BlipFill(
                                 new A.Blip(
                                     new A.BlipExtensionList(
                                         new A.BlipExtension()
                                         {
                                             Uri =
                                                "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                         })
                                 )
                                 {
                                     Embed = relationshipId,
                                     CompressionState =
                                     A.BlipCompressionValues.Print
                                 },
                                 new A.Stretch(
                                     new A.FillRectangle())),
                             new PIC.ShapeProperties(
                                 new A.Transform2D(
                                     new A.Offset() { X = 0L, Y = 0L },
                                     new A.Extents() { Cx = 990000L, Cy = 792000L }),
                                 new A.PresetGeometry(
                                     new A.AdjustValueList()
                                 )
                                 { Preset = A.ShapeTypeValues.Rectangle }))
                     )
                     { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
             )
             {
                 DistanceFromTop = (UInt32Value)0U,
                 DistanceFromBottom = (UInt32Value)0U,
                 DistanceFromLeft = (UInt32Value)0U,
                 DistanceFromRight = (UInt32Value)0U,
                 EditId = "50D07946"
             });

    if (wordDoc.MainDocumentPart is null || wordDoc.MainDocumentPart.Document.Body is null)
    {
        throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
    }

    // Append the reference to body, the element should be in a Run.
    wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
}

Recursos adicionales