docx
    Preparing search index...

    Class Media

    Manages embedded media (images) in a document.

    Media stores all images referenced in the document and provides access to their data for packaging into the DOCX file. Each image is stored under a unique key, such as a hash of its data, so an image used twice is stored once.

    In the package, images are named as Word names them: image1.png, image2.png and so on in word/media, numbered in the order they are added. The numbering is by type, so a document with a PNG and a JPEG has image1.png and image1.jpg. Word for the web shows images with Word's own names, and has been seen to show an empty box in place of an image whose name isn't like them. The IMediaData.fileName of an image in Array is the name it is written with, not the key it was added under.

    const media = new Media();
    media.addImage("4bf7547c9322fd2143bd3e8dfa84109d9d3fed14.png", {
    type: "png",
    fileName: "4bf7547c9322fd2143bd3e8dfa84109d9d3fed14.png",
    transformation: {
    pixels: { x: 200, y: 100 },
    emus: { x: 1828800, y: 914400 }
    },
    data: imageBuffer
    });
    const [image] = media.Array; // image.fileName is "image1.png"
    Index
    • Parameters

      • takePath: MediaPathTaker = ...

        Gives each image its path under word/, by its extension, so that it doesn't take the path of another part of the package, such as a template's image. Default numbers them by type, media/image1.png, media/image2.png and so on

      Returns Media

    • get Array(): readonly IMediaData[]

      Gets all images as an array, in the order they were added, each with the file name it is written with.

      Returns readonly IMediaData[]

      Read-only array of all media data in the collection

    • get Entries(): readonly (readonly [string, IMediaData])[]

      Gets all images with the keys they were added under, in the order they were added.

      Returns readonly (readonly [string, IMediaData])[]

      Read-only array of each image's key and media data, whose file name is the one it is written with

    • Adds an image to the media collection, once: an image added again under the same key, such as one used in the body and in a header, keeps the file name it was given the first time.

      Parameters

      • key: string

        Unique identifier for this image, which the XML refers to it by until it is packed

      • mediaData: IMediaData

        Complete image data including type, transformation, and raw data

      Returns void