Pictures

A Picture element is a type of Shape element that contains an image. GemBox.Document supports pictures of various PictureFormat like PNG, JPEG, GIF, TIFF, EMF and WMF.

Pictures can be inline (of InlineLayout type), in which case the position of an image is constrained to the lines of text on the page. Alternatively, they can be floating (of FloatingLayout type), in which case they can be positioned anywhere on the page.

You can read more about this on the Shapes and Layout help page.

The following example demonstrates how you can create pictures of different formats, sizes, and positions.

Screenshot of Word file with pictures
Word document with Picture elements
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        var section = new Section(document);
        document.Sections.Add(section);

        var paragraph = new Paragraph(document);
        section.Blocks.Add(paragraph);

        // Create and add an inline picture with GIF image.
        Picture picture1 = new Picture(document, "%#Zahnrad.gif%", 61, 53, LengthUnit.Pixel);
        paragraph.Inlines.Add(picture1);

        // Create and add a floating picture with PNG image.
        Picture picture2 = new Picture(document, "%#Dices.png%");
        FloatingLayout layout2 = new FloatingLayout(
            new HorizontalPosition(HorizontalPositionType.Left, HorizontalPositionAnchor.Page),
            new VerticalPosition(2, LengthUnit.Inch, VerticalPositionAnchor.Page),
            picture2.Layout.Size);
        layout2.WrappingStyle = TextWrappingStyle.InFrontOfText;

        picture2.Layout = layout2;
        paragraph.Inlines.Add(picture2);

        // Create and add a floating picture with WMF image.
        Picture picture3 = new Picture(document, "%#Graphics1.wmf%", 378, 189, LengthUnit.Pixel);
        FloatingLayout layout3 = new FloatingLayout(
            new HorizontalPosition(3.5, LengthUnit.Inch, HorizontalPositionAnchor.Page),
            new VerticalPosition(2, LengthUnit.Inch, VerticalPositionAnchor.Page),
            picture3.Layout.Size);
        layout3.WrappingStyle = TextWrappingStyle.BehindText;

        picture3.Layout = layout3;
        paragraph.Inlines.Add(picture3);

        document.Save("Pictures.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As New DocumentModel()

        Dim section As New Section(document)
        document.Sections.Add(section)

        Dim paragraph As New Paragraph(document)
        section.Blocks.Add(paragraph)

        ' Create and add an inline picture with GIF image.
        Dim picture1 As New Picture(document, "%#Zahnrad.gif%", 61, 53, LengthUnit.Pixel)
        paragraph.Inlines.Add(picture1)

        ' Create and add a floating picture with PNG image.
        Dim picture2 As New Picture(document, "%#Dices.png%")
        Dim layout2 As New FloatingLayout(
            New HorizontalPosition(HorizontalPositionType.Left, HorizontalPositionAnchor.Page),
            New VerticalPosition(2, LengthUnit.Inch, VerticalPositionAnchor.Page),
            picture2.Layout.Size)
        layout2.WrappingStyle = TextWrappingStyle.InFrontOfText

        picture2.Layout = layout2
        paragraph.Inlines.Add(picture2)

        ' Create and add a floating picture with WMF image.
        Dim picture3 As New Picture(document, "%#Graphics1.wmf%", 378, 189, LengthUnit.Pixel)
        Dim layout3 As New FloatingLayout(
            New HorizontalPosition(3.5, LengthUnit.Inch, HorizontalPositionAnchor.Page),
            New VerticalPosition(2, LengthUnit.Inch, VerticalPositionAnchor.Page),
            picture3.Layout.Size)
        layout3.WrappingStyle = TextWrappingStyle.BehindText

        picture3.Layout = layout3
        paragraph.Inlines.Add(picture3)

        document.Save("Pictures.%OutputFileType%")

    End Sub
End Module

Even though GemBox.Document provides quite a few Picture constructors that can enable you to specify the element's size and layout, note that you can resize the element to a desired size after inserting it into a document with the Picture.Layout.Size property.

You can also replace an image with a new image by using the Picture.PictureStream property.

Check next example or download examples from GitHub.